Module: RoleStrategy::MongoMapper::ManyRoles::Implementation

Includes:
Roles::MongoMapper::Strategy::Multi
Defined in:
lib/roles_mongo_mapper/strategy/multi/many_roles.rb,
lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb

Instance Method Summary collapse

Methods included from Roles::MongoMapper::Strategy::Multi

#remove_roles

Instance Method Details

#add_roles(*role_names) ⇒ Object



50
51
52
53
54
55
# File 'lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb', line 50

def add_roles(*role_names)  
  raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
  role_relations = role_class.find_roles(*role_names)
  self.send(role_attribute) << role_relations
  save
end

#get_rolesObject



47
48
49
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 47

def get_roles
  self.send(role_attribute)
end

#has_roles?(*roles_names) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 42

def has_roles?(*roles_names)
  compare_roles = extract_roles(roles_names.flat_uniq)
  (roles_list & compare_roles).not.empty?      
end

#new_roles(*role_names) ⇒ Object



73
74
75
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 73

def new_roles *role_names
  role_class.find_roles(extract_roles role_names)
end

#present_roles(roles_names) ⇒ Object



77
78
79
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 77

def present_roles roles_names
  roles_names.to_a.map{|role| role.name.to_s.to_sym}
end

#role_id_attributeObject



36
37
38
# File 'lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb', line 36

def role_id_attribute
  "#{role_attribute}_ids".to_sym
end

#rolesObject

query assigned roles



58
59
60
61
62
# File 'lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb', line 58

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

#roles=(*_roles) ⇒ Object

assign roles



64
65
66
67
68
69
70
71
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 64

def roles=(*role_names)
  role_names = role_names.flat_uniq
  role_names = extract_roles(role_names)
  return nil if role_names.empty?
  valids = role_class.find_roles(role_names).to_a
  vrs = select_valid_roles role_names
  set_roles(vrs)
end

#roles_listObject



57
58
59
60
61
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 57

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

#set_empty_rolesObject



81
82
83
# File 'lib/roles_mongo_mapper/strategy/multi/many_roles.rb', line 81

def set_empty_roles
  self.send("#{role_attribute}=", [])
end