Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/generators/lab/setup/templates/devise/devise_user.rb

Constant Summary collapse

ROLES =
%w[user admin]

Instance Method Summary collapse

Instance Method Details

#add_role(role) ⇒ Object



18
19
20
21
# File 'lib/generators/lab/setup/templates/devise/devise_user.rb', line 18

def add_role(role)
  r = self.roles.push(role)
  self.roles = r
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/generators/lab/setup/templates/devise/devise_user.rb', line 27

def has_role?(role)
  roles.include?(role) ? true : false
end

#role_symbolsObject



23
24
25
# File 'lib/generators/lab/setup/templates/devise/devise_user.rb', line 23

def role_symbols
  roles.map(&:to_sym)
end

#rolesObject



14
15
16
# File 'lib/generators/lab/setup/templates/devise/devise_user.rb', line 14

def roles
  ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end

#roles=(roles) ⇒ Object



9
10
11
12
# File 'lib/generators/lab/setup/templates/devise/devise_user.rb', line 9

def roles=(roles)
  self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
  save
end