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] Refactor missing attribute error creation

Tobias Dillmann27e8ae0d17f2
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
index 895496794ae..4c3f95ae9fb 100644
--- a/app/models/acts_as_customizable/calculated_value.rb
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -124,30 +124,15 @@ module ActsAsCustomizable::CalculatedValue
CalculatedValueError.create(project: self, custom_field_id:, error_code:, missing_custom_field_ids:)
end
- # FIXME: refactor this method to reduce complexity
def create_errors_for_missing_attributes(referenced_values, calculated_fields)
errors_created = []
cf_ids_with_missing_values = referenced_values.filter_map { |k, v| to_id(k) if v.nil? }
calculated_fields.each do |cv|
- missing_values_for_this_cv = cv.formula_referenced_custom_field_ids & cf_ids_with_missing_values
-
- if missing_values_for_this_cv.any?
- # This hits if a directly used field is `nil`
- if create_calculated_value_error(cv.id, "ERROR_MISSING_VALUE", missing_values_for_this_cv)
- errors_created << cv.id
- end
- else
- indirectly_missing = cv.formula_referenced_custom_field_ids.filter do |ref_id|
- calculated_fields.any? { it.id == ref_id }
- end
-
- next if indirectly_missing.empty?
-
- if create_calculated_value_error(cv.id, "ERROR_MISSING_VALUE", indirectly_missing)
- errors_created << cv.id
- end
+ if handle_missing_value_error(cv, cf_ids_with_missing_values) ||
+ handle_indirectly_missing_value_error(cv, calculated_fields)
+ errors_created << cv.id
end
end
@@ -172,5 +157,28 @@ module ActsAsCustomizable::CalculatedValue
CalculatedValueError.where(project: self, custom_field_id: custom_field_ids).delete_all
end
+
+ # Creates an error if a value that is directly required for the calculation is missing.
+ # This is true for example for a formula like `2 + {{cf_12}}` where `cf_12` has no value set.
+ def handle_missing_value_error(calculated_value, cf_ids_with_missing_values)
+ missing_values_for_this_cv = calculated_value.formula_referenced_custom_field_ids & cf_ids_with_missing_values
+
+ if missing_values_for_this_cv.any?
+ create_calculated_value_error(calculated_value.id, "ERROR_MISSING_VALUE", missing_values_for_this_cv)
+ end
+ end
+
+ # Creates an error if a value that is indirectly required for the calculation is missing.
+ # This is true for example for a formula like `2 + {{cf_12}}` where `cf_12` is a calculated value itself
+ # that cannot be calculated because it references a custom field with no value set.
+ def handle_indirectly_missing_value_error(calculated_value, calculated_fields)
+ indirectly_missing = calculated_value.formula_referenced_custom_field_ids.filter do |ref_id|
+ calculated_fields.any? { it.id == ref_id }
+ end
+
+ return false if indirectly_missing.empty?
+
+ create_calculated_value_error(calculated_value.id, "ERROR_MISSING_VALUE", indirectly_missing)
+ end
end
end