Class: Pundit::PolicyFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/pundit/policy_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ PolicyFinder

Returns a new instance of PolicyFinder.



5
6
7
# File 'lib/pundit/policy_finder.rb', line 5

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/pundit/policy_finder.rb', line 3

def object
  @object
end

Instance Method Details

#policyObject



15
16
17
18
19
20
21
# File 'lib/pundit/policy_finder.rb', line 15

def policy
  klass = find
  klass = klass.constantize if klass.is_a?(String)
  klass
rescue NameError
  nil
end

#policy!Object

Raises:



28
29
30
31
# File 'lib/pundit/policy_finder.rb', line 28

def policy!
  raise NotDefinedError, "unable to find policy of nil" if object.nil?
  policy or raise NotDefinedError, "unable to find policy `#{find}` for `#{object.inspect}`"
end

#scopeObject



9
10
11
12
13
# File 'lib/pundit/policy_finder.rb', line 9

def scope
  policy::Scope if policy
rescue NameError
  nil
end

#scope!Object

Raises:



23
24
25
26
# File 'lib/pundit/policy_finder.rb', line 23

def scope!
  raise NotDefinedError, "unable to find policy scope of nil" if object.nil?
  scope or raise NotDefinedError, "unable to find scope `#{find}::Scope` for `#{object.inspect}`"
end