Refactor locale with around method
This commit is contained in:
@ -8,7 +8,8 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
after_action :verify_authorized, except: %i[welcome]
|
||||
around_action :mnemosyne_trace
|
||||
before_action :set_sentry_context, :set_locale, :allow_iframe_requests, :load_embed_options
|
||||
around_action :switch_locale
|
||||
before_action :set_sentry_context, :allow_iframe_requests, :load_embed_options
|
||||
protect_from_forgery(with: :exception, prepend: true)
|
||||
rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized
|
||||
rescue_from ActionController::InvalidAuthenticityToken, with: :render_csrf_error
|
||||
@ -71,12 +72,13 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
private :render_error
|
||||
|
||||
def set_locale
|
||||
def switch_locale(&action)
|
||||
session[:locale] = params[:custom_locale] || params[:locale] || session[:locale]
|
||||
I18n.locale = session[:locale] || I18n.default_locale
|
||||
Sentry.set_extras(locale: I18n.locale)
|
||||
locale = session[:locale] || I18n.default_locale
|
||||
I18n.with_locale(locale, &action)
|
||||
Sentry.set_extras(locale: locale)
|
||||
end
|
||||
private :set_locale
|
||||
private :switch_locale
|
||||
|
||||
def welcome
|
||||
# Show root page
|
||||
|
@ -59,6 +59,8 @@ describe ApplicationController do
|
||||
context "with a 'locale' value in the session" do
|
||||
it 'sets this locale' do
|
||||
session[:locale] = locale
|
||||
# The around block first sets the default language and then the language requested
|
||||
expect(I18n).to receive(:locale=).with(I18n.default_locale)
|
||||
expect(I18n).to receive(:locale=).with(locale)
|
||||
get :welcome
|
||||
end
|
||||
@ -67,7 +69,7 @@ describe ApplicationController do
|
||||
context "without a 'locale' value in the session" do
|
||||
it 'sets the default locale' do
|
||||
expect(session[:locale]).to be_blank
|
||||
expect(I18n).to receive(:locale=).with(I18n.default_locale)
|
||||
expect(I18n).to receive(:locale=).with(I18n.default_locale).at_least(:once)
|
||||
get :welcome
|
||||
end
|
||||
end
|
||||
|
@ -85,9 +85,12 @@ describe SessionsController do
|
||||
end
|
||||
|
||||
it 'sets the specified locale' do
|
||||
expect(controller).to receive(:set_locale).and_call_original
|
||||
expect(controller).to receive(:switch_locale).and_call_original
|
||||
i18n = instance_double 'i18n', locale: locale.to_s
|
||||
allow(I18n).to receive(:locale=).with(I18n.default_locale).and_call_original
|
||||
allow(I18n).to receive(:locale=).with(locale.to_s).and_return(i18n)
|
||||
perform_request
|
||||
expect(I18n.locale).to eq(locale)
|
||||
expect(i18n.locale.to_sym).to eq(locale)
|
||||
end
|
||||
|
||||
it 'assigns the exercise' do
|
||||
@ -107,7 +110,8 @@ describe SessionsController do
|
||||
end
|
||||
|
||||
context 'when LTI outcomes are supported' do
|
||||
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer) }
|
||||
# The expected message should be localized in the requested localization
|
||||
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer, locale: locale) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:lti_outcome_service?).and_return(true)
|
||||
@ -118,7 +122,8 @@ describe SessionsController do
|
||||
end
|
||||
|
||||
context 'when LTI outcomes are not supported' do
|
||||
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer) }
|
||||
# The expected message should be localized in the requested localization
|
||||
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer, locale: locale) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:lti_outcome_service?).and_return(false)
|
||||
|
Reference in New Issue
Block a user