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
handle disabled fields during calculation
Ivan Kuchina7b82a6c72be
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rbindex 566f7156d9c..8a719da2bd8 100644--- a/app/models/acts_as_customizable/calculated_value.rb+++ b/app/models/acts_as_customizable/calculated_value.rb@@ -32,6 +32,13 @@ module ActsAsCustomizable::CalculatedValueextend ActiveSupport::Concernincluded do+ def enabled_custom_field_ids+ fail NoMethodError, <<~DESCRIPTION.squish+ Required for calculated_value custom fields in calculate_custom_fields method.+ Define which fields are enabled, not to be confused with visible, as enabled should not depend on current user.+ DESCRIPTION+ end+def calculate_custom_fields(custom_fields)return if custom_fields.empty?@@ -40,9 +47,11 @@ module ActsAsCustomizable::CalculatedValue"Expected array of calculated value custom fields"end+ enabled_ids = enabled_custom_field_ids+result = calculate_custom_fields_result(- given: calculated_value_fields_referenced_values(custom_fields),- to_compute: custom_fields.to_h { [it.column_name, it.formula_str_without_patterns] }+ given: calculated_value_fields_given(custom_fields:, enabled_ids:),+ to_compute: calculated_value_fields_to_compute(custom_fields:, enabled_ids:))self.custom_field_values = custom_fields.to_h { [it.id, result[it.column_name]] }@@ -57,12 +66,19 @@ module ActsAsCustomizable::CalculatedValuecalculator.solve(to_compute).reject { |_, value| value == :undefined }end- def calculated_value_fields_referenced_values(custom_fields)- given_cf_ids = custom_fields.flat_map(&:formula_referenced_custom_field_ids).uniq - custom_fields.map(&:id)+ def calculated_value_fields_given(custom_fields:, enabled_ids:)+ referenced_ids = custom_fields.flat_map(&:formula_referenced_custom_field_ids)+ given_ids = (enabled_ids & referenced_ids) - custom_fields.map(&:id)custom_field_values(all: true)- .select { it.custom_field_id.in?(given_cf_ids) }+ .select { it.custom_field_id.in?(given_ids) }.to_h { [it.custom_field.column_name, it.typed_value] }end++ def calculated_value_fields_to_compute(custom_fields:, enabled_ids:)+ custom_fields+ .select { it.id.in?(enabled_ids) }+ .to_h { [it.column_name, it.formula_str_without_patterns] }+ endendenddiff --git a/app/models/projects/custom_fields.rb b/app/models/projects/custom_fields.rbindex 126b020d02c..e1f94e05a10 100644--- a/app/models/projects/custom_fields.rb+++ b/app/models/projects/custom_fields.rb@@ -42,6 +42,8 @@ module Projects::CustomFieldshas_many :project_custom_fields, through: :project_custom_field_project_mappings,class_name: "ProjectCustomField"+ def enabled_custom_field_ids = project_custom_field_ids+def available_custom_fieldsreturn all_visible_custom_fields if new_record?diff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rbindex cf8a7cb5c31..281971c06e0 100644--- a/spec/models/acts_as_customizable/calculated_value_spec.rb+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb@@ -37,6 +37,8 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuClass.new doinclude ActsAsCustomizable::CalculatedValue+ def enabled_custom_field_ids = nil+def custom_field_values(*) = nilattr_writer :custom_field_valuesend@@ -45,12 +47,14 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:instance) { model_class.new }before do+ allow(instance).to receive(:enabled_custom_field_ids).and_return(enabled_custom_field_ids)allow(instance).to receive(:custom_field_values).with(all: true).and_return(custom_field_values)allow(instance).to receive(:custom_field_values=)enddescribe "#calculate_custom_fields" docontext "when calling with empty array" do+ let(:enabled_custom_field_ids) { [42] }let(:custom_field_values) { [:foo] }it "doesn't calculate anything" do@@ -61,17 +65,19 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuendcontext "when calling with non calculated value custom fields" do+ let(:static_custom_field) { build_stubbed(:integer_project_custom_field) }+ let(:enabled_custom_field_ids) { [static_custom_field.id] }let(:custom_field_values) { [:foo] }it "raises an exception when" doexpect do- instance.calculate_custom_fields([build(:integer_project_custom_field)])+ instance.calculate_custom_fields([static_custom_field])end.to raise_error(ArgumentError, "Expected array of calculated value custom fields")endit "doesn't calculate anything" dobegin- instance.calculate_custom_fields([build(:integer_project_custom_field)])+ instance.calculate_custom_fields([static_custom_field])rescue ArgumentError# ignoreend@@ -81,7 +87,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuenddescribe "operations" do- let(:cfs) do+ let(:by_op) do{add: build_stubbed(:calculated_value_project_custom_field, formula: "1 + 2"),sub: build_stubbed(:calculated_value_project_custom_field, formula: "2 - 3"),@@ -92,21 +98,24 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valugroup: build_stubbed(:calculated_value_project_custom_field, formula: "2 * (1 + 2)")}end+ let(:custom_fields) { by_op.values }+ let(:ids) { by_op.transform_values(&:id) }+ let(:enabled_custom_field_ids) { custom_fields.map(&:id) }let(:custom_field_values) { [] }it "handles all available operations" do- instance.calculate_custom_fields(cfs.values)+ instance.calculate_custom_fields(custom_fields)expect(instance).to have_received(:custom_field_values=).with(- cfs[:add].id => 3,- cfs[:sub].id => -1,- cfs[:mul].id => 12,- cfs[:div].id => 5/4r,- cfs[:mod].id => 1,- cfs[:percent].id => 607/100r,- cfs[:group].id => 6+ ids[:add] => 3,+ ids[:sub] => -1,+ ids[:mul] => 12,+ ids[:div] => 5/4r,+ ids[:mod] => 1,+ ids[:percent] => 607/100r,+ ids[:group] => 6)endend@@ -116,6 +125,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:cf_mod) { build_stubbed(:calculated_value_project_custom_field, formula: "5 % 0") }let(:cf_add) { build_stubbed(:calculated_value_project_custom_field, formula: "1 + 2") }+ let(:enabled_custom_field_ids) { [cf_div, cf_mod, cf_add].map(&:id) }let(:custom_field_values) { [] }it "blanks field with division by zero, but calculates other field" do@@ -146,6 +156,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:cf1) { build_stubbed(:calculated_value_project_custom_field, formula: "#{cf_a} + #{cf_b}") }let(:cf2) { build_stubbed(:calculated_value_project_custom_field, formula: "#{cf_a} * #{cf_b}") }+ let(:enabled_custom_field_ids) { [cf_a, cf_b, cf1, cf2].map(&:id) }let(:custom_field_values) do{cf_a => 2,@@ -170,6 +181,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:cf2) { build_stubbed(:calculated_value_project_custom_field, formula: "1 + 2") }let(:cf3) { build_stubbed(:calculated_value_project_custom_field, formula: "#{cf1} * #{cf2}") }+ let(:enabled_custom_field_ids) { [cf1, cf2, cf3].map(&:id) }let(:custom_field_values) do{cf1 => 5,@@ -210,6 +222,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:cf_using_unavailable) { build_stubbed(:calculated_value_project_custom_field, formula: "2 + #{cf_unavailable}") }let(:cf_other) { build_stubbed(:calculated_value_project_custom_field, formula: "1 + 2") }+ let(:enabled_custom_field_ids) { [cf_missing, cf_unavailable, cf_using_missing, cf_using_unavailable, cf_other].map(&:id) }let(:custom_field_values) do{cf_missing => nil@@ -226,6 +239,39 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuendend+ context "with disabled fields" do+ let(:cf_a) { build_stubbed(:integer_project_custom_field) }+ let(:cf_b) { build_stubbed(:integer_project_custom_field) }++ let(:cf1) { build_stubbed(:calculated_value_project_custom_field, formula: "#{cf_a} * 5") }+ let(:cf2) { build_stubbed(:calculated_value_project_custom_field, formula: "#{cf_b} * 7") }++ let(:custom_field_values) do+ {+ cf_a => 2,+ cf_b => 3+ }.map { |custom_field, value| build_stubbed(:custom_value, custom_field:, value:) }+ end++ context "when a referenced field is disabled" do+ let(:enabled_custom_field_ids) { [cf_b, cf1, cf2].map(&:id) }++ it "considers its value to be blank" do+ instance.calculate_custom_fields([cf1, cf2])+ expect(instance).to have_received(:custom_field_values=).with(cf1.id => nil, cf2.id => 3 * 7).once+ end+ end++ context "when a calculated field is disabled" do+ let(:enabled_custom_field_ids) { [cf_a, cf_b, cf1].map(&:id) }++ it "blanks its value" do+ instance.calculate_custom_fields([cf1, cf2])+ expect(instance).to have_received(:custom_field_values=).with(cf1.id => 2 * 5, cf2.id => nil).once+ end+ end+ end+context "when calling with custom fields having circular reference" dolet(:cf_a) { build_stubbed(:integer_project_custom_field) }let(:cf_b) { build_stubbed(:integer_project_custom_field) }@@ -237,6 +283,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valulet(:cf3) { build_stubbed(:calculated_value_project_custom_field) }let(:cf4) { build_stubbed(:calculated_value_project_custom_field) }+ let(:enabled_custom_field_ids) { [cf_a, cf_b, cf_c, cf_d, cf1, cf2, cf3, cf4].map(&:id) }let(:custom_field_values) do{cf_a => 2,