Advertisement
Guest User

Untitled

a guest
Apr 7th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.73 KB | None | 0 0
  1. class ApplicationController < ActionController::Base
  2.   protect_from_forgery with: :exception
  3.   after_action :store_action
  4.   before_action :authenticate_user!
  5.   include Pagy::Backend
  6.   before_action :nav_css
  7.   before_action :configure_permitted_parameters, if: :devise_controller?
  8.  
  9.   include Pundit
  10.   add_flash_types :success
  11.  
  12.   # Pundit: white-list approach.
  13.   after_action :verify_authorized, except: :index, unless: :skip_pundit?
  14.   after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?
  15.   before_action :set_locale
  16.   # Uncomment when you *really understand* Pundit!
  17.   # rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
  18.   # def user_not_authorized
  19.   #   flash[:alert] = "You are not authorized to perform this action."
  20.   #   redirect_to(root_path)
  21.   # end
  22.  
  23.   def configure_permitted_parameters
  24.     # For additional fields in app/views/devise/registrations/new.html.erb
  25.     devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
  26.  
  27.     # For additional in app/views/devise/registrations/edit.html.erb
  28.     devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :email, :phone, :language ])
  29.   end
  30.  
  31.   def set_locale
  32.     if user_signed_in?
  33.       I18n.locale = current_user.try(:language).to_sym
  34.     else
  35.       locale = params[:locale].present? ? params[:locale] : I18n.default_locale
  36.       I18n.locale = locale
  37.     end
  38.   end
  39.  
  40.   def store_action
  41.     return unless request.get?
  42.     if (request.path != "/users/sign_in" &&
  43.         request.path != "/users/sign_up" &&
  44.         request.path != "/users/password/new" &&
  45.         request.path != "/users/password/edit" &&
  46.         request.path != "/users/confirmation" &&
  47.         request.path != "/users/sign_out" &&
  48.         !request.xhr?) # don't store ajax calls
  49.       store_location_for(:user, request.fullpath)
  50.     end
  51.   end
  52.  
  53.   protected
  54.  
  55.   def after_sign_in_path_for(resource_or_scope)
  56.     p stored_location_for(resource_or_scope)
  57.     raise
  58.     stored_location_for(resource_or_scope) || super
  59.   end
  60.  
  61.   private
  62.  
  63.   def default_url_options
  64.     { locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
  65.   end
  66.  
  67.   def default_url_options
  68.     { host: ENV["DOMAIN"] || "localhost:3000" }
  69.   end
  70.  
  71.   def skip_pundit?
  72.     devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)|(^newsletter$)/
  73.   end
  74.  
  75.  
  76.   def nav_css
  77.     @border_bottom_teal = 'border-teal-500'
  78.     @border_bottom_transparent = 'border-transparent'
  79.  
  80.     case params[:locale]
  81.     when "en" then @i18n_lang = "English"
  82.     when "es" then @i18n_lang = "Español"
  83.     when "fr" then @i18n_lang = "Français"
  84.     when "de" then @i18n_lang = "Deutsch"
  85.     else
  86.       @i18n_lang = "English"
  87.     end
  88.   end
  89.  
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement