From 33eeab2aafd6bf08d3c60dbab35455b22b0170f2 Mon Sep 17 00:00:00 2001 From: Thomas McDonald Date: Thu, 13 Jun 2019 15:10:04 +0100 Subject: [PATCH] Enable short-circuiting in has_cached_role Rather than iterate through the entire roles array and then report if there are any values returned, we can instead use any? with a block that will short-circuit when the first truthy value is returned. This changes the call signature for RoleAdapter#find_cached* to return boolean rather than an array --- lib/rolify/adapters/active_record/role_adapter.rb | 6 +++--- lib/rolify/adapters/mongoid/role_adapter.rb | 6 +++--- lib/rolify/role.rb | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/rolify/adapters/active_record/role_adapter.rb b/lib/rolify/adapters/active_record/role_adapter.rb index 011a0411..ebc1bbe3 100644 --- a/lib/rolify/adapters/active_record/role_adapter.rb +++ b/lib/rolify/adapters/active_record/role_adapter.rb @@ -23,9 +23,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) @@ -36,7 +36,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 042a8b73..108ae76e 100644 --- a/lib/rolify/adapters/mongoid/role_adapter.rb +++ b/lib/rolify/adapters/mongoid/role_adapter.rb @@ -23,9 +23,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) @@ -36,7 +36,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)