Skip to content

Commit

Permalink
Allow eager loading to work as a default scope. Fixes #1676.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 6, 2012
1 parent 272159f commit 3a1e88a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -71,6 +71,8 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#1676 Allow eager loading to work as a default scope.

* \#1665/#1672 Expand complex criteria in nested criteria selectors, like
#matches. (Hans Hasselberg)

Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/criteria.rb
Expand Up @@ -262,6 +262,7 @@ def as_conditions
scope_options = @options.dup
sorting = scope_options.delete(:sort)
scope_options[:order_by] = sorting if sorting
scope_options[:includes] = inclusions.map(&:name) if inclusions.any?
{ :where => @selector }.merge(scope_options)
end
alias :to_ary :to_a
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criterion/inclusion.rb
Expand Up @@ -190,7 +190,7 @@ def in(attributes = {})
#
# @since 2.2.0
def includes(*relations)
relations.each do |name|
relations.flatten.each do |name|
inclusions.push(klass.reflect_on_association(name))
end
clone
Expand Down
123 changes: 123 additions & 0 deletions spec/mongoid/criterion/inclusion_spec.rb
Expand Up @@ -636,6 +636,129 @@
Person.create
end

context "when providing inclusions to the default scope" do

before do
Person.default_scope(Person.includes(:posts))
end

after do
Person.default_scoping = nil
end

let!(:post_one) do
person.posts.create(:title => "one")
end

let!(:post_two) do
person.posts.create(:title => "two")
end

context "when the criteria has no options" do

before do
Mongoid::IdentityMap.clear
end

let!(:criteria) do
Person.all.entries
end

it "returns the correct documents" do
criteria.should eq([ person ])
end

it "inserts the first document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
end

it "inserts the second document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
end
end

context "when calling first on the criteria" do

before do
Mongoid::IdentityMap.clear
end

let!(:from_db) do
Person.first
end

it "returns the correct documents" do
from_db.should eq(person)
end

it "inserts the first document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
end

it "inserts the second document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
end
end

context "when calling last on the criteria" do

before do
Mongoid::IdentityMap.clear
end

let!(:from_db) do
Person.last
end

it "returns the correct documents" do
from_db.should eq(person)
end

it "inserts the first document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
end

it "inserts the second document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
end
end

context "when the criteria has limiting options" do

let!(:person_two) do
Person.create
end

let!(:post_three) do
person_two.posts.create(:title => "three")
end

before do
Mongoid::IdentityMap.clear
end

let!(:criteria) do
Person.asc(:_id).limit(1).entries
end

it "returns the correct documents" do
criteria.should eq([ person ])
end

it "inserts the first document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_one.id].should eq(post_one)
end

it "inserts the second document into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_two.id].should eq(post_two)
end

it "does not insert the third post into the identity map" do
Mongoid::IdentityMap[Post.collection_name][post_three.id].should be_nil
end
end
end

context "when including a has and belongs to many" do

let!(:preference_one) do
Expand Down

0 comments on commit 3a1e88a

Please sign in to comment.