From eaa780042775d250adca4417bbf3ef11490d8d65 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Fri, 14 Dec 2018 18:55:23 +0800 Subject: [PATCH] Do not assign I18n translation to constants The preferred language could change dynamically. --- .../reports/enterprise_fee_summary/authorizer.rb | 9 +++++---- .../reports/enterprise_fee_summary/parameters.rb | 12 +++++++----- .../enterprise_fee_summary/parameters_spec.rb | 3 ++- .../enterprise_fee_summaries_controller_spec.rb | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/authorizer.rb b/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/authorizer.rb index 8b091577dd..e129905dc6 100644 --- a/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/authorizer.rb +++ b/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/authorizer.rb @@ -2,9 +2,10 @@ module OrderManagement module Reports module EnterpriseFeeSummary class Authorizer < ::Reports::Authorizer - @i18n_scope = "order_management.reports.enterprise_fee_summary" - - PARAMETER_NOT_ALLOWED_ERROR = I18n.t("parameter_not_allowed_error", scope: @i18n_scope) + def self.parameter_not_allowed_error_message + i18n_scope = "order_management.reports.enterprise_fee_summary" + I18n.t("parameter_not_allowed_error", scope: i18n_scope) + end def authorize! authorize_by_distribution! @@ -27,7 +28,7 @@ module OrderManagement def require_ids_allowed(array, allowed_objects) error_klass = ::Reports::Authorizer::ParameterNotAllowedError - error_message = PARAMETER_NOT_ALLOWED_ERROR + error_message = self.class.parameter_not_allowed_error_message ids_allowed = (array - allowed_objects.map(&:id).map(&:to_s)).blank? raise error_klass, error_message unless ids_allowed diff --git a/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/parameters.rb b/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/parameters.rb index 2082d64c9e..cc12d9be1f 100644 --- a/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/parameters.rb +++ b/engines/order_management/app/services/order_management/reports/enterprise_fee_summary/parameters.rb @@ -2,10 +2,6 @@ module OrderManagement module Reports module EnterpriseFeeSummary class Parameters < ::Reports::Parameters::Base - @i18n_scope = "order_management.reports.enterprise_fee_summary" - - DATE_END_BEFORE_START_ERROR = I18n.t("date_end_before_start_error", scope: @i18n_scope) - extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations @@ -23,6 +19,11 @@ module OrderManagement validate :require_valid_datetime_range + def self.date_end_before_start_error_message + i18n_scope = "order_management.reports.enterprise_fee_summary" + I18n.t("date_end_before_start_error", scope: i18n_scope) + end + def initialize(attributes = {}) self.distributor_ids = [] self.producer_ids = [] @@ -44,7 +45,8 @@ module OrderManagement def require_valid_datetime_range return if start_at.blank? || end_at.blank? - errors.add(:end_at, DATE_END_BEFORE_START_ERROR) unless start_at < end_at + error_message = self.class.date_end_before_start_error_message + errors.add(:end_at, error_message) unless start_at < end_at end # Remove the blank strings that Rails multiple selects add by default to diff --git a/engines/order_management/spec/services/order_management/reports/enterprise_fee_summary/parameters_spec.rb b/engines/order_management/spec/services/order_management/reports/enterprise_fee_summary/parameters_spec.rb index 22e54cfbcb..6fd3c8b9a7 100644 --- a/engines/order_management/spec/services/order_management/reports/enterprise_fee_summary/parameters_spec.rb +++ b/engines/order_management/spec/services/order_management/reports/enterprise_fee_summary/parameters_spec.rb @@ -46,7 +46,8 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::Parameters do allow(subject).to receive(:end_at) { (now - 1.hour).to_s } expect(subject).not_to be_valid - expect(subject.errors[:end_at]).to eq([described_class::DATE_END_BEFORE_START_ERROR]) + error_message = described_class.date_end_before_start_error_message + expect(subject.errors[:end_at]).to eq([error_message]) end it "does not add error when start_at is before end_at" do diff --git a/spec/controllers/spree/admin/reports/enterprise_fee_summaries_controller_spec.rb b/spec/controllers/spree/admin/reports/enterprise_fee_summaries_controller_spec.rb index 8cc159852e..5a5747041b 100644 --- a/spec/controllers/spree/admin/reports/enterprise_fee_summaries_controller_spec.rb +++ b/spec/controllers/spree/admin/reports/enterprise_fee_summaries_controller_spec.rb @@ -49,7 +49,7 @@ describe Spree::Admin::Reports::EnterpriseFeeSummariesController, type: :control it "renders the report form with an error" do post :create, report: { distributor_ids: [other_distributor.id] }, report_format: "csv" - expect(flash[:error]).to eq(report_klass::Authorizer::PARAMETER_NOT_ALLOWED_ERROR) + expect(flash[:error]).to eq(report_klass::Authorizer.parameter_not_allowed_error_message) expect(response).to render_template(new_template_path) end end