Module: Roleable::Subject

Defined in:
lib/roleable/subject.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/roleable/subject.rb', line 3

def self.included(base)
  base.has_many :applied_roles, :foreign_key => 'subject_id', :dependent => :destroy
end

Instance Method Details

#add_role(role_name, resource = nil) ⇒ Object

Add a role to the subject 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 subject 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

  ::AppliedRole.create_if_unique!(:subject_id => self.id, :role => role, :resource => resource)
end

#has_role?(role_name, resource = nil) ⇒ Boolean

Check if the subject has the given role for the given resource, or if they have the role globally if no resource given.

Returns true if the subject 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

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/roleable/subject.rb', line 33

def has_role?(role_name, resource = nil)
  ::AppliedRole.
    with_subject(self).
    with_resource(resource).
    with_role_name(role_name).
    exists?
end

#remove_role(role_name, resource = nil) ⇒ Object

Remove the given role from the subject 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


50
51
52
53
54
55
56
# File 'lib/roleable/subject.rb', line 50

def remove_role(role_name, resource = nil)
  applied_roles = ::AppliedRole.with_subject(self).with_resource(resource).with_role_name(role_name)

  deleted_count = applied_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 subject has the given role. If passed an array or roles, returns resources for which the subject has any of the roles.

Examples

user.resources_with_role(:editor, Page)  # => [page1, page2, ...]
user.resources_with_role([:editor, :author], Page)  # => [page1, page2, ...]


66
67
68
69
# File 'lib/roleable/subject.rb', line 66

def resources_with_role(role_name, resource_class)
  applied_roles = ::AppliedRole.with_subject(self).with_role_name(role_name).with_resource_class(resource_class)
  resource_class.includes(:applied_roles).merge(applied_roles)
end

#roles_for_resource(resource) ⇒ Object

Return a list of roles that the subject has for the given resource.

Examples

user.roles_for_resource(page)   # => [role1, role2, ...]


77
78
79
80
# File 'lib/roleable/subject.rb', line 77

def roles_for_resource(resource)
  applied_roles = ::AppliedRole.with_subject(self).with_resource(resource)
  ::Role.includes(:applied_roles).merge(applied_roles)
end