Skip to content

Commit

Permalink
Refactor _type selector handling
Browse files Browse the repository at this point in the history
- pushed down to Contexts, which know if they need that selector
- does it need to be in Enumerable?
- stepping stone to agnostic Criteria
  • Loading branch information
leshill committed Feb 13, 2010
1 parent 8d2f7d3 commit d1cf1a8
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 64 deletions.
3 changes: 3 additions & 0 deletions lib/mongoid/contexts/enumerable.rb
Expand Up @@ -54,6 +54,9 @@ def execute(paginating = false)
# <tt>Mongoid::Contexts::Enumerable.new(criteria)</tt>
def initialize(criteria)
@criteria = criteria
if criteria.klass.hereditary
criteria.in(:_type => criteria.klass._types)
end
end

# Get the largest value for the field in all the documents.
Expand Down
3 changes: 3 additions & 0 deletions lib/mongoid/contexts/mongo.rb
Expand Up @@ -95,6 +95,9 @@ def group
# <tt>Mongoid::Contexts::Mongo.new(criteria)</tt>
def initialize(criteria)
@criteria = criteria
if criteria.klass.hereditary
criteria.in(:_type => criteria.klass._types)
end
end

# Return the last result for the +Context+. Essentially does a find_one on
Expand Down
3 changes: 0 additions & 3 deletions lib/mongoid/criteria.rb
Expand Up @@ -134,9 +134,6 @@ def fuse(criteria_conditions = {})
# klass: The class to execute on.
def initialize(klass)
@selector, @options, @klass, @documents = {}, {}, klass, []
if klass.hereditary
@selector = { :_type => { "$in" => klass._types } }
end
end

# Merges another object into this +Criteria+. The other object may be a
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/mongoid/contexts/enumerable_spec.rb
Expand Up @@ -130,6 +130,12 @@
@context.documents.should == documents
end

it "set the selector to query across the _type of the Criteria's klass when it is hereditary" do
criteria = Mongoid::Criteria.new(Person)
context = Mongoid::Contexts::Enumerable.new(criteria)
context.selector[:_type].should == {'$in' => Person._types}
end

end

describe "#last" do
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/mongoid/contexts/mongo_spec.rb
Expand Up @@ -164,6 +164,9 @@
@context.klass.should == klass
end

it "set the selector to query across the _type of the Criteria's klass when it is hereditary" do
@context.selector[:_type].should == {'$in' => Person._types}
end
end

describe "#last" do
Expand Down
45 changes: 21 additions & 24 deletions spec/unit/mongoid/criteria_spec.rb
Expand Up @@ -243,7 +243,7 @@
criteria = Mongoid::Criteria.new(Person)
collection = mock
Person.expects(:collection).returns(collection)
collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
collection.expects(:find).with(criteria.selector, criteria.options).returns([])
criteria.entries.should == []
end

Expand Down Expand Up @@ -370,24 +370,23 @@

describe "#initialize" do

context "when class is hereditary" do

it "sets the _type value on the selector" do
criteria = Mongoid::Criteria.new(Person)
criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] } }
end
let(:criteria) { Mongoid::Criteria.new(Person) }

it "sets the selector to an empty hash" do
criteria.selector.should == {}
end

context "when class is not hereditary" do

it "sets no _type value on the selector" do
criteria = Mongoid::Criteria.new(Game)
criteria.selector.should == {}
end
it "sets the options to an empty hash" do
criteria.options.should == {}
end

it "sets the documents to an empty array" do
criteria.documents.should == []
end

it "sets the klass to the given class" do
criteria.klass.should == Person
end
end

describe "#last" do
Expand Down Expand Up @@ -431,7 +430,7 @@
before do
@other = Mongoid::Criteria.new(Person)
@other.where(:name => "Chloe").order_by([[:name, :asc]])
@selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30, :name => "Chloe" }
@selector = { :title => "Sir", :age => 30, :name => "Chloe" }
@options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] }
end

Expand All @@ -447,7 +446,7 @@

before do
@other = Mongoid::Criteria.new(Person)
@selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30 }
@selector = { :title => "Sir", :age => 30 }
@options = { :skip => 40, :limit => 20 }
end

Expand Down Expand Up @@ -487,7 +486,7 @@

it "merges the criteria with the next one" do
@new_criteria = @criteria.accepted
@new_criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true }
@new_criteria.selector.should == { :title => "Sir", :terms => true }
end

context "chaining more than one scope" do
Expand All @@ -497,8 +496,7 @@
end

it "returns the final merged criteria" do
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
@criteria.selector.should == { :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
end

end
Expand Down Expand Up @@ -614,8 +612,7 @@
end

it "returns the selector plus the options" do
@criteria.scoped.should ==
{ :where => { :title => "Sir", :_type=>{ "$in" => [ "Doctor", "Person" ] } }, :skip => 20 }
@criteria.scoped.should == { :where => { :title => "Sir" }, :skip => 20 }
end

end
Expand Down Expand Up @@ -719,7 +716,7 @@
end

it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
@criteria.selector.should == { :title => "Test" }
end

it "returns a criteria with klass Person" do
Expand All @@ -735,7 +732,7 @@
end

it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
@criteria.selector.should == { :title => "Test" }
end

it "returns a criteria with klass Person" do
Expand All @@ -751,7 +748,7 @@
end

it "returns a criteria with a selector from the conditions" do
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
@criteria.selector.should == { :title => "Test" }
end

it "returns a criteria with klass Person" do
Expand All @@ -766,7 +763,7 @@
end

it "adds the criteria and the options" do
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
@criteria.selector.should == { :title => "Test" }
@criteria.options.should == { :skip => 10 }
end

Expand Down
16 changes: 3 additions & 13 deletions spec/unit/mongoid/criterion/exclusion_spec.rb
Expand Up @@ -12,10 +12,7 @@
it "adds the $ne query to the selector" do
@criteria.excludes(:title => "Bad Title", :text => "Bad Text")
@criteria.selector.should ==
{ :_type =>
{ "$in" =>
["Doctor", "Person"]
},
{
:title =>
{ "$ne" => "Bad Title"},
:text =>
Expand All @@ -32,21 +29,15 @@
it "accepts id" do
@criteria.excludes(:id => "1")
@criteria.selector.should ==
{ :_type =>
{ "$in" =>
["Doctor", "Person"]
},
{
:_id => { "$ne" => "1" }
}
end

it "accepts _id" do
@criteria.excludes(:_id => "1")
@criteria.selector.should ==
{ :_type =>
{ "$in" =>
["Doctor", "Person"]
},
{
:_id => { "$ne" => "1" }
}
end
Expand All @@ -60,7 +51,6 @@
it "adds the exclusion to the selector" do
@criteria.not_in(:title => ["title1", "title2"], :text => ["test"])
@criteria.selector.should == {
:_type => { "$in" => ["Doctor", "Person"] },
:title => { "$nin" => ["title1", "title2"] },
:text => { "$nin" => ["test"] }
}
Expand Down
36 changes: 17 additions & 19 deletions spec/unit/mongoid/criterion/inclusion_spec.rb
Expand Up @@ -12,7 +12,7 @@
it "adds the $all query to the selector" do
@criteria.all(:title => ["title1", "title2"])
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] },
{
:title => { "$all" => ["title1", "title2"] }
}
end
Expand All @@ -30,7 +30,7 @@
it "adds the clause to the selector" do
@criteria.and(:title => "Title", :text => "Text")
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] },
{
:title => "Title",
:text => "Text"
}
Expand All @@ -43,7 +43,7 @@
it "adds the $where clause to the selector" do
@criteria.and("this.date < new Date()")
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] },
{
"$where" => "this.date < new Date()"
}
end
Expand All @@ -61,10 +61,8 @@
it "adds the $in clause to the selector" do
@criteria.in(:title => ["title1", "title2"], :text => ["test"])
@criteria.selector.should ==
{ :_type =>
{ "$in" =>
["Doctor", "Person"]
}, :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
{
:title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
}
end

Expand All @@ -83,7 +81,7 @@
it "adds the clause to the selector" do
@criteria.where(:title => "Title", :text => "Text")
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Title", :text => "Text" }
{ :title => "Title", :text => "Text" }
end

end
Expand All @@ -95,7 +93,7 @@
it "returns those matching an all clause" do
@criteria.where(:title.all => ["Sir"])
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$all" => ["Sir"] } }
{ :title => { "$all" => ["Sir"] } }
end

end
Expand All @@ -105,7 +103,7 @@
it "returns those matching an exists clause" do
@criteria.where(:title.exists => true)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$exists" => true } }
{ :title => { "$exists" => true } }
end

end
Expand All @@ -115,7 +113,7 @@
it "returns those matching a gt clause" do
@criteria.where(:age.gt => 30)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gt" => 30 } }
{ :age => { "$gt" => 30 } }
end

end
Expand All @@ -125,7 +123,7 @@
it "returns those matching a gte clause" do
@criteria.where(:age.gte => 33)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gte" => 33 } }
{ :age => { "$gte" => 33 } }
end

end
Expand All @@ -135,7 +133,7 @@
it "returns those matching an in clause" do
@criteria.where(:title.in => ["Sir", "Madam"])
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$in" => ["Sir", "Madam"] } }
{ :title => { "$in" => ["Sir", "Madam"] } }
end

end
Expand All @@ -145,7 +143,7 @@
it "returns those matching a lt clause" do
@criteria.where(:age.lt => 34)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lt" => 34 } }
{ :age => { "$lt" => 34 } }
end

end
Expand All @@ -155,7 +153,7 @@
it "returns those matching a lte clause" do
@criteria.where(:age.lte => 33)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lte" => 33 } }
{ :age => { "$lte" => 33 } }
end

end
Expand All @@ -165,7 +163,7 @@
it "returns those matching a ne clause" do
@criteria.where(:age.ne => 50)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$ne" => 50 } }
{ :age => { "$ne" => 50 } }
end

end
Expand All @@ -175,7 +173,7 @@
it "returns those matching a nin clause" do
@criteria.where(:title.nin => ["Esquire", "Congressman"])
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$nin" => ["Esquire", "Congressman"] } }
{ :title => { "$nin" => ["Esquire", "Congressman"] } }
end

end
Expand All @@ -185,7 +183,7 @@
it "returns those matching a size clause" do
@criteria.where(:aliases.size => 2)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :aliases => { "$size" => 2 } }
{ :aliases => { "$size" => 2 } }
end

end
Expand All @@ -199,7 +197,7 @@
it "adds the $where clause to the selector" do
@criteria.where("this.date < new Date()")
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, "$where" => "this.date < new Date()" }
{ "$where" => "this.date < new Date()" }
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/mongoid/criterion/optional_spec.rb
Expand Up @@ -115,7 +115,7 @@
it "adds the _id query to the selector" do
id = Mongo::ObjectID.new.to_s
@criteria.id(id)
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :_id => id }
@criteria.selector.should == { :_id => id }
end

it "returns self" do
Expand All @@ -135,7 +135,7 @@
it "adds the _id query to the selector" do
@criteria.id(@ids)
@criteria.selector.should ==
{ :_type => { "$in" => ["Doctor", "Person"] }, :_id => { "$in" => @ids } }
{ :_id => { "$in" => @ids } }
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/extensions/proc/scoping_spec.rb
Expand Up @@ -24,7 +24,7 @@

it "returns the criteria scoped" do
@proc.scoped("Sir").should ==
{ :where => { :_type => { "$in" => [ "Doctor", "Person" ] }, :title => "Sir" } }
{ :where => { :title => "Sir" } }
end

end
Expand Down

0 comments on commit d1cf1a8

Please sign in to comment.