Class: Miau::PolicyRun

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/miau/run.rb

Instance Method Summary collapse

Instance Method Details

#find_policy(policy, klass, action) ⇒ Object

return instance of policy (may be nil) and the method klass and action are symbols Priority:

- method of <klass>Policy
- method of <klass>Policy specified by "miau action, method"
- method of ApplicationPolicy (independent of klass)
- method of ApplicationPolicy specified by "miau action, method"
- nil

returns method_name



20
21
22
23
24
25
26
27
# File 'lib/miau/run.rb', line 20

def find_policy(policy, klass, action)
  return action if policy.respond_to?(action)

  hsh = PolicyStorage.instance.policies[klass]
  return nil unless hsh

  hsh[action]
end

#run(klass, action, user, resource) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/miau/run.rb', line 29

def run(klass, action, user, resource)
  policy = PolicyStorage.instance.find_or_create_policy(klass)
  meth = find_policy policy, klass, action if policy
  meth ||= find_policy ApplicationPolicy, :application, action

  unless meth
    msg = "class <#{klass}> action <#{action}>"
    raise NotDefinedError, msg
  end

  policy.user = user
  policy.resource = resource
  [meth].flatten.each { |m|
    return false unless policy.send(m)
  }
  true
end