module PunditErrorHandling

Handling for any Pundit-thrown errors

Protected Instance Methods

authentication_failed_redirect_path_for(_resource) click to toggle source

redirect path for failed authentication attempts

# File lib/generators/rockstart/authorization/pundit/templates/app/controllers/concerns/pundit_error_handling.rb, line 14
def authentication_failed_redirect_path_for(_resource)
  after_sign_in_path_for(current_user)
end

Private Instance Methods

user_not_authorized(exception) click to toggle source
# File lib/generators/rockstart/authorization/pundit/templates/app/controllers/concerns/pundit_error_handling.rb, line 20
def user_not_authorized(exception)
  policy_name = exception.policy.class.to_s.underscore

  # e.g. I18n.t("pundit.example_policy.show?")
  flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default

  # redirect to the either the previous page or the default sign in page
  redirect_to authentication_failed_redirect_path_for(exception.record)
end