Skip to content

Commit

Permalink
Enable short-circuiting in has_cached_role
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thomas-mcdonald committed Jun 13, 2019
1 parent e196d70 commit 032dbc4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find_cached(relation, args)

return relation.find_all { |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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rolify/adapters/mongoid/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find_cached(relation, args)

return relation.find_all { |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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rolify/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 032dbc4

Please sign in to comment.