Skip to content
Snippets Groups Projects
Commit 12b0bff0 authored by Mani Tadayon's avatar Mani Tadayon
Browse files

Use `Mongoid::Matchers#matches?` instead of a database query in...

Use `Mongoid::Matchers#matches?` instead of  a database query in `MongoidAdapter#matches_conditions_hash?`
parent bc9ecb22
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,9 @@ module CanCan ...@@ -10,7 +10,9 @@ module CanCan
end end
def self.matches_conditions_hash?(subject, conditions) def self.matches_conditions_hash?(subject, conditions)
subject.class.where(conditions).include?(subject) # TODO don't use a database query here for performance and other instances # To avoid hitting the db, retrieve the raw Mongo selector from
# the Mongoid Criteria and use Mongoid::Matchers#matches?
subject.matches?( subject.class.where(conditions).selector )
end end
def database_records def database_records
......
...@@ -84,7 +84,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid" ...@@ -84,7 +84,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
it "Calls where on the model class when there are criteria" do it "Calls where on the model class when there are criteria" do
obj = MongoidProject.create(:title => 'Bird') obj = MongoidProject.create(:title => 'Bird')
@conditions = {:title.nin => ["Fork", "Spoon"]} @conditions = {:title.nin => ["Fork", "Spoon"]}
mock(MongoidProject).where(@conditions) {[obj]}
@ability.can :read, MongoidProject, @conditions @ability.can :read, MongoidProject, @conditions
@ability.should be_able_to(:read, obj) @ability.should be_able_to(:read, obj)
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment