Class: Can4::Ability
- Inherits:
-
Object
- Object
- Can4::Ability
- Defined in:
- lib/can4/ability.rb
Overview
Ability class for resources.
To define an ability model for your resource, define an ability class in a location of your choosing, and define the actions available to the resource on construction.
Instance Method Summary collapse
-
#allow_anything! ⇒ Object
Allows the object to perform any action on any subject.
-
#authorize!(action, subject, *args) ⇒ Object
Checks whether this resource has authorization to perform an action on a particular subject.
-
#can(action, subject, &block) ⇒ Object
Adds an access-granting rule.
-
#can?(action, subject, *args) ⇒ Boolean
Checks whether the object can perform an action on a subject.
-
#cannot?(*args) ⇒ Boolean
Inverse of #can?.
Instance Method Details
#allow_anything! ⇒ Object
Allows the object to perform any action on any subject. This overrides all #cannot rules.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/can4/ability.rb', line 63 def allow_anything! instance_eval do def can?(*) true end def cannot?(*) false end end end |
#authorize!(action, subject, *args) ⇒ Object
Checks whether this resource has authorization to perform an action on a particular subject. Raises Can4::AccessDenied if it doesn’t.
81 82 83 |
# File 'lib/can4/ability.rb', line 81 def (action, subject, *args) raise AccessDenied if cannot?(action, subject, *args) end |
#can(action, subject, &block) ⇒ Object
Adds an access-granting rule.
57 58 59 |
# File 'lib/can4/ability.rb', line 57 def can(action, subject, &block) rule_for(subject).add_grant(action, block) end |
#can?(action, subject) ⇒ Boolean #can?(action, subject, *args) ⇒ Boolean
Checks whether the object can perform an action on a subject.
41 42 43 |
# File 'lib/can4/ability.rb', line 41 def can?(action, subject, *args) lookup_rule(subject).(action, subject, args) end |
#cannot?(*args) ⇒ Boolean
Inverse of #can?.
48 49 50 |
# File 'lib/can4/ability.rb', line 48 def cannot?(*args) !can?(*args) end |