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
fix custom_field_values to depend on all value
Ivan Kuchin10139945a825
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rbindex 2c1aa76efbe..05bbb564567 100644--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb@@ -125,20 +125,7 @@ module Redminecustom_field_values(all:).select { |cv| cv.custom_field_id == id.to_i }end- def custom_field_values(all: false)- custom_field_values_cache[custom_field_cache_key] ||= begin- current_custom_fields = all ? all_available_custom_fields : available_custom_fields- current_custom_fields.flat_map do |custom_field|- existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }-- if existing_cvs.empty?- build_default_custom_values(custom_field)- else- existing_cvs- end- end- end- end+ def custom_field_values(all: false) = cached_custom_field_values[all ? :all_available : :available]# Override to extend the cache key for caching @custom_field_values_cache.#@@ -155,8 +142,8 @@ module Redmine### Maps custom_values into a hash that can be passed to attributes# but keeps multivalue custom fields as array values- def custom_value_attributes- custom_field_values.each_with_object({}) do |cv, hash|+ def custom_value_attributes(all: false)+ custom_field_values(all:).each_with_object({}) do |cv, hash|key = cv.custom_field_idvalue = cv.value@@ -213,7 +200,7 @@ module Redminedef ensure_custom_values_completereturn unless custom_values.loaded? && (custom_values.any?(&:changed?) || custom_value_destroyed)- self.custom_values = custom_field_values+ self.custom_values = custom_field_values(all: true)enddef reload(*args)@@ -335,6 +322,29 @@ module Redmineprivate+ def custom_field_values_cache+ @custom_field_values_cache ||= {}+ end++ def cached_custom_field_values+ custom_field_values_cache[custom_field_cache_key] ||= {+ all_available: uncached_custom_field_values_by_field(all_available_custom_fields),+ available: uncached_custom_field_values_by_field(available_custom_fields)+ }+ end++ def uncached_custom_field_values_by_field(custom_fields)+ custom_fields.flat_map do |custom_field|+ existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }++ if existing_cvs.empty?+ build_default_custom_values(custom_field)+ else+ existing_cvs+ end+ end+ end+def build_default_custom_values(custom_field)if custom_field.multi_value? && custom_field.default_value.present?custom_field.default_value.map do |value|@@ -431,21 +441,17 @@ module Redminecustom_field_id:,value:)- custom_field_values.push(new_custom_value)+ cached_custom_field_values.each_value { it.push new_custom_value }enddef remove_custom_value(custom_value)return unless custom_valuecustom_value.mark_for_destruction- custom_field_values.delete custom_value+ cached_custom_field_values.each_value { it.delete custom_value }self.custom_value_destroyed = trueend- def custom_field_values_cache- @custom_field_values_cache ||= {}- end-module AddClassMethodsdef available_custom_fields(_model)RequestStore.fetch(:"#{name.underscore}_custom_fields") dodiff --git a/spec/features/projects/copy_spec.rb b/spec/features/projects/copy_spec.rbindex 6bad3fd98de..3bda513e53e 100644--- a/spec/features/projects/copy_spec.rb+++ b/spec/features/projects/copy_spec.rb@@ -342,9 +342,7 @@ RSpec.describe "Projects copy", :js,endcontext "with non-admin user" do- it "does not show invisible fields in the form and but still activates them" do- pending "Admin-only project attributes currently prevent users from creating projects (OP#64479)"-+ it "does not show invisible fields in the form but still activates them" doexpect(page).to have_heading "Copy project \"#{project.name}\""expect(page).to have_no_content "Text for Admins only"diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rbindex c8709ba15cb..1982bcc938b 100644--- a/spec/models/projects/customizable_spec.rb+++ b/spec/models/projects/customizable_spec.rb@@ -168,7 +168,7 @@ RSpec.describe Project, "customizable" do.to eq("foo")expect(project.custom_value_for(bool_custom_field).typed_value).to be_truthy- expect(project.custom_value_for(list_custom_field).typed_value)+ expect(project.custom_value_for(list_custom_field)).to be_nilenddiff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rbindex da3e637d13f..25c8948b9c1 100644--- a/spec/requests/api/v3/projects/update_resource_spec.rb+++ b/spec/requests/api/v3/projects/update_resource_spec.rb@@ -147,18 +147,20 @@ RSpec.describe "API v3 Project resource update", content_type: :json doendit "does not set the cf value" do- expect(project.reload.custom_values)- .to be_empty+ expect(project.reload.custom_values.find_by(custom_field: admin_only_custom_field))+ .to have_attributes(value: nil)endcontext "when the hidden field has a value already" do- it "does not change the cf value" do- project.custom_field_values = { admin_only_custom_field.id => "1234" }- project.save+ before do+ project.update!(custom_field_values: { admin_only_custom_field.id => "1234" })+patch path, body.to_json+ end- expect(project.reload.custom_values.find_by(custom_field: admin_only_custom_field).value)- .to eq "1234"+ it "does not change the cf value" do+ expect(project.reload.custom_values.find_by(custom_field: admin_only_custom_field))+ .to have_attributes(value: "1234")endenddiff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex 44ff05f2116..e76487a19fb 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -503,6 +503,12 @@ RSpec.describe Projects::SetAttributesService, type: :model doit "calculates only accessible values" doexpect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated1.id => "21",+ cf_calculated3.id => "-6"+ )++ expect(subject.result.custom_value_attributes(all: true)).to eq(cf_static.id => "3",cf_calculated1.id => "21",cf_calculated2.id => "-6",@@ -546,6 +552,12 @@ RSpec.describe Projects::SetAttributesService, type: :model doit "calculates only accessible values" doexpect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated1.id => "21",+ cf_calculated3.id => "-6"+ )++ expect(subject.result.custom_value_attributes(all: true)).to eq(cf_static.id => "3",cf_calculated1.id => "21",cf_calculated2.id => "-6",