diff --git a/lib/api/decorators/schema.rb b/lib/api/decorators/schema.rb index 6c0fabdd77..5077cbbbf9 100644 --- a/lib/api/decorators/schema.rb +++ b/lib/api/decorators/schema.rb @@ -30,83 +30,95 @@ module API module Decorators class Schema < Single - def self.schema(property, - type:, - title: represented_class.human_attribute_name(property), - required: true, - writable: true, - min_length: nil, - max_length: nil) - raise ArgumentError if property.nil? - - schema = ::API::Decorators::PropertySchemaRepresenter.new(type: type, - name: title) - schema.required = required - schema.writable = writable - schema.min_length = min_length if min_length - schema.max_length = max_length if max_length - - property property, - getter: -> (*) { schema }, - writeable: false - end + class << self + def schema(property, + type:, + title: make_title(property), + required: true, + writable: true, + min_length: nil, + max_length: nil) + raise ArgumentError if property.nil? - def self.schema_with_allowed_link(property, - type: property.to_s.camelize, - title: represented_class.human_attribute_name(property), - href_callback:, - required: true, - writable: true) - raise ArgumentError if property.nil? - - property property, - exec_context: :decorator, - getter: -> (*) { - representer = ::API::Decorators::AllowedValuesByLinkRepresenter.new( - type: type, - name: title) - representer.required = required - representer.writable = writable - - if represented.defines_assignable_values? - representer.allowed_values_href = instance_eval(&href_callback) - end - - representer - } - end + schema = ::API::Decorators::PropertySchemaRepresenter.new(type: type, + name: title) + schema.required = required + schema.writable = writable + schema.min_length = min_length if min_length + schema.max_length = max_length if max_length - 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 + property property, + getter: -> (*) { schema }, + writeable: false + end + + def schema_with_allowed_link(property, + type: make_type(property), + title: make_title(property), + href_callback:, + required: true, + writable: true) + raise ArgumentError if property.nil? + + property property, + exec_context: :decorator, + getter: -> (*) { + representer = ::API::Decorators::AllowedValuesByLinkRepresenter.new( + type: type, + name: title) + representer.required = required + representer.writable = writable + + if represented.defines_assignable_values? + representer.allowed_values_href = instance_eval(&href_callback) + end + + representer + } + end + + def schema_with_allowed_collection(property, + type: make_type(property), + title: make_title(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 represented_class + end + + private + + def make_title(property_name) + represented_class.human_attribute_name(property_name) + end - def self.represented_class + def make_type(property_name) + property_name.to_s.camelize + end end def current_user