Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable short-circuiting in has_cached_role #560

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/rolify/adapters/mongoid/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
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