cache classes with dynamically injected custom attributes

pull/4126/head
Jens Ulferts 9 years ago
parent af09bf7017
commit e1b3845582
  1. 56
      lib/api/v3/utilities/custom_field_injector.rb
  2. 18
      lib/api/v3/work_packages/work_package_collection_representer.rb

@ -65,21 +65,19 @@ module API
class << self class << self
def create_value_representer(customizable, representer) def create_value_representer(customizable, representer)
injector = CustomFieldInjector.new(representer) new_representer_class_with(representer, customizable) do |injector|
customizable.available_custom_fields.each do |custom_field| customizable.available_custom_fields.each do |custom_field|
injector.inject_value(custom_field, embed_links: true) injector.inject_value(custom_field, embed_links: true)
end
end end
injector.modified_representer_class
end end
def create_schema_representer(customizable, representer) def create_schema_representer(customizable, representer)
injector = CustomFieldInjector.new(representer) new_representer_class_with(representer, customizable) do |injector|
customizable.available_custom_fields.each do |custom_field| customizable.available_custom_fields.each do |custom_field|
injector.inject_schema(custom_field, customized: customizable) injector.inject_schema(custom_field, customized: customizable)
end
end end
injector.modified_representer_class
end end
def create_value_representer_for_property_patching(customizable, representer) def create_value_representer_for_property_patching(customizable, representer)
@ -87,12 +85,11 @@ module API
property_field?(cf) property_field?(cf)
} }
injector = CustomFieldInjector.new(representer) new_representer_class_with(representer, customizable) do |injector|
property_fields.each do |custom_field| property_fields.each do |custom_field|
injector.inject_value(custom_field) injector.inject_value(custom_field)
end
end end
injector.modified_representer_class
end end
def create_value_representer_for_link_patching(customizable, representer) def create_value_representer_for_link_patching(customizable, representer)
@ -100,12 +97,11 @@ module API
linked_field?(cf) linked_field?(cf)
} }
injector = CustomFieldInjector.new(representer) new_representer_class_with(representer, customizable) do |injector|
linked_fields.each do |custom_field| linked_fields.each do |custom_field|
injector.inject_patchable_link_value(custom_field) injector.inject_patchable_link_value(custom_field)
end
end end
injector.modified_representer_class
end end
def linked_field?(custom_field) def linked_field?(custom_field)
@ -115,10 +111,26 @@ module API
def property_field?(custom_field) def property_field?(custom_field)
!linked_field?(custom_field) !linked_field?(custom_field)
end end
def new_representer_class_with(representer, customizable, &block)
injector = new(representer, customizable)
block.call injector
injector.modified_representer_class
end
end end
def initialize(representer_class) def initialize(representer_class, customizable)
@class = Class.new(representer_class) @class = Class.new(representer_class) do
class << self
attr_accessor :customizable
end
end
@class.customizable = customizable
@class
end end
def modified_representer_class def modified_representer_class

@ -57,6 +57,24 @@ module API
current_user: current_user) current_user: current_user)
end end
collection :elements,
getter: -> (*) {
generated_classes = ::Hash.new do |hash, work_package|
hit = hash.values.find { |klass|
klass.customizable.type_id == work_package.type_id &&
klass.customizable.project_id == work_package.project_id
}
hash[work_package] = hit || element_decorator.create_class(work_package)
end
represented.map { |model|
generated_classes[model].new(model, current_user: current_user)
}
},
exec_context: :decorator,
embedded: true
property :groups, property :groups,
exec_context: :decorator, exec_context: :decorator,
getter: -> (*) { getter: -> (*) {

Loading…
Cancel
Save