Module: Challah::Rolls::User::ClassMethods

Defined in:
lib/challah/rolls/user.rb

Instance Method Summary collapse

Instance Method Details

#find_all_by_permission(permission_id_or_key) ⇒ Object Also known as: find_by_permission

Returns a scope of all users that are assigned with the given permission. This takes into account permissions assigned by a user role, or permissions given to a user on an ad-hoc basis.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/challah/rolls/user.rb', line 39

def find_all_by_permission(permission_id_or_key)
  permission = case permission_id_or_key
  when ::Permission
    permission_id_or_key
  when Symbol
    ::Permission[permission_id_or_key]
  else
    ::Permission.find_by_id(permission_id_or_key)
  end

  unless ::Permission === permission
    return self.scoped.limit(0)
  end

  user_ids = permission.permission_users.pluck(:user_id).to_a
  role_ids = permission.permission_roles.pluck(:role_id).to_a

  if user_ids.count.zero?
    self.where(:role_id => role_ids)
  else
    t = self.arel_table
    self.where(t[:role_id].in(role_ids).or(t[:id].in(user_ids)))
  end
end

#find_all_by_role(role_or_id_or_name) ⇒ Object Also known as: find_by_role

Returns a scope of all users that are assigned to the given role. Accepts a ‘Role` instance, a role_id, or a Symbol of the role name.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/challah/rolls/user.rb', line 67

def find_all_by_role(role_or_id_or_name)
  role_id = case role_or_id_or_name
  when ::Role
    role_or_id_or_name[:id]
  when Symbol
    ::Role[role_or_id_or_name][:id]
  else
    role_or_id_or_name
  end

  ::User.with_role(role_id)
end