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 calculation error handling

Tobias Dillmann410fe6651249
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
index e58dcd30a3a..c3cea1e893d 100644
--- a/app/models/acts_as_customizable/calculated_value.rb
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -42,10 +42,7 @@ module ActsAsCustomizable::CalculatedValue
def calculate_custom_fields(custom_fields)
return if custom_fields.empty?
- unless custom_fields.all?(&:field_format_calculated_value?)
- fail ArgumentError,
- "Expected array of calculated value custom fields"
- end
+ validate_custom_fields_for_calculation!(custom_fields)
enabled_ids = enabled_custom_field_ids
given = calculated_value_fields_given(custom_fields:, enabled_ids:)
@@ -59,11 +56,17 @@ module ActsAsCustomizable::CalculatedValue
self.custom_field_values = custom_fields.to_h { [it.id, result[it.column_name]] }
- refresh_calculation_errors!(given, enabled_ids, custom_fields, result, errors)
+ handle_calculation_errors!(given, enabled_ids, custom_fields, result, errors)
end
private
+ def validate_custom_fields_for_calculation!(custom_fields)
+ unless custom_fields.all?(&:field_format_calculated_value?)
+ fail ArgumentError, "Expected array of calculated value custom fields"
+ end
+ end
+
def calculate_custom_fields_result(given:, to_compute:)
calculator = CustomField::CalculatedValue.calculator_instance
calculator.store(given)
@@ -108,55 +111,17 @@ module ActsAsCustomizable::CalculatedValue
.to_h { [it.column_name, it.formula_str_without_patterns] }
end
- def to_id(cf_id)
- cf_id.sub("cf_", "").to_i
- end
-
- def refresh_calculation_errors!(given_cfs, enabled_ids, calculated_fields, result, errors)
- return unless is_a?(Project)
+ def handle_calculation_errors!(given_cfs, enabled_ids, calculated_fields, result, errors)
+ return unless is_a?(Project) && errors.any?
enabled_calculated_fields = calculated_fields.filter { it.id.in?(enabled_ids) }
- create_new_calculated_value_errors!(enabled_calculated_fields, given_cfs, result, errors)
- end
-
- def create_new_calculated_value_errors!(enabled_calculated_fields, given_cfs, result, errors)
- errors.each do |cf_id, error|
- cf_id = to_id(cf_id)
-
- case error
- when :zero_division
- create_calculated_value_error(cf_id, "ERROR_MATHEMATICAL")
- when :missing_value
- create_errors_for_missing_attributes(given_cfs, enabled_calculated_fields, result, errors)
- when Array
- if error.first == :unbound_variable
- disabled_fields = error.last.map { to_id(it) }
- create_calculated_value_error(cf_id, "ERROR_DISABLED_VALUE", disabled_fields)
- else
- create_calculated_value_error(cf_id, "ERROR_UNKNOWN")
- end
- else
- create_calculated_value_error(cf_id, "ERROR_UNKNOWN")
- end
- end
- end
-
- def create_calculated_value_error(custom_field_id, error_code, missing_custom_field_ids = [])
- CalculatedValueError.create(customized: self, custom_field_id:, error_code:, missing_custom_field_ids:)
- end
-
- def create_errors_for_missing_attributes(referenced_values, calculated_fields, result, errors)
- cf_ids_with_missing_values = referenced_values.filter_map { |k, v| to_id(k) if v.nil? }
-
- missing_values = errors.filter_map { |k, v| to_id(k) if v == :missing_value }
- calculated_fields_with_missing_values = calculated_fields.filter do |cv|
- missing_values.include?(cv.id)
- end
- calculated_fields_with_missing_values.each do |cv|
- handle_missing_value_error(cv, cf_ids_with_missing_values) ||
- handle_indirectly_missing_value_error(cv, calculated_fields, result)
- end
+ CalculatedValues::ErrorHandler.handle_calculation_errors(
+ self,
+ { values: result, errors: errors },
+ given_cfs,
+ enabled_calculated_fields
+ )
end
def remove_calculated_value_errors!(custom_field_ids = [])
@@ -164,31 +129,5 @@ module ActsAsCustomizable::CalculatedValue
CalculatedValueError.where(customized: 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, result)
- cf_ids_with_results = result.filter_map { |cf_id, v| to_id(cf_id) unless v.nil? }
- cf_ids_in_formula_without_result = calculated_value.formula_referenced_custom_field_ids - cf_ids_with_results
-
- indirectly_missing = cf_ids_in_formula_without_result.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
diff --git a/app/models/calculated_values/error_handler.rb b/app/models/calculated_values/error_handler.rb
new file mode 100644
index 00000000000..b0a28d6f85a
--- /dev/null
+++ b/app/models/calculated_values/error_handler.rb
@@ -0,0 +1,146 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module CalculatedValues
+ class ErrorHandler
+ class ErrorContext
+ attr_reader :custom_field_id, :error_code, :missing_custom_field_ids
+
+ def initialize(custom_field_id:, error_code:, missing_custom_field_ids: [])
+ @custom_field_id = custom_field_id
+ @error_code = error_code
+ @missing_custom_field_ids = missing_custom_field_ids
+ end
+ end
+
+ def self.handle_calculation_errors(customized, calculation_result, given_values, calculated_fields)
+ new(customized, calculation_result, given_values, calculated_fields).handle_errors
+ end
+
+ def initialize(customized, calculation_result, given_values, calculated_fields)
+ @customized = customized
+ @calculation_result = calculation_result
+ @given_values = given_values
+ @calculated_fields = calculated_fields
+ end
+
+ def handle_errors
+ return unless should_create_errors?
+
+ error_contexts = build_error_contexts
+ create_error_records!(error_contexts)
+ end
+
+ private
+
+ attr_reader :customized, :calculation_result, :given_values, :calculated_fields
+
+ def should_create_errors?
+ customized.is_a?(Project)
+ end
+
+ def build_error_contexts
+ calculation_result[:errors].filter_map do |cf_id, error|
+ build_error_context(cf_id, error)
+ end
+ end
+
+ def build_error_context(cf_id, error)
+ custom_field_id = to_id(cf_id)
+
+ case error
+ when :zero_division
+ ErrorContext.new(custom_field_id:, error_code: "ERROR_MATHEMATICAL")
+ when :missing_value
+ build_missing_value_error_context(custom_field_id)
+ when Array
+ if error.first == :unbound_variable
+ disabled_fields = error.last.map { to_id(it) }
+ ErrorContext.new(custom_field_id:, error_code: "ERROR_DISABLED_VALUE", missing_custom_field_ids: disabled_fields)
+ else
+ ErrorContext.new(custom_field_id:, error_code: "ERROR_UNKNOWN")
+ end
+ else
+ ErrorContext.new(custom_field_id:, error_code: "ERROR_UNKNOWN")
+ end
+ end
+
+ def build_missing_value_error_context(custom_field_id)
+ missing_values = find_missing_values_for_field(custom_field_id)
+ ErrorContext.new(custom_field_id:, error_code: "ERROR_MISSING_VALUE", missing_custom_field_ids: missing_values)
+ end
+
+ def find_missing_values_for_field(custom_field_id)
+ calculated_field = calculated_fields.find { it.id == custom_field_id }
+ return [] unless calculated_field
+
+ direct_missing = calculated_field.formula_referenced_custom_field_ids & cf_ids_with_missing_values
+ indirect_missing = find_indirect_missing_values(calculated_field)
+
+ direct_missing + indirect_missing
+ end
+
+ def find_indirect_missing_values(calculated_field)
+ cf_ids_with_results = calculation_result[:values].filter_map { |cf_id, v| to_id(cf_id) unless v.nil? }
+ cf_ids_in_formula_without_result = calculated_field.formula_referenced_custom_field_ids - cf_ids_with_results
+
+ cf_ids_in_formula_without_result.filter do |ref_id|
+ calculated_fields.any? { it.id == ref_id }
+ end
+ end
+
+ def cf_ids_with_missing_values
+ @cf_ids_with_missing_values ||= given_values.filter_map { |k, v| to_id(k) if v.nil? }
+ end
+
+ def create_error_records!(error_contexts)
+ error_contexts.each do |context|
+ create_calculated_value_error!(
+ context.custom_field_id,
+ context.error_code,
+ context.missing_custom_field_ids
+ )
+ end
+ end
+
+ def create_calculated_value_error!(custom_field_id, error_code, missing_custom_field_ids = [])
+ CalculatedValueError.create(
+ customized:,
+ custom_field_id:,
+ error_code:,
+ missing_custom_field_ids:
+ )
+ end
+
+ def to_id(cf_id)
+ cf_id.sub("cf_", "").to_i
+ end
+ end
+end