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

[#66093] Disabled Calculated Values do not generate errors

Tobias Dillmann5b64778f78dd
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
index ed208385500..9881fe29be6 100644
--- a/app/models/acts_as_customizable/calculated_value.rb
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -92,21 +92,23 @@ module ActsAsCustomizable::CalculatedValue
return unless is_a?(Project)
remove_calculated_value_errors(calculated_fields.map(&:id))
- create_new_calculated_value_errors(given_cfs, enabled_ids, calculated_fields, result)
+
+ enabled_calculated_fields = calculated_fields.filter { it.id.in?(enabled_ids) }
+ create_new_calculated_value_errors(enabled_calculated_fields, given_cfs, enabled_ids, result)
end
- def create_new_calculated_value_errors(given_cfs, enabled_ids, calculated_fields, result)
+ def create_new_calculated_value_errors(enabled_calculated_fields, given_cfs, enabled_ids, result)
unsuccessfully_calculated_cfs = result.filter_map do |cf_id, calculation|
to_id(cf_id) if calculation.nil?
end
cvs_with_errors = []
- calculated_fields_without_value = calculated_fields.filter { unsuccessfully_calculated_cfs.include?(it.id) }
+ calculated_fields_without_value = enabled_calculated_fields.filter { unsuccessfully_calculated_cfs.include?(it.id) }
if unsuccessfully_calculated_cfs.any?
# There are multiple reasons why a calculation could not complete:
# 1. The value of a referenced custom field is missing (nil)
- cvs_with_errors.concat(create_errors_for_missing_attributes(given_cfs, calculated_fields))
+ cvs_with_errors.concat(create_errors_for_missing_attributes(given_cfs, enabled_calculated_fields))
# 2. A referenced custom field is disabled (not present in the enabled_ids list)
disabled_errors = create_errors_for_disabled_attributes(calculated_fields_without_value, enabled_ids)
diff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rb
index ce9eb55ed46..7a5d8a68308 100644
--- a/spec/models/acts_as_customizable/calculated_value_spec.rb
+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb
@@ -384,12 +384,11 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
describe "#calculate_custom_fields error handling" do
def expect_calculated_value_error(calc_val_cf, project, error_code, message_part = nil)
- errors = calc_val_cf.calculated_value_errors.where(project:)
- expect(errors.size).to be >= 1
- expect(errors.first&.error_code).to eq(error_code)
+ error = calc_val_cf.first_calculation_error(project:)
+ expect(error&.error_code).to eq(error_code)
if message_part
- expect(errors.first&.error_message).to include(message_part)
+ expect(error&.error_message).to include(message_part)
end
end
@@ -458,7 +457,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "creates a disabled value error" do
project.calculate_custom_fields([cv1, cv2, cv3])
- expect(cv3.calculated_value_errors.where(project:)).to be_empty
+ expect(cv3.first_calculation_error(project:)).to be_blank
# The referenced int field is disabled, we thus expect a `disabled` error.
expect_calculated_value_error(cv1, project, "ERROR_DISABLED_VALUE", cf_int.name)
@@ -467,5 +466,25 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
expect_calculated_value_error(cv2, project, "ERROR_MISSING_VALUE", cv1.name)
end
end
+
+ context "with disabled calculated values" do
+ let(:cf_int) { create(:integer_project_custom_field) }
+ let(:formula) { "2 / #{cf_int}" }
+ let(:disabled_cv) do
+ create(:calculated_value_project_custom_field, :skip_validations, projects: [], formula:)
+ end
+ let(:enabled_cv) { create(:calculated_value_project_custom_field, :skip_validations, projects: [project], formula:) }
+
+ let(:enabled_custom_fields) do
+ { enabled_cv => nil, cf_int => 0 }
+ end
+
+ it "only creates errors for enabled calculated values" do
+ project.calculate_custom_fields([disabled_cv, enabled_cv])
+
+ expect_calculated_value_error(enabled_cv, project, "ERROR_MATHEMATICAL")
+ expect(disabled_cv.first_calculation_error(project:)).to be_blank
+ end
+ end
end
end