Sha256: 66c5d37682b1ca30f0d7cbcf6ae54379decc7a858a53b8e7caa640f44d01efa1

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Administrate
  module Punditize
    if Object.const_defined?("Pundit")
      extend ActiveSupport::Concern

      if Pundit.const_defined?(:Authorization)
        include Pundit::Authorization
      else
        include Pundit
      end

      included do
        private

        def policy_namespace
          []
        end

        def scoped_resource
          namespaced_scope = policy_namespace + [super]
          policy_scope!(pundit_user, namespaced_scope)
        end

        def authorize_resource(resource)
          namespaced_resource = policy_namespace + [resource]
          authorize namespaced_resource
        end

        def authorized_action?(resource, action)
          namespaced_resource = policy_namespace + [resource]
          policy = Pundit.policy!(pundit_user, namespaced_resource)
          policy.send("#{action}?".to_sym)
        end
      end

      private

      def policy_scope!(user, scope)
        policy_scope_class = Pundit::PolicyFinder.new(scope).scope!

        begin
          policy_scope = policy_scope_class.new(user, pundit_model(scope))
        rescue ArgumentError
          raise(Pundit::InvalidConstructorError,
                "Invalid #<#{policy_scope_class}> constructor is called")
        end

        if policy_scope.respond_to? :resolve_admin
          ActiveSupport::Deprecation.warn(
            "Pundit policy scope `resolve_admin` method is deprecated. " +
            "Please use a namespaced pundit policy instead.",
          )
          policy_scope.resolve_admin
        else
          policy_scope.resolve
        end
      end

      def pundit_model(record)
        record.is_a?(Array) ? record.last : record
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate-0.19.0 app/controllers/concerns/administrate/punditize.rb