Module: Roleable::Subject
- Defined in:
- lib/roleable/subject.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_role(role_name, resource = nil) ⇒ Object
Add a role to the user scoped to the given resource or global if no resource given.
-
#has_role?(role_name, resource = nil) ⇒ Boolean
Check if the user has the given role for the given resource, or if they have the role globally if no resource given.
-
#remove_role(role_name, resource = nil) ⇒ Object
Remove the given role from the user for the given resource, or globally if no resource given.
-
#resources_with_role(role_name, resource_class) ⇒ Object
Return a list of resources of the given class, for which the user has the given role.
-
#roles_for_resource(resource) ⇒ Object
Return a list of roles that the user has for the given resource.
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/roleable/subject.rb', line 3 def self.included(base) base.has_many :user_roles end |
Instance Method Details
#add_role(role_name, resource = nil) ⇒ Object
Add a role to the user scoped to the given resource or global if no resource given.
Does nothing if a role with the given name doesn’t exist, or if the user already has the given role.
Examples
user.add_role(:editor, page) # Add the editor role to user, scoped to page
user.add_role(:admin) # Add the admin role to user, globally
17 18 19 20 21 |
# File 'lib/roleable/subject.rb', line 17 def add_role(role_name, resource = nil) role = ::Role.find_by_name(role_name) or return ::UserRole.create_if_unique!(:user => self, :role => role, :resource => resource) end |
#has_role?(role_name, resource = nil) ⇒ Boolean
Check if the user has the given role for the given resource, or if they have the role globally if no resource given.
Returns true if the user has the role, false otherwise.
Examples
user.has_role?(:editor, page) # True if the user has the editor role for page
user.has_role?(:admin) # True if the user has a global admin role
33 34 35 36 37 |
# File 'lib/roleable/subject.rb', line 33 def has_role?(role_name, resource = nil) user_roles = ::UserRole.with_user(self).with_resource(resource).with_role_name(role_name) user_roles.exists? end |
#remove_role(role_name, resource = nil) ⇒ Object
Remove the given role from the user for the given resource, or globally if no resource given.
Returns true if the role was found and deleted, false otherwise.
Examples
user.remove_role(:editor, page) # Remove the editor role from the user for page
user.remove_role(:admin) # Remove the global admin role from the user
48 49 50 51 52 53 54 |
# File 'lib/roleable/subject.rb', line 48 def remove_role(role_name, resource = nil) user_roles = ::UserRole.with_user(self).with_resource(resource).with_role_name(role_name) deleted_count = user_roles.delete_all deleted_count > 0 end |
#resources_with_role(role_name, resource_class) ⇒ Object
Return a list of resources of the given class, for which the user has the given role.
Examples
user.resources_with_role(:editor, Page) # => [page1, page2, ...]
62 63 64 65 |
# File 'lib/roleable/subject.rb', line 62 def resources_with_role(role_name, resource_class) user_roles = ::UserRole.with_user(self).with_role_name(role_name).with_resource_class(resource_class) resource_class.includes(:user_roles).merge(user_roles) end |
#roles_for_resource(resource) ⇒ Object
Return a list of roles that the user has for the given resource.
Examples
user.roles_for_resource(page) # => [role1, role2, ...]
73 74 75 76 |
# File 'lib/roleable/subject.rb', line 73 def roles_for_resource(resource) user_roles = ::UserRole.with_user(self).with_resource(resource) ::Role.includes(:user_roles).merge(user_roles) end |