From aa956e5932659f22eddd9627168f74d9254790e5 Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Mon, 16 Feb 2015 09:31:27 +0100 Subject: [PATCH] reduce duplication inside WP schema representer --- lib/api/decorators/schema.rb | 30 ++++ .../schema/work_package_schema_representer.rb | 144 +++++++----------- 2 files changed, 83 insertions(+), 91 deletions(-) diff --git a/lib/api/decorators/schema.rb b/lib/api/decorators/schema.rb index fb4ed7f7f6..c221d0d906 100644 --- a/lib/api/decorators/schema.rb +++ b/lib/api/decorators/schema.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 diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb index 210d1f89d6..7729fe2c2d 100644 --- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb +++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb @@ -134,97 +134,59 @@ 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( - type: 'Status', - name: WorkPackage.human_attribute_name(:status), - current_user: current_user, - value_representer: API::V3::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( - type: 'Category', - name: WorkPackage.human_attribute_name(:category), - value_representer: API::V3::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 - } - - property :version, - exec_context: :decorator, - getter: -> (*) { - representer = ::API::Decorators::AllowedValuesByCollectionRepresenter.new( - type: 'Version', - 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 - } + schema_with_allowed_collection :status, + type: 'Status', + 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 + } + } + + schema_with_allowed_collection :category, + type: 'Category', + values_callback: -> (*) { + represented.assignable_categories + }, + value_representer: Categories::CategoryRepresenter, + link_factory: -> (category) { + { + href: api_v3_paths.category(category.id), + title: category.name + } + }, + required: false + + schema_with_allowed_collection :version, + type: 'Version', + values_callback: -> (*) { + represented.assignable_versions + }, + value_representer: Versions::VersionRepresenter, + link_factory: -> (version) { + { + href: api_v3_paths.version(version.id), + title: version.name + } + }, + required: false + + schema_with_allowed_collection :priority, + type: 'Priority', + values_callback: -> (*) { + represented.assignable_priorities + }, + value_representer: Priorities::PriorityRepresenter, + link_factory: -> (priority) { + { + href: api_v3_paths.priority(priority.id), + title: priority.name + } + } def current_user context[:current_user]