Module: ActsAsRoleRestricted::ClassMethods
- Defined in:
- app/models/concerns/acts_as_role_restricted.rb
Instance Method Summary collapse
- #acts_as_role_restricted? ⇒ Boolean
-
#for_role(*roles) ⇒ Object
Returns all records which have been assigned any of the given roles, as well as any record with no role assigned.
-
#with_role(*roles) ⇒ Object
Returns all records which have been assigned any of the the given roles.
- #with_role_sql(*roles) ⇒ Object
- #without_role(*roles) ⇒ Object
Instance Method Details
#acts_as_role_restricted? ⇒ Boolean
58 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 58 def acts_as_role_restricted?; true; end |
#for_role(*roles) ⇒ Object
Returns all records which have been assigned any of the given roles, as well as any record with no role assigned
68 69 70 71 72 73 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 68 def for_role(*roles) sql = with_role_sql(roles) || '' sql += ' OR ' if sql.present? sql += "(#{self.table_name}.roles_mask = 0) OR (#{self.table_name}.roles_mask IS NULL)" where(sql) end |
#with_role(*roles) ⇒ Object
Returns all records which have been assigned any of the the given roles
63 64 65 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 63 def with_role(*roles) where(with_role_sql(roles)) end |
#with_role_sql(*roles) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 75 def with_role_sql(*roles) roles = roles.flatten.compact roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles) roles = roles.map { |role| role.to_sym } if(invalid = (roles - EffectiveRoles.roles)).present? raise("unknown role :#{invalid.to_sentence}") end roles.map { |role| "(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' OR ') end |
#without_role(*roles) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 87 def without_role(*roles) roles = roles.flatten.compact roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles) roles = roles.map { |role| role.to_sym } if(invalid = (roles - EffectiveRoles.roles)).present? raise("unknown role :#{invalid.to_sentence}") end where( roles.map { |role| "NOT(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' AND ') ).or(where(roles_mask: nil)) end |