Module: ActsAsRoleRestricted::ClassMethods

Defined in:
app/models/concerns/acts_as_role_restricted.rb

Instance Method Summary collapse

Instance Method Details

#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



64
65
66
67
68
69
# File 'app/models/concerns/acts_as_role_restricted.rb', line 64

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



59
60
61
# File 'app/models/concerns/acts_as_role_restricted.rb', line 59

def with_role(*roles)
  where(with_role_sql(roles))
end

#with_role_sql(*roles) ⇒ Object



71
72
73
74
75
76
77
# File 'app/models/concerns/acts_as_role_restricted.rb', line 71

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 } & EffectiveRoles.roles)

  roles.map { |role| "(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' OR ')
end

#without_role(*roles) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'app/models/concerns/acts_as_role_restricted.rb', line 79

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 } & EffectiveRoles.roles)

  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