Module: IIPolicy::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/ii_policy/controller.rb

Instance Method Summary collapse

Instance Method Details

#authorize(item, context = {}) ⇒ Object



21
22
23
24
25
# File 'lib/ii_policy/controller.rb', line 21

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

#policy(item, context = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/ii_policy/controller.rb', line 11

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

#policy_contextObject



7
8
9
# File 'lib/ii_policy/controller.rb', line 7

def policy_context
  { user: current_user }
end