Sha256: 760aaad9dbd84718c28cb35d49887d42d6e61c0fa1b209052188309171cd205d

Contents?: true

Size: 823 Bytes

Versions: 15

Compression:

Stored size: 823 Bytes

Contents

module Controller
  module PunditAuthorizationFailureHandlingConcern
    extend ActiveSupport::Concern

    included do
      rescue_from Pundit::NotAuthorizedError, with: :not_authorized
    end

    private

    def not_authorized(exception)
      resource_name   = exception.record.respond_to?(:model_name) ? exception.record.model_name.human : exception.record
      collection_name = exception.record.respond_to?(:model_name) ? exception.record.model_name.human(count: :other) : exception.record
      flash[:error] = I18n.t("pundit.not_authorized.#{action_name}", collection_name: collection_name,resource_name: resource_name)
      respond_to do |format|
        format.html { redirect_to main_app.root_path }
        format.json { render json: { error: 'not authorized' }, status: 403 }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
itsf_backend-1.2.8 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.7 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.6 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.5 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.4 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.2.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.15 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.14 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.13 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.12 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.11 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.10 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.9 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-1.1.8 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb