Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApplicationController < ActionController::Base
- protect_from_forgery with: :exception
- after_action :store_action
- before_action :authenticate_user!
- include Pagy::Backend
- before_action :nav_css
- before_action :configure_permitted_parameters, if: :devise_controller?
- include Pundit
- add_flash_types :success
- # Pundit: white-list approach.
- after_action :verify_authorized, except: :index, unless: :skip_pundit?
- after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?
- before_action :set_locale
- # Uncomment when you *really understand* Pundit!
- # rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
- # def user_not_authorized
- # flash[:alert] = "You are not authorized to perform this action."
- # redirect_to(root_path)
- # end
- def configure_permitted_parameters
- # For additional fields in app/views/devise/registrations/new.html.erb
- devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
- # For additional in app/views/devise/registrations/edit.html.erb
- devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :email, :phone, :language ])
- end
- def set_locale
- if user_signed_in?
- I18n.locale = current_user.try(:language).to_sym
- else
- locale = params[:locale].present? ? params[:locale] : I18n.default_locale
- I18n.locale = locale
- end
- end
- def store_action
- return unless request.get?
- if (request.path != "/users/sign_in" &&
- request.path != "/users/sign_up" &&
- request.path != "/users/password/new" &&
- request.path != "/users/password/edit" &&
- request.path != "/users/confirmation" &&
- request.path != "/users/sign_out" &&
- !request.xhr?) # don't store ajax calls
- store_location_for(:user, request.fullpath)
- end
- end
- protected
- def after_sign_in_path_for(resource_or_scope)
- p stored_location_for(resource_or_scope)
- raise
- stored_location_for(resource_or_scope) || super
- end
- private
- def default_url_options
- { locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
- end
- def default_url_options
- { host: ENV["DOMAIN"] || "localhost:3000" }
- end
- def skip_pundit?
- devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)|(^newsletter$)/
- end
- def nav_css
- @border_bottom_teal = 'border-teal-500'
- @border_bottom_transparent = 'border-transparent'
- case params[:locale]
- when "en" then @i18n_lang = "English"
- when "es" then @i18n_lang = "Español"
- when "fr" then @i18n_lang = "Français"
- when "de" then @i18n_lang = "Deutsch"
- else
- @i18n_lang = "English"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement