forked from RolifyCommunity/rolify
/
finders.rb
39 lines (37 loc) · 1.11 KB
/
finders.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Rolify
module Finders
def with_role(role_name, resource = nil)
self.adapter.scope(self, :name => role_name, :resource => resource)
end
def with_all_roles(*args)
users = []
args.each do |arg|
if arg.is_a? Hash
users_to_add = self.with_role(arg[:name], arg[:resource])
elsif arg.is_a?(String) || arg.is_a?(Symbol)
users_to_add = self.with_role(arg)
else
raise ArgumentError, "Invalid argument type: only hash or string or symbol allowed"
end
users = users_to_add if users.empty?
users &= users_to_add
return [] if users.empty?
end
users
end
def with_any_role(*args)
users = []
args.each do |arg|
if arg.is_a? Hash
users_to_add = self.with_role(arg[:name], arg[:resource])
elsif arg.is_a?(String) || arg.is_a?(Symbol)
users_to_add = self.with_role(arg)
else
raise ArgumentError, "Invalid argument type: only hash or string or symbol allowed"
end
users += users_to_add
end
users.uniq
end
end
end