reduce duplication inside WP schema representer

pull/2550/head
Jan Sandbrink 10 years ago
parent a5e1c33733
commit aa956e5932
  1. 30
      lib/api/decorators/schema.rb
  2. 144
      lib/api/v3/work_packages/schema/work_package_schema_representer.rb

@ -76,6 +76,36 @@ module API
} }
end end
def self.schema_with_allowed_collection(property,
type: property.to_s.camelize,
title: represented_class.human_attribute_name(property),
values_callback:,
value_representer:,
link_factory:,
required: true,
writable: true)
raise ArgumentError unless property
property property,
exec_context: :decorator,
getter: -> (*) {
representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new(
type: type,
name: title,
current_user: current_user,
value_representer: value_representer,
link_factory: -> (value) { instance_exec(value, &link_factory) })
representer.required = required
representer.writable = writable
if represented.defines_assignable_values?
representer.allowed_values = instance_exec(&values_callback)
end
representer
}
end
def self.represented_class def self.represented_class
end end
end end

@ -134,97 +134,59 @@ module API
api_v3_paths.available_responsibles(represented.project.id) api_v3_paths.available_responsibles(represented.project.id)
} }
property :status, schema_with_allowed_collection :status,
exec_context: :decorator, type: 'Status',
getter: -> (*) { values_callback: -> (*) {
assignable_statuses = represented.assignable_statuses_for(current_user) represented.assignable_statuses_for(current_user)
representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new( },
type: 'Status', value_representer: Statuses::StatusRepresenter,
name: WorkPackage.human_attribute_name(:status), link_factory: -> (status) {
current_user: current_user, {
value_representer: API::V3::Statuses::StatusRepresenter, href: api_v3_paths.status(status.id),
link_factory: -> (status) { title: status.name
{ }
href: api_v3_paths.status(status.id), }
title: status.name
} schema_with_allowed_collection :category,
}) type: 'Category',
values_callback: -> (*) {
if represented.defines_assignable_values? represented.assignable_categories
representer.allowed_values = assignable_statuses },
end value_representer: Categories::CategoryRepresenter,
link_factory: -> (category) {
representer {
} href: api_v3_paths.category(category.id),
title: category.name
property :category, }
exec_context: :decorator, },
getter: -> (*) { required: false
representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new(
type: 'Category', schema_with_allowed_collection :version,
name: WorkPackage.human_attribute_name(:category), type: 'Version',
value_representer: API::V3::Categories::CategoryRepresenter, values_callback: -> (*) {
link_factory: -> (category) { represented.assignable_versions
{ },
href: api_v3_paths.category(category.id), value_representer: Versions::VersionRepresenter,
title: category.name link_factory: -> (version) {
} {
}) href: api_v3_paths.version(version.id),
title: version.name
representer.required = false }
},
if represented.defines_assignable_values? required: false
representer.allowed_values = represented.assignable_categories
end schema_with_allowed_collection :priority,
type: 'Priority',
representer values_callback: -> (*) {
} represented.assignable_priorities
},
property :version, value_representer: Priorities::PriorityRepresenter,
exec_context: :decorator, link_factory: -> (priority) {
getter: -> (*) { {
representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new( href: api_v3_paths.priority(priority.id),
type: 'Version', title: priority.name
name: WorkPackage.human_attribute_name(:version), }
current_user: current_user, }
value_representer: API::V3::Versions::VersionRepresenter,
link_factory: -> (version) {
{
href: api_v3_paths.version(version.id),
title: version.name
}
})
representer.required = false
if represented.defines_assignable_values?
representer.allowed_values = represented.assignable_versions
end
representer
}
property :priority,
exec_context: :decorator,
getter: -> (*) {
representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new(
type: 'Priority',
name: WorkPackage.human_attribute_name(:priority),
current_user: current_user,
value_representer: API::V3::Priorities::PriorityRepresenter,
link_factory: -> (priority) {
{
href: api_v3_paths.priority(priority.id),
title: priority.name
}
})
if represented.defines_assignable_values?
representer.allowed_values = represented.assignable_priorities
end
representer
}
def current_user def current_user
context[:current_user] context[:current_user]

Loading…
Cancel
Save