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 CustomField.with_formula_referencing(id) scope
Ivan Kuchin32741851d2e3
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 69d82d15622..50ee3691c5f 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -44,6 +44,10 @@ module CustomField::CalculatedValueendclass_methods do+ def with_formula_referencing(id)+ where("(formula -> 'referenced_custom_fields') @> ?", id)+ end+# 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 fieldsdiff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rbindex ece7ff147f9..e9d73806944 100644--- a/spec/models/custom_field/calculated_value_spec.rb+++ b/spec/models/custom_field/calculated_value_spec.rb@@ -33,10 +33,37 @@ 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", :aggregate_failures do+ def ref(custom_field) = "{{cf_#{custom_field.id}}}"++ describe ".with_formula_referencing", :aggregate_failures 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(: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 cf_b} + #{ref cf1}") }+let(:scope) { CustomField }- def ref(custom_field) = "{{cf_#{custom_field.id}}}"+ it "finds all fields referencing given id" do+ expect(scope.with_formula_referencing(cf_a.id)).to contain_exactly(cf1)+ expect(scope.with_formula_referencing(cf_b.id)).to contain_exactly(cf1, cf2)+ expect(scope.with_formula_referencing(cf_c.id)).to be_empty+ expect(scope.with_formula_referencing(cf1.id)).to contain_exactly(cf2)+ expect(scope.with_formula_referencing(cf2.id)).to be_empty+ end++ it "finds all fields referencing given custom field" do+ expect(scope.with_formula_referencing(cf_a)).to contain_exactly(cf1)+ expect(scope.with_formula_referencing(cf_b)).to contain_exactly(cf1, cf2)+ expect(scope.with_formula_referencing(cf_c)).to be_empty+ expect(scope.with_formula_referencing(cf1)).to contain_exactly(cf2)+ expect(scope.with_formula_referencing(cf2)).to be_empty+ end+ end++ describe ".affected_calculated_fields", :aggregate_failures do+ let(:scope) { CustomField }context "given simple formulas" doshared_let(:cf_a) { create(:integer_project_custom_field, default_value: 1) }