Cache all_work_package_form_attributes

Especially with lots of custom fields, this is otherwise called once per
represented WP and is quite costly.
pull/5304/head
Oliver Günther 8 years ago
parent 21e7c584a4
commit 01351399d5
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 48
      app/models/type/attributes.rb
  2. 1
      spec/controllers/projects_controller_spec.rb
  3. 5
      spec/models/type/attribute_groups_spec.rb

@ -60,30 +60,34 @@ module Type::Attributes
#
# @return [Hash{String => Hash}] Map from attribute names to options.
def all_work_package_form_attributes(merge_date: false)
rattrs = API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter.representable_attrs
definitions = rattrs[:definitions]
skip = ['_type', '_dependencies', 'attribute_groups', 'links', 'parent_id', 'parent', 'description']
attributes = definitions.keys
.reject { |key| skip.include?(key) || definitions[key][:required] }
.map { |key| [key, definitions[key]] }.to_h
# within the form date is shown as a single entry including start and due
if merge_date
attributes['date'] = { required: false, has_default: false }
attributes.delete 'due_date'
attributes.delete 'start_date'
all_attributes = RequestStore.fetch(:all_work_package_form_attributes) do
rattrs = API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter.representable_attrs
definitions = rattrs[:definitions]
skip = ['_type', '_dependencies', 'attribute_groups', 'links', 'parent_id', 'parent', 'description']
attributes = definitions.keys
.reject { |key| skip.include?(key) || definitions[key][:required] }
.map { |key| [key, definitions[key]] }.to_h
# within the form date is shown as a single entry including start and due
if merge_date
attributes['date'] = { required: false, has_default: false }
attributes.delete 'due_date'
attributes.delete 'start_date'
end
WorkPackageCustomField.includes(:translations, :custom_options).all.each do |field|
attributes["custom_field_#{field.id}"] = {
required: field.is_required,
has_default: field.default_value.present?,
is_cf: true,
display_name: field.name
}
end
attributes
end
WorkPackageCustomField.includes(:translations).all.each do |field|
attributes["custom_field_#{field.id}"] = {
required: field.is_required,
has_default: field.default_value.present?,
is_cf: true,
display_name: field.name
}
end
attributes
all_attributes
end
end

@ -246,6 +246,7 @@ describe ProjectsController, type: :controller do
before do
expect(type.attribute_visibility.keys).not_to include "custom_field_#{custom_field_1.id}"
RequestStore.clear!
request
end

@ -32,6 +32,11 @@ require 'spec_helper'
describe ::Type, type: :model do
let(:type) { FactoryGirl.build(:type) }
before do
# Clear up the request store cache for all_work_package_attributes
RequestStore.clear!
end
describe "#attribute_groups" do
it 'returns #default_attribute_groups if not yet set' do
expect(type.read_attribute(:attribute_groups)).to be_empty

Loading…
Cancel
Save