Class: Abilities::Definitions
- Inherits:
-
Object
- Object
- Abilities::Definitions
- Defined in:
- lib/abilities/definitions.rb
Instance Attribute Summary collapse
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #add(actions, resources, policy) ⇒ Object
- #can?(action, resource) ⇒ Boolean
- #cannot?(*args) ⇒ Boolean
-
#initialize(user, &block) ⇒ Definitions
constructor
A new instance of Definitions.
Constructor Details
#initialize(user, &block) ⇒ Definitions
Returns a new instance of Definitions.
6 7 8 9 |
# File 'lib/abilities/definitions.rb', line 6 def initialize(user, &block) @user = user Proxy.new self, &block end |
Instance Attribute Details
#user ⇒ Object (readonly)
Returns the value of attribute user.
4 5 6 |
# File 'lib/abilities/definitions.rb', line 4 def user @user end |
Instance Method Details
#add(actions, resources, policy) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/abilities/definitions.rb', line 34 def add(actions, resources, policy) unless actions.is_a?(Array) actions = [actions] end unless resources.is_a?(Array) resources = [resources] end resources.each do |resource| actions.each do |action| id = resource_id(resource) registry[id] ||= {} registry[id][action] = policy end end end |
#can?(action, resource) ⇒ Boolean
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/abilities/definitions.rb', line 11 def can?(action, resource) id = resource_id(resource) if id != :any && can?(action, :any) true elsif actions = registry[id] if policy = (actions[action] || actions[:manage]) if policy.is_a?(Proc) user.instance_exec resource, &policy else policy end else false end else false end end |
#cannot?(*args) ⇒ Boolean
30 31 32 |
# File 'lib/abilities/definitions.rb', line 30 def cannot?(*args) !can?(*args) end |