Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Do Not Execute Scope When Checking Class Rule #971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matt-glover
Copy link

I expect that a class level access can? check will not trigger a scope defined on the ability. The current code executes a count against the scope and drives logic based on the result. This pull patches the presumably inadvertent scope execution. If that scope execution is the expected behavior then this pull should be rejected and the docs should be clarified to make this behavior explicit.

For example, I expect the following will not trigger Foo.some_scoped_query:

# Ability.rb
can :read, Foo, Foo.some_scoped_query do |foo|
  foo.some_instance_check
end

# In some controller
can?(:read, Foo)

In the current code CanCan checks the conditions on a rule to see if they are empty. @conditions.empty? calls ActiveRecord::Relation#empty? when a scoped condition is provided. ActiveRecord::Relation#empty? will execute the scoped query as a count to see if any records are returned.

@conditions.empty? calls ActiveRecord::Relation#empty? when a scoped condition is provided. ActiveRecord::Relation#empty? will query the database while CanCan only needs to check if conditions are set on the rule.
@inkstak
Copy link

inkstak commented Dec 12, 2013

+1

1 similar comment
@bryanrite
Copy link

👍

@xhoy
Copy link

xhoy commented Jul 1, 2014

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013.
Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!

@matt-glover
Copy link
Author

Looks like the change was already pulled into cancancan via CanCanCommunity/cancancan@d3e4fd7

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants