move writability decision into WP schema

- reducing the method count (aka interface size) of the schema
- giving it more control
- still allow outside override in representer (for plugin compatibility)
pull/3338/head
Jan Sandbrink 9 years ago
parent 0a73e6bac4
commit d5fe64bd5e
  1. 2
      lib/api/decorators/schema.rb
  2. 40
      lib/api/v3/work_packages/schema/work_package_schema.rb
  3. 29
      lib/api/v3/work_packages/schema/work_package_schema_representer.rb

@ -35,7 +35,7 @@ module API
type:,
name_source: property,
required: true,
writable: true,
writable: -> { represented.writable?(property) },
min_length: nil,
max_length: nil,
regular_expression: nil,

@ -84,23 +84,39 @@ module API
project.all_work_package_custom_fields.to_a & type.custom_fields.to_a
end
def percentage_done_writable?
if Setting.work_package_done_ratio == 'status' ||
Setting.work_package_done_ratio == 'disabled'
return false
def writable?(property)
case property
when :percentage_done
percentage_done_writable?
when :estimated_time, :start_date, :due_date
nil_or_leaf?(@work_package)
else
writable_properties.include? property
end
nil_or_leaf?(@work_package)
end
def estimated_time_writable?
nil_or_leaf?(@work_package)
end
def start_date_writable?
nil_or_leaf?(@work_package)
private
def writable_properties
[
:subject,
:description,
:estimated_time,
:assignee,
:responsible,
:type,
:status,
:category,
:version,
:priority
]
end
def due_date_writable?
def percentage_done_writable?
if Setting.work_package_done_ratio == 'status' ||
Setting.work_package_done_ratio == 'disabled'
return false
end
nil_or_leaf?(@work_package)
end

@ -60,12 +60,10 @@ module API
schema :lock_version,
type: 'Integer',
name_source: -> (*) { I18n.t('api_v3.attributes.lock_version') },
writable: false
name_source: -> (*) { I18n.t('api_v3.attributes.lock_version') }
schema :id,
type: 'Integer',
writable: false
type: 'Integer'
schema :subject,
type: 'String',
@ -77,22 +75,18 @@ module API
schema :start_date,
type: 'Date',
required: false,
writable: -> { represented.start_date_writable? }
required: false
schema :due_date,
type: 'Date',
required: false,
writable: -> { represented.due_date_writable? }
required: false
schema :estimated_time,
type: 'Duration',
required: false,
writable: -> { represented.estimated_time_writable? }
required: false
schema :spent_time,
type: 'Duration',
writable: false,
show_if: -> (_) do
current_user_allowed_to(:view_time_entries, context: represented.project)
end
@ -100,24 +94,19 @@ module API
schema :percentage_done,
type: 'Integer',
name_source: :done_ratio,
writable: -> { represented.percentage_done_writable? },
show_if: -> (*) { Setting.work_package_done_ratio != 'disabled' }
schema :created_at,
type: 'DateTime',
writable: false
type: 'DateTime'
schema :updated_at,
type: 'DateTime',
writable: false
type: 'DateTime'
schema :author,
type: 'User',
writable: false
type: 'User'
schema :project,
type: 'Project',
writable: false
type: 'Project'
schema_with_allowed_link :assignee,
type: 'User',

Loading…
Cancel
Save