Class: Godmin::Authorization::PolicyFinder

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

Class Method Summary collapse

Class Method Details

.find(object, namespace = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/godmin/authorization/policy_finder.rb', line 5

def find(object, namespace = nil)
  return object.policy_class if object.respond_to?(:policy_class)
  return object.class.policy_class if object.class.respond_to?(:policy_class)

  klass =
    if object.respond_to?(:model_name)
      object.model_name
    elsif object.class.respond_to?(:model_name)
      object.class.model_name
    elsif object.is_a?(Class)
      object
    elsif object.is_a?(Symbol)
      object.to_s.classify
    else
      object.class
    end

  if namespace
    "#{namespace}::#{klass}Policy"
  else
    "#{klass}Policy"
  end.constantize
end