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] Error when calculating with a disabled CF

Tobias Dillmannf560aba10a1a
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
index e7de4ec4800..431bc4cf0c6 100644
--- a/app/models/acts_as_customizable/calculated_value.rb
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -49,13 +49,14 @@ module ActsAsCustomizable::CalculatedValue
enabled_ids = enabled_custom_field_ids
given = calculated_value_fields_given(custom_fields:, enabled_ids:)
+
result = calculate_custom_fields_result(
given:,
to_compute: calculated_value_fields_to_compute(custom_fields:, enabled_ids:)
)
self.custom_field_values = custom_fields.to_h { [it.id, result[it.column_name]] }
- refresh_calculation_errors!(given, custom_fields, result)
+ refresh_calculation_errors!(given, enabled_ids, custom_fields, result)
end
private
@@ -86,7 +87,7 @@ module ActsAsCustomizable::CalculatedValue
cf_id.sub("cf_", "").to_i
end
- def refresh_calculation_errors!(referenced_values, calculated_fields, result)
+ def refresh_calculation_errors!(given_cfs, enabled_ids, calculated_fields, result)
return unless is_a?(Project)
remove_calculated_value_errors(calculated_fields.map(&:id))
@@ -97,8 +98,16 @@ module ActsAsCustomizable::CalculatedValue
unsuccessfully_calculated_cfs = failed_calculations.map { |cf_id, _| to_id(cf_id) }
+ cvs_with_errors = []
if unsuccessfully_calculated_cfs.any?
- cvs_with_errors = create_errors_for_missing_attributes(referenced_values, calculated_fields)
+ # There are two reasons for unsuccessful calculations here.
+ # 1. The value of a referenced custom field is missing (nil)
+ cvs_with_errors.concat(create_errors_for_missing_attributes(given_cfs, calculated_fields))
+
+ # 2. A referenced custom field disabled (not present in the enabled_ids list)
+ unsuccessful_cvs = calculated_fields.filter { unsuccessfully_calculated_cfs.include?(it.id) }
+ disabled_errors = create_errors_for_disabled_attributes(unsuccessful_cvs, enabled_ids)
+ cvs_with_errors.concat(disabled_errors)
end
unsuccessfully_calculated_cfs.each do |cf_id|
@@ -126,7 +135,7 @@ module ActsAsCustomizable::CalculatedValue
errors_created << cv.id
end
else
- indirectly_missing = cv.formula_referenced_custom_field_ids.select do |ref_id|
+ indirectly_missing = cv.formula_referenced_custom_field_ids.filter do |ref_id|
calculated_fields.any? { it.id == ref_id }
end
@@ -141,6 +150,19 @@ module ActsAsCustomizable::CalculatedValue
errors_created
end
+ def create_errors_for_disabled_attributes(calculated_fields, enabled_ids)
+ errors_created = []
+
+ calculated_fields.each do |cv|
+ disabled_ids = cv.formula_referenced_custom_field_ids - enabled_ids
+ if disabled_ids.any? && create_calculated_value_error(cv.id, "ERROR_DISABLED_VALUE", disabled_ids)
+ errors_created << cv.id
+ end
+ end
+
+ errors_created
+ end
+
def remove_calculated_value_errors(custom_field_ids = [])
return if custom_field_ids.empty?
diff --git a/app/models/calculated_value_error.rb b/app/models/calculated_value_error.rb
index 5e6765d0502..381e8ad0166 100644
--- a/app/models/calculated_value_error.rb
+++ b/app/models/calculated_value_error.rb
@@ -34,7 +34,8 @@ class CalculatedValueError < ApplicationRecord
ERROR_TRANSLATIONS = {
"ERROR_MATHEMATICAL" => "calculated_values.errors.mathematical",
- "ERROR_MISSING_VALUE" => "calculated_values.errors.missing_value"
+ "ERROR_MISSING_VALUE" => "calculated_values.errors.missing_value",
+ "ERROR_DISABLED_VALUE" => "calculated_values.errors.disabled_value"
}.freeze
VALID_ERROR_CODES = ERROR_TRANSLATIONS.keys.freeze
@@ -51,8 +52,8 @@ class CalculatedValueError < ApplicationRecord
translation_key = ERROR_TRANSLATIONS.fetch(error_code, "calculated_values.errors.unknown")
translation_options = {}
- if error_code == "ERROR_MISSING_VALUE"
- # To keep the error message short, we only show the first custom field with a missing value.
+ if %w[ERROR_MISSING_VALUE ERROR_DISABLED_VALUE].include?(error_code)
+ # To keep the error message short, we only show the first custom field with a missing/disabled value.
cf = CustomField.find(missing_custom_field_ids.first)
if cf
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 640243568b7..b5d16b46fad 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -365,6 +365,7 @@ en:
unknown: "An unknown error occurred during calculation. Please review the formula and try again."
mathematical: "A mathematical error occurred during calculation. Please review the formula and try again."
missing_value: "The attribute %{custom_field_name} is not set. Please set all attributes required for the calculation."
+ disabled_value: "The attribute %{custom_field_name} is disabled. Please enable all attributes required for the calculation."
concatenation:
single: "or"