lib/pundit/policy_finder.rb in pundit-0.1.0 vs lib/pundit/policy_finder.rb in pundit-0.2.0

- old
+ new

@@ -4,46 +4,49 @@ def initialize(object) @object = object end - def name - if object.respond_to?(:model_name) - object.model_name.to_s - elsif object.class.respond_to?(:model_name) - object.class.model_name.to_s - elsif object.is_a?(Class) - object.to_s - else - object.class.to_s - end - end - def scope - scope_name.constantize + policy::Scope if policy rescue NameError nil end def policy - policy_name.constantize + klass = find + klass = klass.constantize if klass.is_a?(String) + klass rescue NameError nil end def scope! - scope or raise NotDefinedError, "unable to find scope #{scope_name} for #{object}" + scope or raise NotDefinedError, "unable to find scope #{find}::Scope for #{object}" end def policy! - policy or raise NotDefinedError, "unable to find policy #{policy_name} for #{object}" + policy or raise NotDefinedError, "unable to find policy #{find} for #{object}" end - def scope_name - "#{name}Policy::Scope" - end + private - def policy_name - "#{name}Policy" + def find + if 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 + else + object.class + end + "#{klass}Policy" + end end end end