Sha256: 0c32f611c1aa8a781866e6e5a171eef79724e2998053be9aed6da4ee3dac5018

Contents?: true

Size: 741 Bytes

Versions: 1

Compression:

Stored size: 741 Bytes

Contents

module Policies::Methods
  def self.included(base)
    base.send(:helper_method, :authorized?) if base.respond_to?(:helper_method)
  end

  def authorize(action = action_name, object)
    raise Policies::UnauthorizedError unless authorized?(action, object)
  end

  def authorized?(action = action_name, object)
    policy_class(object).new(current_user, current_role, object).public_send(action.to_s + '?')
  end

  private

  def policy_class(object)
    class_name =
      if object.is_a?(Symbol)
        object.to_s.classify
      else
        object.class.to_s
      end + 'Policy'

    class_name.constantize
  end
end

if defined?(ActionController::Base)
  ActionController::Base.class_eval do
    include Policies::Methods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
policies-1.1.0 lib/policies/methods.rb