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

Significantly optimize #without_role #500

Merged
merged 3 commits into from
Feb 27, 2019
Merged
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
2 changes: 1 addition & 1 deletion lib/rolify/adapters/active_record/resource_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def applied_roles(relation, children)

def all_except(resource, excluded_obj)
prime_key = resource.primary_key.to_sym
resource.where(prime_key => (resource.all - excluded_obj).map(&prime_key))
resource.where.not(prime_key => excluded_obj.pluck(prime_key))
end

private
Expand Down
10 changes: 2 additions & 8 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,13 @@ def exists?(relation, column)
end

def scope(relation, conditions)
if Rails.version < "4.0"
query = relation.scoped
else
query = relation.all
end
query = query.joins(:roles)
query = relation.joins(:roles)
query = where(query, conditions)
query
end

def all_except(user, excluded_obj)
prime_key = user.primary_key.to_sym
user.where(prime_key => (user.all - excluded_obj).map(&prime_key))
user.where.not(user.primary_key => excluded_obj)
end

private
Expand Down