Skip to content

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

recalculate values on formula change

Ivan Kuchin4ab159db1869
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index 0aa398c7775..69d82d15622 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -50,8 +50,9 @@ module CustomField::CalculatedValue
def affected_calculated_fields(changed_cf_ids)
return [] if changed_cf_ids.empty?
- # exclude ids that are not in the list
+ # exclude ids that are not in the scope
changed_cf_ids = where(id: changed_cf_ids).pluck(:id)
+ return [] if changed_cf_ids.empty?
to_check = field_format_calculated_value
diff --git a/app/services/custom_fields/update_service.rb b/app/services/custom_fields/update_service.rb
index f4a195a81ec..504ec0339b4 100644
--- a/app/services/custom_fields/update_service.rb
+++ b/app/services/custom_fields/update_service.rb
@@ -30,5 +30,30 @@
module CustomFields
class UpdateService < ::BaseServices::Update
+ protected
+
+ def after_perform(service_call)
+ super.tap do
+ recalculate_values
+ end
+ end
+
+ private
+
+ def recalculate_values
+ return unless recalculate_values?
+
+ model.class.customized_class.find_each do |customized|
+ affected_cfs = customized.available_custom_fields.affected_calculated_fields([model.id])
+
+ customized.calculate_custom_fields(affected_cfs)
+
+ customized.save if customized.changed_for_autosave?
+ end
+ end
+
+ def recalculate_values?
+ model.field_format_calculated_value? && model.formula_previously_changed?
+ end
end
end
diff --git a/spec/services/custom_fields/update_service_spec.rb b/spec/services/custom_fields/update_service_spec.rb
index 266dda45917..01317b9a6d6 100644
--- a/spec/services/custom_fields/update_service_spec.rb
+++ b/spec/services/custom_fields/update_service_spec.rb
@@ -33,4 +33,114 @@ require "services/base_services/behaves_like_update_service"
RSpec.describe CustomFields::UpdateService, type: :model do
it_behaves_like "BaseServices update service"
+
+ describe "#call" do
+ let(:user) { build_stubbed(:admin) }
+ let(:contract_class) { CustomFields::UpdateContract }
+ let(:contract_instance) { instance_double(CustomFields::UpdateContract, validate: true) }
+
+ let(:instance) do
+ described_class.new(user:,
+ model: custom_field,
+ contract_class:)
+ end
+
+ subject(:instance_call) { instance.call(attributes) }
+
+ before do
+ User.current = user
+ allow(contract_class).to receive(:new).with(custom_field, user, options: {}).and_return(contract_instance)
+ end
+
+ describe "calculated value custom field", with_flag: { calculated_value_project_attribute: true } do
+ shared_let(:project1) { create(:project) }
+ shared_let(:project2) { create(:project) }
+ shared_let(:project3) { create(:project) }
+ shared_let(:project4) { create(:project) }
+ shared_let(:projects) { [project1, project2, project3, project4] }
+
+ before do
+ find_each = allow(Project).to receive(:find_each)
+
+ projects.each do |project|
+ find_each.and_yield project
+ end
+ end
+
+ context "when updating not a calculated value" do
+ let!(:custom_field) { create(:integer_project_custom_field) }
+ let(:attributes) { { name: "foo" } }
+
+ it "doesn't try to update calculated values" do
+ expect(subject).to be_success
+
+ expect(Project).not_to have_received(:find_each)
+ end
+ end
+
+ context "when not updating formula of calculated value" do
+ let!(:custom_field) { create(:calculated_value_project_custom_field) }
+ let(:attributes) { { name: "foo" } }
+
+ it "doesn't try to update calculated values" do
+ expect(subject).to be_success
+ expect(subject.result).to have_attributes(name: "foo")
+
+ expect(Project).not_to have_received(:find_each)
+ end
+ end
+
+ context "when updating formula of calculated value" do
+ let!(:static) { create(:integer_project_custom_field, projects:) }
+ let!(:custom_field) do
+ create(:calculated_value_project_custom_field, :skip_validations,
+ projects: [project1, project2],
+ formula: "1 + 1")
+ end
+ let!(:custom_field2) do
+ create(:calculated_value_project_custom_field, :skip_validations,
+ projects: [project1, project3],
+ formula: "{{cf_#{custom_field.id}}} * 10.5")
+ end
+ let(:attributes) { { formula: "{{cf_#{static.id}}} * 2" } }
+
+ before do
+ projects.each.with_index(1) do |project, i|
+ create(:custom_value, customized: project, custom_field: static, value: i)
+ create(:custom_value, customized: project, custom_field: custom_field, value: 0)
+ create(:custom_value, customized: project, custom_field: custom_field2, value: 0)
+ end
+ end
+
+ it "updates calculated values on all objects that have the field enabled" do
+ expect(subject).to be_success
+
+ aggregate_failures do
+ expect(project1.custom_value_attributes(all: true))
+ .to include(custom_field.id => "2", custom_field2.id => "21.0")
+ expect(project2.custom_value_attributes(all: true))
+ .to include(custom_field.id => "4", custom_field2.id => "0")
+ expect(project3.custom_value_attributes(all: true))
+ .to include(custom_field.id => "0", custom_field2.id => "0")
+ expect(project4.custom_value_attributes(all: true))
+ .to include(custom_field.id => "0", custom_field2.id => "0")
+ end
+ end
+
+ it "saves the objects when there are changes" do
+ allow(project1).to receive(:save)
+ allow(project2).to receive(:save)
+ allow(project3).to receive(:save)
+ allow(project4).to receive(:save)
+
+ expect(subject).to be_success
+
+ expect(project1).to have_received(:save)
+ expect(project2).to have_received(:save)
+ expect(project3).not_to have_received(:save)
+ expect(project4).not_to have_received(:save)
+ end
+ end
+ end
+ end
end