Do not assign I18n translation to constants

The preferred language could change dynamically.
This commit is contained in:
Kristina Lim
2018-12-14 18:55:23 +08:00
committed by luisramos0
parent 6378afdddb
commit eaa7800427
4 changed files with 15 additions and 11 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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