diff --git a/lib/rolify/adapters/active_record/role_adapter.rb b/lib/rolify/adapters/active_record/role_adapter.rb index a1f01fb3..83937bd5 100644 --- a/lib/rolify/adapters/active_record/role_adapter.rb +++ b/lib/rolify/adapters/active_record/role_adapter.rb @@ -29,9 +29,9 @@ 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 + return relation.any? { |role| role.name == args[:name].to_s } if args[:resource] == :any - relation.find_all do |role| + relation.any? 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) @@ -42,7 +42,7 @@ 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| + relation.any? do |role| role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s end end diff --git a/lib/rolify/adapters/mongoid/role_adapter.rb b/lib/rolify/adapters/mongoid/role_adapter.rb index a3fb0486..382bc199 100644 --- a/lib/rolify/adapters/mongoid/role_adapter.rb +++ b/lib/rolify/adapters/mongoid/role_adapter.rb @@ -29,9 +29,9 @@ 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 + return relation.any? { |role| role.name == args[:name].to_s } if args[:resource] == :any - relation.find_all do |role| + relation.any? 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) @@ -42,7 +42,7 @@ 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| + relation.any? do |role| role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s end end diff --git a/lib/rolify/role.rb b/lib/rolify/role.rb index 34f6f13a..a326eaa8 100644 --- a/lib/rolify/role.rb +++ b/lib/rolify/role.rb @@ -46,11 +46,11 @@ def has_strict_role?(role_name, resource) def has_cached_role?(role_name, resource = nil) return has_strict_cached_role?(role_name, resource) if self.class.strict_rolify and resource and resource != :any - self.class.adapter.find_cached(self.roles, name: role_name, resource: resource).any? + self.class.adapter.find_cached(self.roles, name: role_name, resource: resource) end def has_strict_cached_role?(role_name, resource = nil) - self.class.adapter.find_cached_strict(self.roles, name: role_name, resource: resource).any? + self.class.adapter.find_cached_strict(self.roles, name: role_name, resource: resource) end def has_all_roles?(*args)