Sha256: 8b628dc9c058ff7066a4c00c57d14bd4bbc5e5d571cfa971351a930d2040007a

Contents?: true

Size: 788 Bytes

Versions: 2

Compression:

Stored size: 788 Bytes

Contents

# frozen_string_literal: true

require "active_support/core_ext/module/delegation"
require "active_support/core_ext/string/inflections"
require "action_authorization/version"
require "action_authorization/base_policy"
require "action_authorization/authorization_failure"

module ActionAuthorization
  def self.included(base)
    base.class_eval do
      helper_method :policy
    end
  end

  protected

  def authorize(object, action: action_name, policy_class: nil)
    raise AuthorizationFailure unless policy(object, policy_class).public_send(:"#{action}?")

    object
  end

  def policy(object, policy_class = nil)
    (policy_class || policy_class_for(object)).new(current_user, object)
  end

  def policy_class_for(object)
    "#{object.model_name}Policy".constantize
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
action_authorization-0.3.2 lib/action_authorization.rb
action_authorization-0.3.1 lib/action_authorization.rb