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
add affected_calculated_fields method
Ivan Kuchin9e299ac4f0a0
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 353e90ee3ef..b754f0e7afa 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -43,6 +43,30 @@ module CustomField::CalculatedValueDentaku::Calculator.new(case_sensitive: true)end+ class_methods do+ # Select custom fields of type calculated_value that are listed in+ # changed_cf_ids, or are referencing custom fields in changed_cf_ids either+ # directly or through other calculated fields+ def affected_calculated_fields(changed_cf_ids)+ return [] if changed_cf_ids.empty?++ to_check = select(&:field_format_calculated_value?)++ # include calculated value fields themselves+ all_affected, to_check = to_check.partition { it.id.in?(changed_cf_ids) }++ loop do+ affected, to_check = to_check.partition { it.formula_referenced_custom_field_ids.intersect?(changed_cf_ids) }+ break if affected.empty?++ all_affected += affected+ changed_cf_ids = affected.map(&:id)+ end++ all_affected+ end+ end+included dovalidate :validate_formula, if: :field_format_calculated_value?diff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rbindex c8affe87086..3c41b5ff157 100644--- a/spec/models/custom_field/calculated_value_spec.rb+++ b/spec/models/custom_field/calculated_value_spec.rb@@ -33,6 +33,141 @@ require "spec_helper"RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_project_attribute: true } dosubject(:custom_field) { create(:calculated_value_project_custom_field, formula: "1 + 1") }+ describe ".affected_calculated_fields" do+ let(:scope) { CustomField }++ def ref(custom_field) = "{{cf_#{custom_field.id}}}"++ context "given simple formulas" do+ shared_let(:cf_a) { create(:integer_project_custom_field, default_value: 1) }+ shared_let(:cf_b) { create(:integer_project_custom_field, default_value: 2) }+ shared_let(:cf_c) { create(:integer_project_custom_field, default_value: 3) }+ shared_let(:cf_d) { create(:integer_project_custom_field, default_value: 4) }+ shared_let(:cf1) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf_a} * 2") }+ shared_let(:cf2) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf_a} + #{ref cf_b}") }+ shared_let(:cf3) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf_b} * 2") }+ shared_let(:cf4) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf_c} * 2") }++ it "returns an empty array if argument is empty" do+ expect(scope.affected_calculated_fields([])).to eq([])+ end++ it "returns an empty array when passing non referenced constant field ids" do+ expect(scope.affected_calculated_fields([cf_d.id])).to eq([])+ end++ it "returns an empty array when passing non existing field id" do+ expect(scope.affected_calculated_fields([-1, -2])).to eq([])+ end++ it "returns referencing fields when passing referenced constant field ids" do+ expect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1, cf2)+ expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf_c.id])).to contain_exactly(cf4)+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf_a.id, cf_c.id])).to contain_exactly(cf1, cf2, cf4)+ expect(scope.affected_calculated_fields([cf_b.id, cf_c.id])).to contain_exactly(cf2, cf3, cf4)+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id, cf_c.id])).to contain_exactly(cf1, cf2, cf3, cf4)+ end++ it "returns referencing fields once when passing referenced constant field ids multiple times" do+ expect(scope.affected_calculated_fields([cf_a.id] * 5)).to contain_exactly(cf1, cf2)+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id] * 5)).to contain_exactly(cf1, cf2, cf3)+ end++ it "returns fields themselves when passing calculated field ids" do+ expect(scope.affected_calculated_fields([cf1.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf2.id])).to contain_exactly(cf2)+ expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf3)+ expect(scope.affected_calculated_fields([cf4.id])).to contain_exactly(cf4)+ end++ it "returns fields once when passing mixture of calculated and constant field ids" do+ expect(scope.affected_calculated_fields([cf_a.id, cf1.id])).to contain_exactly(cf1, cf2)+ end++ context "when scope doesn't include some values" do+ let(:scope) { CustomField.where(id: [cf2, cf3]) }++ it "receives referencing fields when passing referenced constant field ids" do+ expect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf2)+ expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf_c.id])).to be_empty+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf_a.id, cf_c.id])).to contain_exactly(cf2)+ expect(scope.affected_calculated_fields([cf_b.id, cf_c.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id, cf_c.id])).to contain_exactly(cf2, cf3)+ end++ it "receives fields themselves when passing calculated field ids" do+ expect(scope.affected_calculated_fields([cf1.id])).to be_empty+ expect(scope.affected_calculated_fields([cf2.id])).to contain_exactly(cf2)+ expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf3)+ expect(scope.affected_calculated_fields([cf4.id])).to be_empty+ end+ end+ end++ context "given formulas referencing other calculated fields" do+ shared_let(:cf_a) { create(:integer_project_custom_field, default_value: 1) }+ shared_let(:cf_b) { create(:integer_project_custom_field, default_value: 2) }+ shared_let(:cf_c) { create(:integer_project_custom_field, default_value: 3) }+ shared_let(:cf_d) { create(:integer_project_custom_field, default_value: 4) }++ shared_let(:cf1) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf_a} + #{ref cf_b}") }+ shared_let(:cf2) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf1} * #{ref cf_c}") }+ shared_let(:cf3) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf2} * #{ref cf_d}") }++ it "receives referencing fields when passing referenced constant field ids" do+ expect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf_c.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf_d.id])).to contain_exactly(cf3)+ end++ context "when scope doesn't include some values" do+ let(:scope) { CustomField.where(id: [cf1, cf3]) }++ it "receives referencing fields when passing referenced constant field ids" do+ expect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf_c.id])).to be_empty+ expect(scope.affected_calculated_fields([cf_d.id])).to contain_exactly(cf3)+ end+ end+ end++ context "if given formulas with circular references" do+ let!(:cf_a) { create(:integer_project_custom_field, default_value: 1) }+ let!(:cf_b) { create(:integer_project_custom_field, default_value: 2) }+ let!(:cf_c) { create(:integer_project_custom_field, default_value: 3) }++ let!(:cf1) { create(:calculated_value_project_custom_field) }+ let!(:cf2) { create(:calculated_value_project_custom_field) }+ let!(:cf3) { create(:calculated_value_project_custom_field) }++ before do+ {+ cf1 => "#{ref cf_a} + #{ref cf2}",+ cf2 => "#{ref cf_b} + #{ref cf3}",+ cf3 => "#{ref cf_c} + #{ref cf1}"+ }.each do |cf, formula|+ cf.formula = formula+ cf.save!(validate: false)+ end+ end++ it "does not enter infinite loop and returns all affected fields" do+ expect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf_c.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf1.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf2.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf1, cf2, cf3)+ end+ end+ end+describe "#usable_custom_field_references_for_formula" dolet!(:int) { create(:project_custom_field, :integer, default_value: 4, is_for_all: true) }let!(:float) { create(:project_custom_field, :float, default_value: 5.5, is_for_all: true) }