Sha256: 01508f4d1b23690758f2424e32b379da5b224aa8b759958441ee46bba6b9f31e

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

require 'pundit'

module Trax
  module Controller
    module Authorization
      module Pundit
        module Adapter
          extend ::ActiveSupport::Concern

          include ::Pundit

          included do
            rescue_from ::Pundit::NotAuthorizedError, :with => :render_pundit_errors
            class_attribute :_policy_class
          end

          def render_pundit_errors
            render_errors(:forbidden, error_messages_hash: { :not_authorized => 'You are not authorized to perform this action' } )
          end

          def policy(record)
            self.class._policy_class.new(current_user, record)
          end

          #not included into controller
          def self.authorization_action_for(action_name)
            "::Trax::Controller::Authorization::Pundit::Actions::#{action_name.to_s.camelize}".safe_constantize
          end

          module ClassMethods
            def policy_class(klass)
              self._policy_class = klass
            end

            def policy!(user, record)
              _policy_class.new(user, record)
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
trax_controller-1.0.0 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.1.4 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.1.3 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.1.2 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.1.1 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.1.0 lib/trax/controller/authorization/pundit/adapter.rb
trax_controller-0.0.4 lib/trax/controller/authorization/pundit/adapter.rb