OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
introduce cache_if option for custom field injector
ulferts8742bcb8057f
diff --git a/lib/api/decorators/linked_resource.rb b/lib/api/decorators/linked_resource.rbindex 9f8faef2562..1cc72d0acce 100644--- a/lib/api/decorators/linked_resource.rb+++ b/lib/api/decorators/linked_resource.rb@@ -61,11 +61,12 @@ module APIsetter:,link:,uncacheable_link: false,+ link_cache_if: nil,show_if: ->(*) { true },skip_render: nil,embedded: true)- link(link_attr(name, uncacheable_link), &link)+ link(link_attr(name, uncacheable_link, link_cache_if), &link)property name,exec_context: :decorator,@@ -83,11 +84,12 @@ module APIsetter:,link:,uncacheable_link: false,+ link_cache_if: nil,show_if: ->(*) { true },skip_render: nil,embedded: true)- links(link_attr(name, uncacheable_link), &link)+ links(link_attr(name, uncacheable_link, link_cache_if), &link)property name,exec_context: :decorator,@@ -144,9 +146,10 @@ module APIskip_render:)end- def link_attr(name, uncacheable)+ def link_attr(name, uncacheable, link_cache_if)links_attr = { rel: name.to_s.camelize(:lower) }links_attr[:uncacheable] = true if uncacheable+ links_attr[:cache_if] = link_cache_if if link_cache_iflinks_attrenddiff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rbindex eef19572a3f..82bf0cf2bf6 100644--- a/lib/api/v3/projects/project_representer.rb+++ b/lib/api/v3/projects/project_representer.rb@@ -39,6 +39,12 @@ module APIinclude API::Decorators::FormattablePropertyextend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass+ def self.current_user_view_allowed_lambda+ ->(*) { current_user.allowed_in_project?(:view_project, represented) || current_user.allowed_globally?(:add_project) }+ end++ custom_field_injector cache_if: current_user_view_allowed_lambda+cached_representer disabled: falseself_link@@ -183,15 +189,18 @@ module APIproperty :publicformattable_property :description,- cache_if: -> { current_user.allowed_in_project?(:view_project, represented) }+ cache_if: current_user_view_allowed_lambdadate_time_property :created_atdate_time_property :updated_atresource :status,- show_if: ->(*) { !current_user.allowed_in_project?(:view_project, represented) },- uncacheable_link: true,+ skip_render: ->(*) {+ !current_user.allowed_in_project?(:view_project, represented) &&+ !current_user.allowed_globally?(:add_project)+ },+ link_cache_if: current_user_view_allowed_lambda,getter: ->(*) {next unless represented.status_code@@ -199,9 +208,7 @@ module API.create(represented.status_code, current_user:, embed_links:)},link: ->(*) {- if !current_user.allowed_in_project?(:view_project, represented)- { href: nil }- elsif represented.status_code+ if represented.status_code{href: api_v3_paths.project_status(represented.status_code),title: I18n.t(:"activerecord.attributes.project.status_codes.#{represented.status_code}",@@ -222,7 +229,7 @@ module API}formattable_property :status_explanation,- cache_if: -> { current_user.allowed_in_project?(:view_project, represented) }+ cache_if: current_user_view_allowed_lambdadef _type'Project'diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rbindex 4895ff7d7de..3c61e835050 100644--- a/lib/api/v3/utilities/custom_field_injector.rb+++ b/lib/api/v3/utilities/custom_field_injector.rb@@ -61,7 +61,7 @@ module APIdef create_value_representer(custom_fields, representer)new_representer_class_with(representer) do |injector|custom_fields.each do |custom_field|- injector.inject_value(custom_field)+ injector.inject_value(custom_field, representer.custom_field_injector_config)endendend@@ -116,12 +116,12 @@ module APIendend- def inject_value(custom_field)+ def inject_value(custom_field, config)case custom_field.field_formatwhen *LINK_FORMATS- inject_link_value(custom_field)+ inject_link_value(custom_field, config)else- inject_property_value(custom_field)+ inject_property_value(custom_field, config)endend@@ -181,7 +181,7 @@ module APIoptions: cf_options(custom_field)end- def inject_link_value(custom_field)+ def inject_link_value(custom_field, config)name = property_name(custom_field)expected_namespace = NAMESPACE_MAP[custom_field.field_format]@@ -197,6 +197,8 @@ module API@class.send(method,property_name(custom_field),+ link_cache_if: config[:cache_if],+ skip_render: config[:cache_if] ? ->(*) { !instance_exec(&config[:cache_if]) } : nil,link:,setter:,getter:)@@ -242,11 +244,12 @@ module APIendend- def inject_property_value(custom_field)+ def inject_property_value(custom_field, config)@class.property custom_field.attribute_name.to_sym,as: property_name(custom_field),getter: property_value_getter_for(custom_field),setter: property_value_setter_for(custom_field),+ cache_if: config[:cache_if],render_nil: trueenddiff --git a/lib/api/v3/utilities/custom_field_sum_injector.rb b/lib/api/v3/utilities/custom_field_sum_injector.rbindex 5b3c94fe246..58bb65fdfdf 100644--- a/lib/api/v3/utilities/custom_field_sum_injector.rb+++ b/lib/api/v3/utilities/custom_field_sum_injector.rb@@ -34,7 +34,7 @@ module APIinject_basic_schema(custom_field)end- def inject_basic_schema(custom_field)+ def inject_basic_schema(custom_field, _options = {})@class.schema property_name(custom_field),type: TYPE_MAP[custom_field.field_format],name_source: ->(*) { custom_field.name },@@ -43,7 +43,7 @@ module APIshow_if: ->(*) { custom_field.summable? }end- def inject_property_value(custom_field)+ def inject_property_value(custom_field, _options = {})@class.property property_name(custom_field),getter: property_value_getter_for(custom_field),setter: property_value_setter_for(custom_field),diff --git a/spec/lib/api/v3/projects/project_collection_representer_spec.rb b/spec/lib/api/v3/projects/project_collection_representer_spec.rbindex 37372ab8c73..0e5556b210a 100644--- a/spec/lib/api/v3/projects/project_collection_representer_spec.rb+++ b/spec/lib/api/v3/projects/project_collection_representer_spec.rb@@ -75,8 +75,8 @@ RSpec.describe API::V3::Projects::ProjectCollectionRepresenter doenddescribe '.checked_permissions' do- it 'lists add_work_packages' do- expect(described_class.checked_permissions).to match_array([:add_work_packages])+ it 'lists add_work_packages and view_projects' do+ expect(described_class.checked_permissions).to contain_exactly(:add_work_packages, :view_project)endendenddiff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rbindex a3d2c634eae..b4754658a9b 100644--- a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb@@ -160,6 +160,29 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' doexpect(subject).not_to have_json_path("customField#{int_custom_field.id}")endend++ context 'if the user is no admin and the field is visible' do+ before do+ int_custom_field.visible = true+ end++ it "has a property for the int custom field" do+ expect(subject).to be_json_eql(int_custom_value.value.to_json)+ .at_path("customField#{int_custom_field.id}")+ end+ end++ context 'if the user lacks the :view_project permission' do+ let(:permissions) { [] }++ before do+ int_custom_field.visible = true+ end++ it "has no property for the int custom field" do+ expect(subject).not_to have_json_path("customField#{int_custom_field.id}")+ end+ endendend@@ -359,12 +382,9 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' docontext 'if the user does not have the view_project permission' dolet(:permissions) { [] }- it_behaves_like 'has an untitled link' do+ it_behaves_like 'has no link' dolet(:link) { 'status' }- let(:href) { nil }end-- it_behaves_like 'no property', 'status'endend@@ -505,7 +525,7 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' doversion_custom_field.visible = falseend- it "has no property for the int custom field" do+ it "does not link the custom field" doexpect(subject).not_to have_json_path("links/customField#{version_custom_field.id}")endend@@ -516,6 +536,14 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do.at_path("_links/customField#{version_custom_field.id}/href")endend++ context 'if the user lacks the :view_project permission' do+ let(:permissions) { [] }++ it 'does not link the custom field' do+ expect(subject).not_to have_json_path("links/customField#{version_custom_field.id}")+ end+ endenddescribe 'update' do@@ -613,6 +641,38 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' doendendend++ describe 'status' do+ let(:embedded_path) { '_embedded/status' }++ it 'has the status embedded' do+ expect(generated)+ .to be_json_eql('ProjectStatus'.to_json)+ .at_path("#{embedded_path}/_type")++ expect(generated)+ .to be_json_eql(I18n.t("activerecord.attributes.project.status_codes.#{project.status_code}").to_json)+ .at_path("#{embedded_path}/name")+ end++ context 'if the status_code is nil' do+ before { project.status_code = nil }++ it 'has no status embedded' do+ expect(generated)+ .not_to have_json_path(embedded_path)+ end+ end++ context 'if the user does not have the view_project permission' do+ let(:permissions) { [] }++ it 'has no status embedded' do+ expect(generated)+ .not_to have_json_path(embedded_path)+ end+ end+ endenddescribe 'caching' dodiff --git a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb b/spec/lib/api/v3/utilities/custom_field_injector_spec.rbindex 27ac3a4a65b..642641db58f 100644--- a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb+++ b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb@@ -340,7 +340,13 @@ RSpec.describe API::V3::Utilities::CustomFieldInjector doendend- let(:base_class) { Class.new(API::Decorators::Single) }+ let(:base_class) do+ Class.new(API::Decorators::Single) do+ def self.custom_field_injector_config+ {}+ end+ end+ endlet(:modified_class) { described_class.create_value_representer([custom_field], base_class) }let(:represented) dodouble('represented',@@ -522,7 +528,13 @@ RSpec.describe API::V3::Utilities::CustomFieldInjector doenddescribe '#inject_patchable_link_value' do- let(:base_class) { Class.new(API::Decorators::Single) }+ let(:base_class) do+ Class.new(API::Decorators::Single) do+ def self.custom_field_injector_config+ {}+ end+ end+ endlet(:modified_class) dodescribed_class.create_value_representer([custom_field], base_class)end