Class: MoulinRouge::Ability
- Inherits:
-
Object
- Object
- MoulinRouge::Ability
- Defined in:
- lib/moulin_rouge/ability.rb
Overview
The CanCan wrapper with the custom DSL methods
Constant Summary collapse
- CANCAN_METHODS =
[:can, :cannot, :can?, :cannot?]
Instance Attribute Summary collapse
-
#is_a_group ⇒ Object
readonly
Returns true if it’s a group or flase otherwise.
-
#parent ⇒ Object
readonly
Returns a instance of the parent permission.
-
#singular_name ⇒ Object
readonly
Returns a string with the given name.
Instance Method Summary collapse
-
#abilities ⇒ Object
Returns an array with all authorizations declared on this permission.
-
#childrens ⇒ Object
Returns an array with all childrens.
-
#find(name) ⇒ Object
Returns the instance of the children with the given name if exists and nil otherwise.
-
#group(name, options = {}, &block) ⇒ Object
Define a group inside this scope.
-
#group? ⇒ Boolean
Returns true if is a group.
-
#include(name) ⇒ Object
Appends all childrens and abilities from one object to another, raises an error if could not found a match.
-
#inherithed_abilities ⇒ Object
Returns an array with the abilities collected from this node and from their childrens.
-
#initialize(name, options = {}, &block) ⇒ Ability
constructor
Creates a new permission and evaluate the given block with roles, groups and abilities on this scope.
-
#method_missing(name, *args, &block) ⇒ Object
Stores the CanCan methods and current_user method to later evaluation.
-
#name ⇒ Object
Returns a symbol with the name appended with the parents separeted by a underscore.
-
#role(name = :main, options = {}, &block) ⇒ Object
Define a new role inside this scope.
-
#role? ⇒ Boolean
Returns true if is a role.
-
#store_method(name, *args, &block) ⇒ Object
Add the given parameters to the authorizations list.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Ability
Creates a new permission and evaluate the given block with roles, groups and abilities on this scope. If the parent is a group append all their abilities to self
19 20 21 22 23 24 25 26 27 |
# File 'lib/moulin_rouge/ability.rb', line 19 def initialize(name, = {}, &block) @singular_name = name @parent = .delete(:parent) @is_group = .delete(:group) abilities.concat(parent.abilities) if not parent.nil? and parent.group? instance_eval(&block) if block_given? # Register this ability MoulinRouge::Authorization.register(self) unless parent.nil? or group? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Stores the CanCan methods and current_user method to later evaluation
30 31 32 33 34 |
# File 'lib/moulin_rouge/ability.rb', line 30 def method_missing(name, *args, &block) #:nodoc: return store_method(name, *args, &block) if CANCAN_METHODS.include?(name) return MoulinRouge::ModelDouble.new if MoulinRouge.configuration.model_instance == name super(name, *args, &block) end |
Instance Attribute Details
#is_a_group ⇒ Object (readonly)
Returns true if it’s a group or flase otherwise
14 15 16 |
# File 'lib/moulin_rouge/ability.rb', line 14 def is_a_group @is_a_group end |
#parent ⇒ Object (readonly)
Returns a instance of the parent permission. When nil this is the main permission.
11 12 13 |
# File 'lib/moulin_rouge/ability.rb', line 11 def parent @parent end |
#singular_name ⇒ Object (readonly)
Returns a string with the given name
7 8 9 |
# File 'lib/moulin_rouge/ability.rb', line 7 def singular_name @singular_name end |
Instance Method Details
#abilities ⇒ Object
Returns an array with all authorizations declared on this permission
97 98 99 |
# File 'lib/moulin_rouge/ability.rb', line 97 def abilities @abilities ||= [] end |
#childrens ⇒ Object
Returns an array with all childrens
92 93 94 |
# File 'lib/moulin_rouge/ability.rb', line 92 def childrens @childrens ||= [] end |
#find(name) ⇒ Object
Returns the instance of the children with the given name if exists and nil otherwise
108 109 110 111 |
# File 'lib/moulin_rouge/ability.rb', line 108 def find(name) childrens.each { |children| return children if children.name == name or children.singular_name == name } return nil end |
#group(name, options = {}, &block) ⇒ Object
Define a group inside this scope
49 50 51 52 |
# File 'lib/moulin_rouge/ability.rb', line 49 def group(name, = {}, &block) .merge!({:group => true}) role(name, , &block) end |
#group? ⇒ Boolean
Returns true if is a group
76 77 78 |
# File 'lib/moulin_rouge/ability.rb', line 76 def group? @is_group end |
#include(name) ⇒ Object
Appends all childrens and abilities from one object to another, raises an error if could not found a match.
56 57 58 59 60 61 62 |
# File 'lib/moulin_rouge/ability.rb', line 56 def include(name) unless from = MoulinRouge::Authorization.abilities[name] raise RoleNotFound end from.childrens.each { |children| childrens << children.dup } from.abilities.each { |ability| abilities << ability.dup } end |
#inherithed_abilities ⇒ Object
Returns an array with the abilities collected from this node and from their childrens.
103 104 105 |
# File 'lib/moulin_rouge/ability.rb', line 103 def inherithed_abilities abilities.concat(childrens.map(&:inherithed_abilities).flatten).uniq end |
#name ⇒ Object
Returns a symbol with the name appended with the parents separeted by a underscore
65 66 67 68 69 70 71 72 73 |
# File 'lib/moulin_rouge/ability.rb', line 65 def name unless @name @name = [] @name << self.parent.name.to_s if not self.parent.nil? and not self.parent.parent.nil? @name << self.singular_name.to_s @name = @name.join('_') end @name.to_sym end |
#role(name = :main, options = {}, &block) ⇒ Object
Define a new role inside this scope. If exists a role with the same name evaluate the block inside them instead of create a new one
38 39 40 41 42 43 44 45 46 |
# File 'lib/moulin_rouge/ability.rb', line 38 def role(name = :main, = {}, &block) if children = find(name) children.instance_eval(&block) children else childrens << self.class.new(name, .merge!({:parent => self}), &block) childrens.last end end |
#role? ⇒ Boolean
Returns true if is a role
81 82 83 |
# File 'lib/moulin_rouge/ability.rb', line 81 def role? !group? end |
#store_method(name, *args, &block) ⇒ Object
Add the given parameters to the authorizations list
86 87 88 89 |
# File 'lib/moulin_rouge/ability.rb', line 86 def store_method(name, *args, &block) abilities << MoulinRouge::CanCan::Method.new(name, *args, &block) abilities.last end |