Class: Rolify::Adapter::RoleAdapter
- Inherits:
-
RoleAdapterBase
show all
- Defined in:
- lib/rolify/adapters/mongoid/role_adapter.rb,
lib/rolify/adapters/active_record/role_adapter.rb
Instance Method Summary
collapse
-
#add(relation, role) ⇒ Object
-
#all_except(user, excluded_obj) ⇒ Object
-
#exists?(relation, column) ⇒ Boolean
-
#find_cached(relation, args) ⇒ Object
-
#find_cached_strict(relation, args) ⇒ Object
-
#find_or_create_by(role_name, resource_type = nil, resource_id = nil) ⇒ Object
-
#remove(relation, role_name, resource = nil) ⇒ Object
-
#scope(relation, conditions) ⇒ Object
-
#where(relation, *args) ⇒ Object
-
#where_strict(relation, args) ⇒ Object
Methods inherited from Base
create, #initialize, #relation_types_for, #role_class, #role_table, #user_class
Instance Method Details
#add(relation, role) ⇒ Object
50
51
52
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 50
def add(relation, role)
relation.roles << role
end
|
#all_except(user, excluded_obj) ⇒ Object
94
95
96
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 94
def all_except(user, excluded_obj)
user.not_in(_id: excluded_obj.to_a)
end
|
#exists?(relation, column) ⇒ Boolean
83
84
85
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 83
def exists?(relation, column)
relation.where(column.to_sym.ne => nil)
end
|
#find_cached(relation, args) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 22
def find_cached(relation, args)
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any
relation.find_all do |role|
(role.name == args[:name].to_s && role.resource_type == nil && role.resource_id == nil) ||
(role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == nil) ||
(role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == resource_id)
end
end
|
#find_cached_strict(relation, args) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 35
def find_cached_strict(relation, args)
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
relation.find_all do |role|
role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s
end
end
|
#find_or_create_by(role_name, resource_type = nil, resource_id = nil) ⇒ Object
44
45
46
47
48
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 44
def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
self.role_class.find_or_create_by(:name => role_name,
:resource_type => resource_type,
:resource_id => resource_id)
end
|
#remove(relation, role_name, resource = nil) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 54
def remove(relation, role_name, resource = nil)
cond = { :name => role_name }
cond[:resource_type] = (resource.is_a?(Class) ? resource.to_s : resource.class.name) if resource
cond[:resource_id] = resource.id if resource && !resource.is_a?(Class)
roles = relation.roles.where(cond)
roles.each do |role|
relation.roles.delete(role)
role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).delete(relation)
if Rolify.remove_role_if_empty && role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).empty?
role.destroy
end
end if roles
roles
end
|
#scope(relation, conditions) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 87
def scope(relation, conditions)
roles = where(role_class, conditions).map { |role| role.id }
return [] if roles.size.zero?
query = relation.any_in(:role_ids => roles)
query
end
|
#where(relation, *args) ⇒ Object
6
7
8
9
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 6
def where(relation, *args)
conditions = build_conditions(relation, args)
relation.any_of(*conditions)
end
|
#where_strict(relation, args) ⇒ Object
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/rolify/adapters/mongoid/role_adapter.rb', line 11
def where_strict(relation, args)
return relation.where(:name => args[:name]) if args[:resource].blank?
resource = if args[:resource].is_a?(Class)
{class: args[:resource].to_s, id: nil}
else
{class: args[:resource].class.name, id: args[:resource].id}
end
relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
end
|