Module: RoleStrategy::Mongoid::ManyRoles::Implementation
- Includes:
- Mongoid::Roles::Strategy::Multi
- Defined in:
- lib/roles_mongoid/strategy/multi/many_roles.rb
Instance Method Summary collapse
- #get_roles ⇒ Object
- #has_roles?(*roles_names) ⇒ Boolean
- #new_roles(*role_names) ⇒ Object
- #present_roles(roles_names) ⇒ Object
- #roles ⇒ Object
-
#roles=(*role_names) ⇒ Object
assign multiple roles.
- #roles_list ⇒ Object
- #set_empty_roles ⇒ Object
Instance Method Details
#get_roles ⇒ Object
41 42 43 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 41 def get_roles self.send(role_attribute) end |
#has_roles?(*roles_names) ⇒ Boolean
36 37 38 39 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 36 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
67 68 69 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 67 def new_roles *role_names role_class.find_roles(extract_roles role_names) end |
#present_roles(roles_names) ⇒ Object
71 72 73 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 71 def present_roles roles_names roles_names.to_a.map{|role| role.name.to_s.to_sym} end |
#roles ⇒ Object
45 46 47 48 49 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 45 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
58 59 60 61 62 63 64 65 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 58 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_list ⇒ Object
51 52 53 54 55 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 51 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_roles ⇒ Object
75 76 77 |
# File 'lib/roles_mongoid/strategy/multi/many_roles.rb', line 75 def set_empty_roles self.send("#{role_attribute}=", []) end |