Module: RoleMaking::Role
- Defined in:
- lib/role_making/role.rb
Instance Method Summary collapse
- #add_role(role_name) ⇒ Object (also: #grant)
- #has_all_roles?(*args) ⇒ Boolean
- #has_any_role?(*args) ⇒ Boolean
- #has_role?(role_name) ⇒ Boolean
- #remove_role(roles_name) ⇒ Object (also: #revoke)
- #roles_name ⇒ Object
Instance Method Details
#add_role(role_name) ⇒ Object Also known as: grant
5 6 7 8 9 10 11 12 |
# File 'lib/role_making/role.rb', line 5 def add_role(role_name) role = self.roles.where(name: role_name).first unless role role = self.class.role_cname.constantize.where(name: role_name.to_s).first_or_initialize self.roles << role end role end |
#has_all_roles?(*args) ⇒ Boolean
26 27 28 29 30 |
# File 'lib/role_making/role.rb', line 26 def has_all_roles?(*args) args.all? do |item| self.has_role?(item) end end |
#has_any_role?(*args) ⇒ Boolean
32 33 34 |
# File 'lib/role_making/role.rb', line 32 def has_any_role?(*args) args.any? { |r| self.has_role?(r) } end |
#has_role?(role_name) ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/role_making/role.rb', line 15 def has_role?(role_name) # role_array = if new_record? # self.roles.detect { |r| r.name.to_s == role_name.to_s } # else # #self.respond_to?(:cache_roles) ? self.cache_roles.detect{|it| it.name == role_name.to_s} : self.roles.exists?( name: role_name) # self.roles.detect {||} # end !!self.roles.detect { |r| r.name.to_s == role_name.to_s } #!!role_array end |
#remove_role(roles_name) ⇒ Object Also known as: revoke
37 38 39 |
# File 'lib/role_making/role.rb', line 37 def remove_role(roles_name) self.roles.delete(self.class.role_cname.constantize.where(name: roles_name)) end |
#roles_name ⇒ Object
43 44 45 46 |
# File 'lib/role_making/role.rb', line 43 def roles_name #self.roles.select(:name).map { |r| r.name } self.roles.map { |r| r.display_name } end |