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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#66093] Move calculation error translations into helper
Tobias Dillmann653dd5eb33eb
diff --git a/app/components/projects/row_component.rb b/app/components/projects/row_component.rbindex 8c3d66abee1..8b899a69fcc 100644--- a/app/components/projects/row_component.rb+++ b/app/components/projects/row_component.rb@@ -29,6 +29,8 @@#++module Projectsclass RowComponent < ::RowComponent+ include CalculatedValues::ErrorsHelper+delegate :identifier, to: :projectdelegate :favorited_project_ids,:project_phase_by_definition,@@ -110,7 +112,7 @@ module Projects"aria-label": I18n.t("calculated_values.error_dialog.title"),data: { test_selector: "calculated-value-error-btn-#{custom_field.id}" },scheme: :invisible)- dialog.with_body { error.error_message }+ dialog.with_body { calculated_value_error_msg(error) }endelsecustom_valuediff --git a/app/helpers/calculated_values/errors_helper.rb b/app/helpers/calculated_values/errors_helper.rbnew file mode 100644index 00000000000..e9c7d6b3a06--- /dev/null+++ b/app/helpers/calculated_values/errors_helper.rb@@ -0,0 +1,62 @@+# 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::ErrorsHelper+ ERROR_TRANSLATIONS = {+ "ERROR_MATHEMATICAL" => "calculated_values.errors.mathematical",+ "ERROR_MISSING_VALUE" => "calculated_values.errors.missing_value",+ "ERROR_DISABLED_VALUE" => "calculated_values.errors.disabled_value"+ }.freeze++ def self.calculated_value_error_msg(calculated_value_error)+ return unless calculated_value_error.is_a?(CalculatedValueError)++ error_code = calculated_value_error.error_code+ missing_custom_field_ids = calculated_value_error.missing_custom_field_ids++ translation_key = ERROR_TRANSLATIONS.fetch(error_code, "calculated_values.errors.unknown")+ translation_options = {}++ 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+ translation_options[:custom_field_name] = cf.name+ else+ translation_key = "calculated_values.errors.unknown"+ end+ end++ I18n.t(translation_key, **translation_options)+ end++ delegate :calculated_value_error_msg, to: :"CalculatedValues::ErrorsHelper"+enddiff --git a/app/models/calculated_value_error.rb b/app/models/calculated_value_error.rbindex 381e8ad0166..9dc6d6c56c7 100644--- a/app/models/calculated_value_error.rb+++ b/app/models/calculated_value_error.rb@@ -32,13 +32,9 @@ class CalculatedValueError < ApplicationRecordbelongs_to :projectbelongs_to :custom_field- ERROR_TRANSLATIONS = {- "ERROR_MATHEMATICAL" => "calculated_values.errors.mathematical",- "ERROR_MISSING_VALUE" => "calculated_values.errors.missing_value",- "ERROR_DISABLED_VALUE" => "calculated_values.errors.disabled_value"- }.freeze-- VALID_ERROR_CODES = ERROR_TRANSLATIONS.keys.freeze+ VALID_ERROR_CODES = %w[ERROR_MATHEMATICAL+ ERROR_MISSING_VALUE+ ERROR_DISABLED_VALUE].freezevalidates :project_id, presence: truevalidates :custom_field_id, presence: true@@ -47,22 +43,4 @@ class CalculatedValueError < ApplicationRecord# It makes no sense to have the exact same error multiple times.validates :project_id, uniqueness: { scope: %i[custom_field_id error_code] }-- def error_message- translation_key = ERROR_TRANSLATIONS.fetch(error_code, "calculated_values.errors.unknown")- translation_options = {}-- 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- translation_options[:custom_field_name] = cf.name- else- translation_key = "calculated_values.errors.unknown"- end- end-- I18n.t(translation_key, **translation_options)- endenddiff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rbindex cef0be0a94c..ebb71427ffc 100644--- a/lib/api/v3/utilities/custom_field_injector.rb+++ b/lib/api/v3/utilities/custom_field_injector.rb@@ -314,7 +314,8 @@ module APIerrors = custom_field.calculated_value_errors.where(project_id: id)errors.map do |err|- { error_code: err.error_code, error_message: err.error_message }+ { error_code: err.error_code,+ error_message: CalculatedValues::ErrorsHelper.calculated_value_error_msg(err) }end}enddiff --git a/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb b/modules/overviews/app/components/overviews/project_custom_fields/item_component.rbindex b7047bc38f4..508dd068422 100644--- a/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb+++ b/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb@@ -32,6 +32,7 @@ module Overviewsmodule ProjectCustomFieldsclass ItemComponent < ApplicationComponentinclude ApplicationHelper+ include CalculatedValues::ErrorsHelperinclude CustomFieldsHelperinclude OpPrimer::ComponentHelpers@@ -65,7 +66,7 @@ module Overviewsendcontainer.with_column(ml: 2) dorender Primer::Beta::Text.new(color: :danger) do- error&.error_message+ calculated_value_error_msg(error)endendenddiff --git a/spec/helpers/calculated_values/errors_helper_spec.rb b/spec/helpers/calculated_values/errors_helper_spec.rbnew file mode 100644index 00000000000..df153f15378--- /dev/null+++ b/spec/helpers/calculated_values/errors_helper_spec.rb@@ -0,0 +1,103 @@+# 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.+#++++require "spec_helper"++RSpec.describe CalculatedValues::ErrorsHelper do+ let(:error) { build(:calculated_value_error) }++ shared_let(:custom_field) { create(:custom_field) }+ shared_let(:other_custom_field) { create(:custom_field) }++ subject { described_class }++ describe ".calculated_value_error_msg" do+ it "returns nil when not given a CalculatedValueError" do+ expect(subject.calculated_value_error_msg(1)).to be_nil+ end++ it "returns a human readable error message for the error code" do+ error.error_code = "ERROR_MATHEMATICAL"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.mathematical"))+ end++ it "returns a default message for unknown error codes" do+ error.error_code = "UNKNOWN_ERROR_CODE"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.unknown"))+ end++ describe "missing values" do+ it "includes the custom field name for a missing value" do+ error.missing_custom_field_ids = [custom_field.id]++ error.error_code = "ERROR_MISSING_VALUE"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.missing_value",+ custom_field_name: custom_field.name))+ end++ it "when there are multiple missing values, only includes the first custom field name" do+ error.missing_custom_field_ids = [other_custom_field.id, custom_field.id]++ error.error_code = "ERROR_MISSING_VALUE"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.missing_value",+ custom_field_name: other_custom_field.name))+ end+ end++ describe "disabled values" do+ it "includes the custom field name for a disabled value" do+ error.missing_custom_field_ids = [custom_field.id]++ error.error_code = "ERROR_DISABLED_VALUE"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.disabled_value",+ custom_field_name: custom_field.name))+ end++ it "when there are multiple disabled values, only includes the first custom field name" do+ error.missing_custom_field_ids = [other_custom_field.id, custom_field.id]++ error.error_code = "ERROR_DISABLED_VALUE"+ expect(subject.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.disabled_value",+ custom_field_name: other_custom_field.name))+ end+ end+ end++ describe "#calculated_value_error_msg" do+ let(:instance) do+ Object.new.extend(described_class)+ end++ it "delegates to the class method" do+ error.error_code = "ERROR_MATHEMATICAL"+ expect(instance.calculated_value_error_msg(error)).to eq(I18n.t("calculated_values.errors.mathematical"))+ end+ end+enddiff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rbindex 7a5d8a68308..51d5b0ad386 100644--- a/spec/models/acts_as_customizable/calculated_value_spec.rb+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb@@ -32,6 +32,7 @@ require "spec_helper"RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_value_project_attribute: true } dousing CustomFieldFormulaReferencing+ include CalculatedValues::ErrorsHelperdescribe "#calculate_custom_fields" dolet(:model_class) do@@ -388,7 +389,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuexpect(error&.error_code).to eq(error_code)if message_part- expect(error&.error_message).to include(message_part)+ expect(calculated_value_error_msg(error)).to include(message_part)endenddiff --git a/spec/models/calculated_value_error_spec.rb b/spec/models/calculated_value_error_spec.rbindex 949bde51919..594dc8950cc 100644--- a/spec/models/calculated_value_error_spec.rb+++ b/spec/models/calculated_value_error_spec.rb@@ -56,51 +56,4 @@ RSpec.describe CalculatedValueError doendend- describe "#error_message" do- it "returns a human readable error message for the error code" do- subject.error_code = "ERROR_MATHEMATICAL"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.mathematical"))- end-- it "returns a default message for unknown error codes" do- subject.error_code = "UNKNOWN_ERROR_CODE"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.unknown"))- end-- describe "missing values" do- it "includes the custom field name for a missing value" do- subject.missing_custom_field_ids = [custom_field.id]-- subject.error_code = "ERROR_MISSING_VALUE"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.missing_value",- custom_field_name: custom_field.name))- end-- it "when there are multiple missing values, only includes the first custom field name" do- subject.missing_custom_field_ids = [other_custom_field.id, custom_field.id]-- subject.error_code = "ERROR_MISSING_VALUE"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.missing_value",- custom_field_name: other_custom_field.name))- end- end-- describe "disabled values" do- it "includes the custom field name for a disabled value" do- subject.missing_custom_field_ids = [custom_field.id]-- subject.error_code = "ERROR_DISABLED_VALUE"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.disabled_value",- custom_field_name: custom_field.name))- end-- it "when there are multiple disabled values, only includes the first custom field name" do- subject.missing_custom_field_ids = [other_custom_field.id, custom_field.id]-- subject.error_code = "ERROR_DISABLED_VALUE"- expect(subject.error_message).to eq(I18n.t("calculated_values.errors.disabled_value",- custom_field_name: other_custom_field.name))- end- end- endend