Module: Acl9::ModelExtensions::Subject
- Defined in:
- lib/acl9/model_extensions/subject.rb
Instance Method Summary collapse
-
#has_no_role!(role_name, object = nil) ⇒ Object
Free
self
from a specified role onobject
. -
#has_no_roles! ⇒ Object
Unassign all roles from
self
. -
#has_no_roles_for!(object = nil) ⇒ Object
Unassign any roles on
object
fromself
. -
#has_role!(role_name, object = nil) ⇒ Object
Add specified role on
object
toself
. -
#has_role?(role_name, object = nil) ⇒ Boolean
Role check.
-
#has_roles_for?(object) ⇒ Boolean
(also: #has_role_for?)
Are there any roles for
self
onobject
?. -
#roles_for(object) ⇒ Array<Role>
Which roles does
self
have onobject
?.
Instance Method Details
#has_no_role!(role_name, object = nil) ⇒ Object
Free self
from a specified role on object
.
50 51 52 |
# File 'lib/acl9/model_extensions/subject.rb', line 50 def has_no_role!(role_name, object = nil) delete_role(get_role(role_name, object)) end |
#has_no_roles! ⇒ Object
Unassign all roles from self
.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/acl9/model_extensions/subject.rb', line 91 def has_no_roles! # for some reason simple # # self.roles.each { |role| delete_role(role) } # # doesn't work. seems like a bug in ActiveRecord self.role_objects.map(&:id).each do |role_id| delete_role self._auth_role_class.find(role_id) end end |
#has_no_roles_for!(object = nil) ⇒ Object
Unassign any roles on object
from self
.
85 86 87 |
# File 'lib/acl9/model_extensions/subject.rb', line 85 def has_no_roles_for!(object = nil) roles_for(object).each { |role| delete_role(role) } end |
#has_role!(role_name, object = nil) ⇒ Object
Add specified role on object
to self
.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/acl9/model_extensions/subject.rb', line 28 def has_role!(role_name, object = nil) role = get_role(role_name, object) if role.nil? role_attrs = case object when Class then { :authorizable_type => object.to_s } when nil then {} else { :authorizable => object } end.merge( { :name => role_name.to_s }) role = self._auth_role_class.create(role_attrs) end self.role_objects << role if role && !self.role_objects.exists?(role.id) end |
#has_role?(role_name, object = nil) ⇒ Boolean
Role check.
12 13 14 15 16 17 18 19 20 |
# File 'lib/acl9/model_extensions/subject.rb', line 12 def has_role?(role_name, object = nil) !! if object.nil? self.role_objects.find_by_name(role_name.to_s) || self.role_objects.member?(get_role(role_name, nil)) else role = get_role(role_name, object) role && self.role_objects.exists?(role.id) end end |
#has_roles_for?(object) ⇒ Boolean Also known as: has_role_for?
Are there any roles for self
on object
?
60 61 62 |
# File 'lib/acl9/model_extensions/subject.rb', line 60 def has_roles_for?(object) !!self.role_objects.detect(&role_selecting_lambda(object)) end |
#roles_for(object) ⇒ Array<Role>
Which roles does self
have on object
?
77 78 79 |
# File 'lib/acl9/model_extensions/subject.rb', line 77 def roles_for(object) self.role_objects.select(&role_selecting_lambda(object)) end |