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 from project custom field toggle service

Ivan Kuchine7aae4f12828
diff --git a/app/services/project_custom_field_project_mappings/toggle_service.rb b/app/services/project_custom_field_project_mappings/toggle_service.rb
index 4b434d8059e..1aac1c410ce 100644
--- a/app/services/project_custom_field_project_mappings/toggle_service.rb
+++ b/app/services/project_custom_field_project_mappings/toggle_service.rb
@@ -40,6 +40,23 @@ module ProjectCustomFieldProjectMappings
service_result
end
+ def after_perform(service_result)
+ super.tap do
+ recalculate_values(service_result)
+ end
+ end
+
+ def recalculate_values(service_result)
+ mapping = service_result.result
+ project = mapping.project
+
+ affected_cfs = project.all_available_custom_fields.affected_calculated_fields([mapping.custom_field_id])
+
+ project.calculate_custom_fields(affected_cfs)
+
+ project.save if project.changed_for_autosave?
+ end
+
def create_mapping(service_result)
return if service_result.result.persisted?
diff --git a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
index 83a5b502ad0..360cb4c1986 100644
--- a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
+++ b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
@@ -31,55 +31,97 @@
require "spec_helper"
RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
- let!(:project) { create(:project) }
- let!(:project_custom_field_section) { create(:project_custom_field_section, name: "Section with invisible fields") }
-
- let!(:visible_custom_field) do
- create(:project_custom_field,
- name: "Visible field",
- admin_only: false,
- project_custom_field_section:)
- end
+ shared_let(:project) { create(:project) }
+ shared_let(:project_custom_field_section) { create(:project_custom_field_section) }
- let!(:required_custom_field) do
- create(:project_custom_field,
- name: "Visible required field",
- admin_only: false,
- is_required: true,
- project_custom_field_section:)
- end
+ let(:instance) { described_class.new(user:) }
- let!(:invisible_custom_field) do
- create(:project_custom_field,
- name: "Admin only field",
- admin_only: true,
- project_custom_field_section:)
- end
+ describe "permissions" do
+ shared_let(:visible_custom_field) do
+ create(:project_custom_field,
+ name: "Visible field",
+ admin_only: false,
+ project_custom_field_section:)
+ end
- let(:instance) { described_class.new(user:) }
+ shared_let(:required_custom_field) do
+ create(:project_custom_field,
+ name: "Visible required field",
+ admin_only: false,
+ is_required: true,
+ project_custom_field_section:)
+ end
- let(:visible_custom_field_params) { { project_id: project.id, custom_field_id: visible_custom_field.id } }
- let(:required_custom_field_params) { { project_id: project.id, custom_field_id: required_custom_field.id } }
- let(:invisible_custom_field_params) { { project_id: project.id, custom_field_id: invisible_custom_field.id } }
+ shared_let(:invisible_custom_field) do
+ create(:project_custom_field,
+ name: "Admin only field",
+ admin_only: true,
+ project_custom_field_section:)
+ end
+
+ let(:visible_custom_field_params) { { project_id: project.id, custom_field_id: visible_custom_field.id } }
+ let(:required_custom_field_params) { { project_id: project.id, custom_field_id: required_custom_field.id } }
+ let(:invisible_custom_field_params) { { project_id: project.id, custom_field_id: invisible_custom_field.id } }
+
+ context "with admin permissions" do
+ shared_let(:user) { create(:admin) }
- context "with admin permissions" do
- let(:user) { create(:admin) }
+ it "toggles visible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
- it "toggles visible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
- 2.times do
- expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, visible_custom_field
+ )
+ end
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field, visible_custom_field
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
+ end
+
+ it "toggles invisible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
)
+
+ 2.times do
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, invisible_custom_field
+ )
+ end
+
+ 2.times do
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
end
- 2.times do
- expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
+ it "does not toggle required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
required_custom_field
@@ -87,21 +129,72 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
end
end
- it "toggles invisible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ context "with non-admin but sufficient permissions" do
+ shared_let(:user) do
+ create(:user,
+ firstname: "Project",
+ lastname: "Admin",
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ select_project_custom_fields
+ ]
+ })
+ end
+
+ it "toggles visible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, visible_custom_field
+ )
+ end
+
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
+ end
+
+ it "does not toggle invisible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
- 2.times do
- expect(instance.call(**invisible_custom_field_params, value: "1")).to be_success
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field, invisible_custom_field
+ required_custom_field
)
end
- 2.times do
- expect(instance.call(**invisible_custom_field_params, value: "0")).to be_success
+ it "does not toggle required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
required_custom_field
@@ -109,163 +202,188 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
end
end
- it "does not toggle required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ context "with insufficient permissions" do
+ shared_let(:user) do
+ create(:user,
+ firstname: "Project",
+ lastname: "Editor",
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ ]
+ })
+ end
+
+ it "does not toggle visible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_failure
- expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_failure
- expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
- end
- end
+ it "does not toggle invisible, non-required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
- context "with non-admin but sufficient permissions" do
- let(:user) do
- create(:user,
- firstname: "Project",
- lastname: "Admin",
- member_with_permissions: {
- project => %w[
- view_work_packages
- edit_project
- select_project_custom_fields
- ]
- })
- end
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
- it "toggles visible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
- 2.times do
- expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field, visible_custom_field
+ required_custom_field
)
end
- 2.times do
- expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
+ it "does not toggle required fields" do
+ expect(project.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
required_custom_field
)
end
end
+ end
- it "does not toggle invisible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ describe "calculated values", with_flag: { calculated_value_project_attribute: true } do
+ using CustomFieldFormulaReferencing
- expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
+ shared_let(:user) { create(:admin) }
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ shared_let(:static) { create(:integer_project_custom_field, project_custom_field_section:) }
+ shared_let(:calculated) do
+ create(:calculated_value_project_custom_field, :skip_validations, formula: "#{static} * 7", project_custom_field_section:)
+ end
+ shared_let(:enabled) do
+ create(:calculated_value_project_custom_field, :skip_validations, formula: "11", project_custom_field_section:)
+ end
+ shared_let(:disabled) do
+ create(:calculated_value_project_custom_field, :skip_validations, formula: "13", project_custom_field_section:)
+ end
- expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
+ let(:with_calculated) { { static.id => "2", calculated.id => "14", enabled.id => "11", disabled.id => nil } }
+ let(:without_calculated) { { static.id => "2", calculated.id => nil, enabled.id => "11", disabled.id => nil } }
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ before do
+ # this will auto enable custom fields for the project
+ {
+ static => 2,
+ enabled => 11
+ }.each { |custom_field, value| create(:custom_value, custom_field:, value:, customized: project) }
end
- it "does not toggle required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ context "when toggling referenced static field" do
+ let(:custom_field_id) { static.id }
- expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+ context "when referencing calculated field is enabled" do
+ before do
+ project.project_custom_fields = [calculated, enabled]
+ end
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ it "recalculates the value" do
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "1")).to be_success
- expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(calculated, enabled, static)
+ expect(project.custom_value_attributes(all: true)).to eq(with_calculated)
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
- end
- end
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "0")).to be_success
- context "with insufficient permissions" do
- let(:user) do
- create(:user,
- firstname: "Project",
- lastname: "Editor",
- member_with_permissions: {
- project => %w[
- view_work_packages
- edit_project
- ]
- })
- end
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(calculated, enabled)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
+ end
+ end
- it "does not toggle visible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ context "when referencing calculated field is disabled" do
+ before do
+ project.project_custom_fields = [enabled]
+ end
- expect(instance.call(**visible_custom_field_params, value: "1")).to be_failure
+ it "doesn't recalculate the value" do
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "1")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(enabled, static)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
- expect(instance.call(**visible_custom_field_params, value: "0")).to be_failure
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "0")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(enabled)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
+ end
+ end
end
- it "does not toggle invisible, non-required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ context "when toggling calculated field" do
+ let(:custom_field_id) { calculated.id }
- expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
+ context "when referenced static field is enabled" do
+ before do
+ project.project_custom_fields = [static, enabled]
+ end
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ it "recalculates the value" do
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "1")).to be_success
- expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(static, enabled, calculated)
+ expect(project.custom_value_attributes(all: true)).to eq(with_calculated)
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
- end
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "0")).to be_success
- it "does not toggle required fields" do
- expect(project.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(static, enabled)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
+ end
+ end
+
+ context "when referenced static field is disabled" do
+ before do
+ project.project_custom_fields = [enabled]
+ end
- expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+ it "doesn't recalculate the value" do
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "1")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(enabled, calculated)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
- expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
+ expect(instance.call(project_id: project.id, custom_field_id:, value: "0")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- required_custom_field
- )
+ project.reload
+ expect(project.project_custom_fields).to contain_exactly(enabled)
+ expect(project.custom_value_attributes(all: true)).to eq(without_calculated)
+ end
+ end
end
end
end