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

@ -57,6 +57,24 @@ module API
current_user: current_user)
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,
exec_context: :decorator,
getter: -> (*) {

Loading…
Cancel
Save