Sha256: 4356dd3d8e89aa0d47f6cddb89a701c02cb665b7f410b079e110fc1f445902e3

Contents?: true

Size: 862 Bytes

Versions: 2

Compression:

Stored size: 862 Bytes

Contents

module ActionAuthorization
  class BasePolicy
    attr_accessor :user, :object

    def self.type
      name.gsub('Policy', '')
    end

    def self.type_class
      type.constantize
    rescue NameError
      NilClass
    end

    delegate :type, :type_class, to: :class

    def initialize(user, object)
      self.user = user
      self.object = object
    end

    # create alias to object from subclass name
    def self.inherited(klass)
      klass.send(:alias_method, klass.type.underscore, :object)
    end

    def index?
      authorized?
    end

    def show?
      authorized?
    end

    def new?
      create?
    end

    def create?
      authorized?
    end

    def edit?
      update?
    end

    def update?
      authorized?
    end

    def destroy?
      create?
    end

    private

    def authorized?
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
action_authorization-0.3.0 lib/action_authorization/base_policy.rb
action_authorization-0.2.3 lib/action_authorization/base_policy.rb