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. 86
      lib/api/v3/work_packages/schema/work_package_schema_representer.rb

@ -76,6 +76,36 @@ module API
}
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
end
end

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

Loading…
Cancel
Save