45 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
class ApplicationController < ActionController::Base
 | 
						|
  include Authentication
 | 
						|
  include Pundit::Authorization
 | 
						|
 | 
						|
  before_action :set_title, :set_current_user, :set_lang
 | 
						|
  after_action :verify_authorized
 | 
						|
 | 
						|
  # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
 | 
						|
  allow_browser versions: :modern
 | 
						|
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
 | 
						|
  layout "authenticated"
 | 
						|
 | 
						|
  private
 | 
						|
 | 
						|
  def set_title
 | 
						|
    t_action_name = action_name
 | 
						|
    t_action_name = "new" if action_name == "create"
 | 
						|
    t_action_name = "edit" if action_name == "update"
 | 
						|
    @title = I18n.t("#{controller_name}.#{t_action_name}.title")
 | 
						|
  end
 | 
						|
 | 
						|
  def set_current_user
 | 
						|
    @current_user = current_user
 | 
						|
  end
 | 
						|
 | 
						|
  def set_lang
 | 
						|
    I18n.locale = @current_user.lang if @current_user
 | 
						|
  end
 | 
						|
 | 
						|
  def user_not_authorized(exception)
 | 
						|
    policy_name = exception.policy.class.to_s.underscore
 | 
						|
 | 
						|
    if current_user
 | 
						|
      flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
 | 
						|
      redirect_back_or_to(root_path)
 | 
						|
    else
 | 
						|
      not_found
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def not_found
 | 
						|
    render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found
 | 
						|
  end
 | 
						|
end
 |