lib/rolify/adapters/mongoid/role_adapter.rb in rolify-4.1.1 vs lib/rolify/adapters/mongoid/role_adapter.rb in rolify-5.0.0
- old
+ new
@@ -16,9 +16,31 @@
end
relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
end
+ 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
+
+ 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
+
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