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
[#66738] Admin only static and non affected calculated values are missing from calculations
Ivan Kuchin728678ba5e5e
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rbindex 9984f8f207b..566f7156d9c 100644--- a/app/models/acts_as_customizable/calculated_value.rb+++ b/app/models/acts_as_customizable/calculated_value.rb@@ -60,7 +60,7 @@ module ActsAsCustomizable::CalculatedValuedef calculated_value_fields_referenced_values(custom_fields)given_cf_ids = custom_fields.flat_map(&:formula_referenced_custom_field_ids).uniq - custom_fields.map(&:id)- custom_field_values+ custom_field_values(all: true).select { it.custom_field_id.in?(given_cf_ids) }.to_h { [it.custom_field.column_name, it.typed_value] }enddiff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rbindex 462b4d27ef8..eca2566922c 100644--- a/spec/models/acts_as_customizable/calculated_value_spec.rb+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb@@ -35,14 +35,16 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuClass.new doinclude ActsAsCustomizable::CalculatedValue- attr_accessor :custom_field_values+ def custom_field_values(*) = nil+ attr_writer :custom_field_valuesendendlet(:instance) { model_class.new }before do- allow(instance).to receive_messages(custom_field_values:, :custom_field_values= => nil)+ allow(instance).to receive(:custom_field_values).with(all: true).and_return(custom_field_values)+ allow(instance).to receive(:custom_field_values=)enddef ref(custom_field) = "{{cf_#{custom_field.id}}}"diff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex 1075889fbef..df07cb5be9f 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -550,7 +550,7 @@ RSpec.describe Projects::SetAttributesService, type: :model docreate(:custom_value, customized: project, custom_field: cf_calculated3, value: -6)end- it "calculates only accessible values" do+ it "calculates all values" doexpect(subject.result.custom_value_attributes).to eq(cf_static.id => "3",cf_calculated1.id => "21",@@ -559,6 +559,102 @@ RSpec.describe Projects::SetAttributesService, type: :model do)endend++ context "when referenced value field is for admin only" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_static} * #{ref cf_referenced}")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => 3+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: 1)+ create(:custom_value, customized: project, custom_field: cf_calculated, value: -6)+ end++ context "when referenced value is static" do+ let!(:cf_referenced) { create(:integer_project_custom_field, :admin_only, projects: [project]) }++ before do+ create(:custom_value, customized: project, custom_field: cf_referenced, value: 2)+ end++ it "calculates using existing value" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated.id => "6"+ )++ expect(subject.result.custom_value_attributes(all: true)).to eq(+ cf_static.id => "3",+ cf_referenced.id => "2",+ cf_calculated.id => "6"+ )+ end+ end++ context "when referenced value is calculated value without references" do+ let!(:cf_referenced) do+ create(:calculated_value_project_custom_field, :skip_validations, :admin_only,+ projects: [project],+ formula: "21 * -2")+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_referenced, value: 2)+ end++ it "calculates using existing value" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated.id => "6"+ )++ expect(subject.result.custom_value_attributes(all: true)).to eq(+ cf_static.id => "3",+ cf_referenced.id => "2",+ cf_calculated.id => "6"+ )+ end+ end++ context "when referenced value is calculated value with unchanged reference" do+ let!(:cf_referenced1) { create(:integer_project_custom_field, :admin_only, projects: [project]) }+ let!(:cf_referenced) do+ create(:calculated_value_project_custom_field, :skip_validations, :admin_only,+ projects: [project],+ formula: "21 * #{ref cf_referenced1}")+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_referenced1, value: -2)+ create(:custom_value, customized: project, custom_field: cf_referenced, value: 2)+ end++ it "calculates using existing value" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated.id => "6"+ )++ expect(subject.result.custom_value_attributes(all: true)).to eq(+ cf_static.id => "3",+ cf_referenced1.id => "-2",+ cf_referenced.id => "2",+ cf_calculated.id => "6"+ )+ end+ end+ endendendend