1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00
varvet--pundit/lib/pundit/policy_finder.rb
2015-03-27 10:14:44 -07:00

60 lines
1.4 KiB
Ruby

module Pundit
class PolicyFinder
attr_reader :object
def initialize(object)
@object = object
end
def scope
policy::Scope if policy
rescue NameError
nil
end
def policy
klass = find
klass = klass.constantize if klass.is_a?(String)
klass
rescue NameError
nil
end
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
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
private
def find
if object.nil?
nil
elsif object.respond_to?(:policy_class)
object.policy_class
elsif object.class.respond_to?(:policy_class)
object.class.policy_class
else
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
elsif object.is_a?(Array)
object.join('/').to_s.classify
else
object.class
end
"#{klass}Policy"
end
end
end
end