Sha256: 79c99c8664bb4d15f09ed09b434c0591c548dbf70327f2d7b2256c3e4b1801bb

Contents?: true

Size: 736 Bytes

Versions: 3

Compression:

Stored size: 736 Bytes

Contents

# frozen_string_literal: true

module IIPolicy
  module Controller
    extend ActiveSupport::Concern

    def policy_context
      { user: current_user }
    end

    def policy(item, context = {})
      if item.is_a?(Class) && item < IIPolicy::Base
        item.new(policy_context.merge(context))
      else
        klass = IIPolicy::Base.lookup(item)
        raise IIPolicy::Error.new("could not find policy for #{item}") unless klass
        klass.new(policy_context.merge(context.merge(item: item)))
      end
    end

    def authorize(item, context = {})
      instance = policy(item, context)
      raise IIPolicy::AuthorizationError.new('Not Authorized') unless instance.call("#{action_name}?")
      instance
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ii_policy-2.0.0 lib/ii_policy/controller.rb
ii_policy-1.1.0 lib/ii_policy/controller.rb
ii_policy-1.0.0 lib/ii_policy/controller.rb