Skip to content

Commit 33eeab2

Browse files
author
Thomas McDonald
committed
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
1 parent e196d70 commit 33eeab2

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

lib/rolify/adapters/active_record/role_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def find_cached(relation, args)
2323
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id
2424
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
2525

26-
return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any
26+
return relation.any? { |role| role.name == args[:name].to_s } if args[:resource] == :any
2727

28-
relation.find_all do |role|
28+
relation.any? do |role|
2929
(role.name == args[:name].to_s && role.resource_type == nil && role.resource_id == nil) ||
3030
(role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == nil) ||
3131
(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)
3636
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id
3737
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
3838

39-
relation.find_all do |role|
39+
relation.any? do |role|
4040
role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s
4141
end
4242
end

lib/rolify/adapters/mongoid/role_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def find_cached(relation, args)
2323
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id
2424
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
2525

26-
return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any
26+
return relation.any? { |role| role.name == args[:name].to_s } if args[:resource] == :any
2727

28-
relation.find_all do |role|
28+
relation.any? do |role|
2929
(role.name == args[:name].to_s && role.resource_type == nil && role.resource_id == nil) ||
3030
(role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == nil) ||
3131
(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)
3636
resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id
3737
resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
3838

39-
relation.find_all do |role|
39+
relation.any? do |role|
4040
role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s
4141
end
4242
end

lib/rolify/role.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def has_strict_role?(role_name, resource)
4646

4747
def has_cached_role?(role_name, resource = nil)
4848
return has_strict_cached_role?(role_name, resource) if self.class.strict_rolify and resource and resource != :any
49-
self.class.adapter.find_cached(self.roles, name: role_name, resource: resource).any?
49+
self.class.adapter.find_cached(self.roles, name: role_name, resource: resource)
5050
end
5151

5252
def has_strict_cached_role?(role_name, resource = nil)
53-
self.class.adapter.find_cached_strict(self.roles, name: role_name, resource: resource).any?
53+
self.class.adapter.find_cached_strict(self.roles, name: role_name, resource: resource)
5454
end
5555

5656
def has_all_roles?(*args)

0 commit comments

Comments
 (0)