Class: Pundit::PolicyFinder
- Inherits:
-
Object
- Object
- Pundit::PolicyFinder
- Defined in:
- lib/pundit/policy_finder.rb
Overview
Finds policy and scope classes for given object.
Instance Attribute Summary collapse
- #object ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(object) ⇒ PolicyFinder
constructor
A new instance of PolicyFinder.
-
#param_key ⇒ String
The name of the key this object would have in a params hash.
-
#policy ⇒ nil, Class
Policy class with query methods.
-
#policy! ⇒ Class
Policy class with query methods.
-
#scope ⇒ nil, Scope{#resolve}
Scope class which can resolve to a scope.
-
#scope! ⇒ Scope{#resolve}
Scope class which can resolve to a scope.
Constructor Details
#initialize(object) ⇒ PolicyFinder
Returns a new instance of PolicyFinder.
17 18 19 |
# File 'lib/pundit/policy_finder.rb', line 17 def initialize(object) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
13 14 15 |
# File 'lib/pundit/policy_finder.rb', line 13 def object @object end |
Instance Method Details
#param_key ⇒ String
Returns the name of the key this object would have in a params hash.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pundit/policy_finder.rb', line 59 def param_key # rubocop:disable Metrics/AbcSize model = object.is_a?(Array) ? object.last : object if model.respond_to?(:model_name) model.model_name.param_key.to_s elsif model.is_a?(Class) model.to_s.demodulize.underscore else model.class.to_s.demodulize.underscore end end |
#policy ⇒ nil, Class
Returns policy class with query methods.
38 39 40 41 |
# File 'lib/pundit/policy_finder.rb', line 38 def policy klass = find(object) klass.is_a?(String) ? klass.safe_constantize : klass end |
#policy! ⇒ Class
Returns policy class with query methods.
53 54 55 |
# File 'lib/pundit/policy_finder.rb', line 53 def policy! policy or raise NotDefinedError, "unable to find policy `#{find(object)}` for `#{object.inspect}`" end |
#scope ⇒ nil, Scope{#resolve}
Returns scope class which can resolve to a scope.
27 28 29 |
# File 'lib/pundit/policy_finder.rb', line 27 def scope "#{policy}::Scope".safe_constantize end |
#scope! ⇒ Scope{#resolve}
Returns scope class which can resolve to a scope.
46 47 48 |
# File 'lib/pundit/policy_finder.rb', line 46 def scope! scope or raise NotDefinedError, "unable to find scope `#{find(object)}::Scope` for `#{object.inspect}`" end |