cancan icon indicating copy to clipboard operation
cancan copied to clipboard

Do Not Execute Scope When Checking Class Rule

Open matt-glover opened this issue 10 years ago • 4 comments

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.

matt-glover avatar Dec 08 '13 00:12 matt-glover

+1

inkstak avatar Dec 12 '13 07:12 inkstak

:+1:

bryanrite avatar Jan 27 '14 17:01 bryanrite

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!

xhoy avatar Jul 01 '14 07:07 xhoy

Looks like the change was already pulled into cancancan via https://github.com/CanCanCommunity/cancancan/commit/d3e4fd7665e9e3e891d7175816a15e8d283052db

matt-glover avatar Jul 01 '14 14:07 matt-glover