Class: Role
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Role
- Defined in:
- app/models/role.rb
Overview
Roles are hierarchically organized, so that the current role for a session can be downgraded to a lower role. The hierarchy gives meaning to “lower role”.
Class Method Summary collapse
- .developer ⇒ Object
- .developer_id ⇒ Object
-
.equal_or_lower_than(role) ⇒ Object
takes either an array of roles or a single role object.
-
.lower_than(role) ⇒ Object
takes either an array of roles or a single role object.
-
.with_ancestor(role) ⇒ Object
returns an array of roles that have the passed-in role as an ancestor.
Instance Method Summary collapse
Class Method Details
.developer ⇒ Object
60 61 62 |
# File 'app/models/role.rb', line 60 def self.developer where('name = "developer"').first end |
.developer_id ⇒ Object
64 65 66 |
# File 'app/models/role.rb', line 64 def self.developer_id developer && developer.id end |
.equal_or_lower_than(role) ⇒ Object
takes either an array of roles or a single role object
26 27 28 29 |
# File 'app/models/role.rb', line 26 def self.equal_or_lower_than(role) roles = role.is_a?(Array) ? role : [role] (lower_than(role) + roles).uniq end |
.lower_than(role) ⇒ Object
takes either an array of roles or a single role object
32 33 34 35 36 37 38 |
# File 'app/models/role.rb', line 32 def self.lower_than(role) roles = role.is_a?(Array) ? role : [role] collection = roles.inject([]) do |ar, r| ar + with_ancestor(r) end (collection).uniq end |
.with_ancestor(role) ⇒ Object
returns an array of roles that have the passed-in role as an ancestor
42 43 44 |
# File 'app/models/role.rb', line 42 def self.with_ancestor(role) all.select{|r| r.has_ancestor?(role)} end |
Instance Method Details
#ancestors ⇒ Object
50 51 52 53 54 |
# File 'app/models/role.rb', line 50 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#has_ancestor?(role) ⇒ Boolean
46 47 48 |
# File 'app/models/role.rb', line 46 def has_ancestor?(role) ancestors.include?(role) end |
#is_developer? ⇒ Boolean
56 57 58 |
# File 'app/models/role.rb', line 56 def is_developer? name == 'developer' end |
#to_s ⇒ Object
68 69 70 |
# File 'app/models/role.rb', line 68 def to_s name end |