Module: RoleStrategy::Mongoid::EmbedManyRoles::Implementation

Includes:
Mongoid::Roles::Strategy::Multi
Defined in:
lib/roles_mongoid/strategy/multi/embed_many_roles.rb

Instance Method Summary collapse

Instance Method Details

#exchange_roles(*role_names) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 63

def exchange_roles *role_names
  options = last_option role_names
  raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if !options || !options.kind_of?(Hash)
  common = role_names.to_symbols & roles_list
  if !common.empty?
    with_roles = options[:with]
    set_roles with_roles
  end
end

#get_rolesObject



82
83
84
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 82

def get_roles
  self.send(role_attribute)
end

#has_roles?(*roles_names) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 77

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



108
109
110
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 108

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

#present_roles(roles_names) ⇒ Object



112
113
114
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 112

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

#remove_roles(*role_names) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 53

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)       
  
  diff = roles_diff(role_names)
  set_roles(diff) 
  true
end

#rolesObject



86
87
88
89
90
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 86

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

#roles=(*role_names) ⇒ Object

assign multiple roles



99
100
101
102
103
104
105
106
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 99

def roles=(*role_names)
  role_names = role_names.flat_uniq
  role_names = extract_roles(role_names)
  return nil if role_names.empty?

  vrs = select_valid_roles role_names
  set_roles(vrs)
end

#roles_diff(*roles) ⇒ Object



49
50
51
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 49

def roles_diff *roles
  self.roles_list - extract_roles(roles.flat_uniq)
end

#roles_listObject



92
93
94
95
96
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 92

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

#select_valid_roles(*role_names) ⇒ Object



73
74
75
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 73

def select_valid_roles *role_names
  role_names = role_names.flat_uniq.select{|role| valid_role? role }        
end

#set_empty_rolesObject



116
117
118
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 116

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

#set_role(role) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 35

def set_role role
  raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
  self.send("#{role_attribute}=", nil)
  self.send("create_#{role_attribute}").create(:name => role)
end

#set_roles(*roles) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/roles_mongoid/strategy/multi/embed_many_roles.rb', line 41

def set_roles *roles
  roles = roles.flat_uniq
  self.send("#{role_attribute}=", nil)        
  roles.each do |role|
    self.send("#{role_attribute}").create(:name => role)
  end
end