This cop scans only the tests files by default, but we prefer to scan all application Ruby files, so when a developer uses the class method `I18n.locale=`, the cop will embrace using the method `I18n.with_locale` instead. By doing this way, the cop will help developers to avoid unexpected translation errors. Quoting the Rails 6 guides: > I18n.locale can leak into subsequent requests served by the same thread/process if it is not consistently set in every controller. For example executing I18n.locale = :es in one POST requests will have effects for all later requests to controllers that don't set the locale, but only in that particular thread/process. For that reason, instead of I18n.locale = you can use I18n.with_locale which does not have this leak issue. Now we enabled the cop for all application Ruby files; we have to remove the assignments at the controller level to set the request locale. As Rails 6 guides suggest [1], we can use the `around_action` controller callback to set each request locale without breaking the rule. This cop will warn CONSUL developers when using `I18n.locale` assignment embracing them to use the `I18n.with_locale`instead. [1] https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests
65 lines
1.5 KiB
Ruby
65 lines
1.5 KiB
Ruby
class Management::BaseController < ActionController::Base
|
|
include GlobalizeFallbacks
|
|
layout "management"
|
|
default_form_builder ConsulFormBuilder
|
|
|
|
before_action :verify_manager
|
|
around_action :switch_locale
|
|
|
|
helper_method :managed_user
|
|
helper_method :current_user
|
|
helper_method :manager_logged_in
|
|
|
|
private
|
|
|
|
def verify_manager
|
|
raise ActionController::RoutingError, "Not Found" if current_manager.blank?
|
|
end
|
|
|
|
def current_manager
|
|
session[:manager]
|
|
end
|
|
|
|
def current_user
|
|
managed_user
|
|
end
|
|
|
|
def managed_user
|
|
@managed_user ||= Verification::Management::ManagedUser.find(
|
|
session[:document_type],
|
|
session[:document_number]
|
|
)
|
|
end
|
|
|
|
def check_verified_user(alert_msg)
|
|
return if managed_user.persisted? && managed_user.level_two_or_three_verified?
|
|
|
|
message = managed_user.persisted? ? alert_msg : t("management.sessions.need_managed_user")
|
|
redirect_to management_document_verifications_path, alert: message
|
|
end
|
|
|
|
def switch_locale(&action)
|
|
if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
|
|
session[:locale] = params[:locale]
|
|
end
|
|
|
|
session[:locale] ||= I18n.default_locale
|
|
|
|
I18n.with_locale(session[:locale], &action)
|
|
end
|
|
|
|
def current_budget
|
|
Budget.current
|
|
end
|
|
|
|
def clear_password
|
|
session[:new_password] = nil
|
|
end
|
|
|
|
def manager_logged_in
|
|
if current_manager
|
|
@manager_logged_in = User.find_by_manager_login(session[:manager]["login"])
|
|
end
|
|
end
|
|
end
|