Class: Role
- Inherits:
-
ActiveYaml::Base
- Object
- ActiveYaml::Base
- Role
show all
- Includes:
- ActiveYaml::Aliases
- Defined in:
- lib/models/role.rb
Defined Under Namespace
Classes: AbilityGenerator, Collection
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.admin ⇒ Object
15
16
17
|
# File 'lib/models/role.rb', line 15
def self.admin
find_by_key("admin")
end
|
.assignable ⇒ Object
40
41
42
|
# File 'lib/models/role.rb', line 40
def self.assignable
all.reject(&:default?)
end
|
.default ⇒ Object
19
20
21
|
# File 'lib/models/role.rb', line 19
def self.default
find_by_key("default")
end
|
.find(key) ⇒ Object
44
45
46
|
# File 'lib/models/role.rb', line 44
def self.find(key)
all.find { |role| role.key == key.to_s }
end
|
.find_by_key(key) ⇒ Object
Allow us to use either symbols or strings when searching
24
25
26
|
# File 'lib/models/role.rb', line 24
def self.find_by_key(key)
super(key.to_s)
end
|
.includes(role_or_key) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/models/role.rb', line 28
def self.includes(role_or_key)
role_key = role_or_key.is_a?(Role) ? role_or_key.key : role_or_key.to_s
role = Role.find_by_key(role_key)
return [] if role.nil?
return Role.all.select(&:assignable?) if role.default?
result = []
all.each do |role|
result << role if role.includes.include?(role_key)
end
result
end
|
Instance Method Details
#ability_generator(user, through, parent, intermediary) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/models/role.rb', line 99
def ability_generator(user, through, parent, intermediary)
models.each do |model_name, _|
ag = AbilityGenerator.new(self, model_name, user, through, parent, intermediary)
yield(ag)
end
end
|
#admin? ⇒ Boolean
72
73
74
|
# File 'lib/models/role.rb', line 72
def admin?
key == "admin"
end
|
#assignable? ⇒ Boolean
95
96
97
|
# File 'lib/models/role.rb', line 95
def assignable?
!default?
end
|
#default? ⇒ Boolean
68
69
70
|
# File 'lib/models/role.rb', line 68
def default?
key == "default"
end
|
#id ⇒ Object
We don’t want to ever use the automatically generated ids from ActiveYaml. These are created based on the order of objects in the yml file so if someone ever changed the order of that file around, they would really mess up the user permissions. Instead, we’re using the key attribute.
50
51
52
|
# File 'lib/models/role.rb', line 50
def id
key
end
|
#included_by ⇒ Object
58
59
60
|
# File 'lib/models/role.rb', line 58
def included_by
Role.includes(self)
end
|
#included_roles ⇒ Object
76
77
78
79
80
|
# File 'lib/models/role.rb', line 76
def included_roles
default_roles = []
default_roles << Role.default unless default?
(default_roles + includes.map { |included_key| Role.find_by_key(included_key) }).uniq.compact
end
|
#key_plus_included_by_keys ⇒ Object
We need to search for memberships that have this role included directly OR any memberships that have this role through it being included in another role they have
63
64
65
66
|
# File 'lib/models/role.rb', line 63
def key_plus_included_by_keys
(included_by.map(&:key_plus_included_by_keys).flatten + [id]).uniq
end
|
#manageable_by?(role_or_roles) ⇒ Boolean
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/models/role.rb', line 82
def manageable_by?(role_or_roles)
return true if default?
roles = role_or_roles.is_a?(Array) ? role_or_roles : [role_or_roles]
roles.each do |role|
return true if role.manageable_roles.include?(key)
role.included_roles.each do |included_role|
return true if manageable_by?([included_role])
end
end
false
end
|
#to_s ⇒ Object
54
55
56
|
# File 'lib/models/role.rb', line 54
def to_s
key
end
|