Class: Kant::PolicyAccess
- Inherits:
-
Object
- Object
- Kant::PolicyAccess
- Includes:
- Resolvers::ActiveRecord
- Defined in:
- lib/kant/policy_access.rb
Instance Attribute Summary collapse
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#accessible(action, scope, *rest, **kwargs) ⇒ Object
Example:.
-
#can?(action, object) ⇒ Boolean
Delegates to an appropriate Policy module.
-
#initialize(user, policies_module: nil) ⇒ PolicyAccess
constructor
A new instance of PolicyAccess.
Constructor Details
#initialize(user, policies_module: nil) ⇒ PolicyAccess
Returns a new instance of PolicyAccess.
9 10 11 12 |
# File 'lib/kant/policy_access.rb', line 9 def initialize(user, policies_module: nil) @user = user @policies_module = policies_module || Kernel end |
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'lib/kant/policy_access.rb', line 7 def user @user end |
Instance Method Details
#accessible(action, scope, *rest, **kwargs) ⇒ Object
Example:
ability.accessible(:read, Content)
# => a Content scope
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kant/policy_access.rb', line 40 def accessible(action, scope, *rest, **kwargs) abilities = resolve_scope(scope) _scope_method = scope_method(abilities, action) if _scope_method abilities.send(_scope_method, scope, user, *rest, **kwargs) else scope.none end end |
#can?(action, object) ⇒ Boolean
Delegates to an appropriate Policy module. For example,
Ability.new(user).can?(:read, Foo.first)
will return
FooPolicy.can_read?(Foo.first, user)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kant/policy_access.rb', line 21 def can?(action, object) method_eh = "can_#{action}?" abilities = resolve_object(object) _scope_method = scope_method(abilities, action) model_class = object.class if abilities.respond_to?(method_eh) abilities.send(method_eh, object, user) elsif _scope_method && object.id abilities.send(_scope_method, model_class, user).where(id: object.id).any? else false end end |