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
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
2 changes: 1 addition & 1 deletion lib/cancan/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def matches_conditions?(action, subject, extra_args)
matches_conditions_hash?(subject)
else
# Don't stop at "cannot" definitions when there are conditions.
@conditions.empty? ? true : @base_behavior
conditions_empty? ? true : @base_behavior
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,17 @@
# adapter.matches_condition?(article1, :name.nlike, "%helo%").should be_true
# adapter.matches_condition?(article1, :name.nlike, "%ello worl%").should be_false
end

it 'should not execute a scope when checking ability on the class' do
relation = Article.where(:secret => true)
@ability.can :read, Article, relation do |article|
article.secret == true
end

# Ensure the ActiveRecord::Relation condition does not trigger a count query
stub(relation).count { fail 'Unexpected scope execution.' }

expect { @ability.can? :read, Article }.not_to raise_error
end
end
end