Module: Roles::ActiveRecord::Strategy::Multi

Includes:
Shared
Included in:
RoleStrategy::ActiveRecord::ManyRoles::Implementation, RoleStrategy::ActiveRecord::RolesMask::Implementation
Defined in:
lib/roles_active_record/strategy/multi.rb

Instance Method Summary collapse

Methods included from Shared

#get_role, #has_any_roles?, #has_no_roles?, #remove_all_roles!, #select_valid_roles, #set_default_role, #set_role

Instance Method Details

#add_roles(*roles) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/roles_active_record/strategy/multi.rb', line 15

def add_roles *roles
  new_roles = select_valid_roles(roles)
  if !new_roles.empty?
    self.roles = self.roles + new_roles
    self.save
  end
end

#remove_roles(*role_names) ⇒ Object

should remove the current single role (set = nil) only if it is contained in the list of roles to be removed



25
26
27
28
29
30
31
# File 'lib/roles_active_record/strategy/multi.rb', line 25

def remove_roles *role_names
  role_names = role_names.flat_uniq
  set_empty_roles and return if roles_diff(role_names).empty?
  roles_to_remove = select_valid_roles(role_names)
  set_roles roles_diff(role_names)
  true
end

#rolesObject

query assigned roles



34
35
36
37
38
# File 'lib/roles_active_record/strategy/multi.rb', line 34

def roles
  get_roles.map do |role|
    role.respond_to?(:sym) ? role.to_sym : role
  end
end

#roles=(*role_names) ⇒ Object

assign multiple roles



9
10
11
12
13
# File 'lib/roles_active_record/strategy/multi.rb', line 9

def roles=(*role_names)
  extracted_roles = extract_roles(role_names)
  return nil if extracted_roles.empty?
  set_roles(select_valid_roles extracted_roles)
end

#roles_listObject



40
41
42
43
44
# File 'lib/roles_active_record/strategy/multi.rb', line 40

def roles_list
  my_roles = [roles].flat_uniq
  return [] if my_roles.empty?
  has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
end