Permalink
Browse files

refactoring preload

  • Loading branch information...
1 parent 90fa60d commit 85cf957c35c02f6a5739975521c2c83fab438189 @flyerhzm committed Oct 9, 2010
Showing with 35 additions and 70 deletions.
  1. +34 −34 lib/mongoid/criterion/eager_loading.rb
  2. +1 −36 spec/unit/mongoid/criterion/eager_loading_spec.rb
View
68 lib/mongoid/criterion/eager_loading.rb
@@ -22,46 +22,46 @@ def preload(documents)
document_class = documents.first.class
@eager_loadings.each do |eager_loading|
reflection = association_reflection(document_class, eager_loading)
+ setup_associations(documents, reflection)
+ end
+ end
+
+ private
+ def association_reflection(document_class, eager_loading)
+ document_class.reflect_on_association(eager_loading)
+ end
+
+ def setup_associations(documents, reflection)
if reflection.association == Mongoid::Associations::ReferencesOne
- ids = documents.collect(&:id)
- eager_associations = reflection.name.singularize.camelize.constantize.where(reflection.foreign_key.to_sym.in => ids).to_a
- documents.each do |document|
- document.send("#{reflection.name}=", eager_associations.find { |eager_association|
- eager_association.send(reflection.foreign_key) == document.id
- })
- end
+ setup_associations_with_ids(documents, reflection, :find)
elsif reflection.association == Mongoid::Associations::ReferencesMany
- ids = documents.collect(&:id)
- eager_associations = reflection.name.singularize.camelize.constantize.where(reflection.foreign_key.to_sym.in => ids).to_a
- documents.each do |document|
- document.send("#{reflection.name}=", eager_associations.find_all { |eager_association|
- eager_association.send(reflection.foreign_key) == document.id
- })
- end
+ setup_associations_with_ids(documents, reflection, :find_all)
elsif reflection.association == Mongoid::Associations::ReferencesManyAsArray
- ids = documents.collect(&:"#{reflection.foreign_key}").flatten
- eager_associations = reflection.name.singularize.camelize.constantize.find(ids).to_a
- documents.each do |document|
- document.send("#{reflection.name}=", eager_associations.find_all { |eager_association|
- eager_association.id == document.send(reflection.foreign_key)
- })
- end
+ setup_associations_with_foreign_keys(documents, reflection, :find_all)
elsif reflection.association == Mongoid::Associations::ReferencedIn
- ids = documents.collect(&:"#{reflection.foreign_key}")
- eager_associations = reflection.name.singularize.camelize.constantize.find(ids).to_a
- documents.each do |document|
- document.send("#{reflection.name}=", eager_associations.find { |eager_association|
- eager_association.id == document.send(reflection.foreign_key)
- })
- end
- else
+ setup_associations_with_foreign_keys(documents, reflection, :find)
+ end
+ end
+
+ def setup_associations_with_ids(documents, reflection, method)
+ ids = documents.collect(&:id)
+ eager_associations = reflection.name.singularize.camelize.constantize.where(reflection.foreign_key.to_sym.in => ids).to_a
+ documents.each do |document|
+ document.send("#{reflection.name}=", eager_associations.send(method) { |eager_association|
+ eager_association.send(reflection.foreign_key) == document.id
+ })
+ end
+ end
+
+ def setup_associations_with_foreign_keys(documents, reflection, method)
+ ids = documents.collect(&:"#{reflection.foreign_key}").compact.flatten
+ eager_associations = reflection.name.singularize.camelize.constantize.find(ids).to_a
+ documents.each do |document|
+ document.send("#{reflection.name}=", eager_associations.send(method) { |eager_association|
+ eager_association.id == document.send(reflection.foreign_key)
+ })
end
end
- end
-
- def association_reflection(document_class, eager_loading)
- document_class.reflect_on_association(eager_loading)
- end
end
end
end
View
37 spec/unit/mongoid/criterion/eager_loading_spec.rb
@@ -55,7 +55,7 @@
criteria.preload([person1, person2])
end
- it "preload references_many association" do
+ it "preload references_many_as_array association" do
Preference.expects(:find).with((person1.preferences + person2.preferences).collect(&:id)).returns(person1.preferences + person2.preferences)
criteria = Mongoid::Criteria.new(Person)
@@ -71,39 +71,4 @@
criteria.preload([person1.game, person2.game])
end
end
-
- describe "#association_reflection" do
-
- it "with references_one" do
- criteria = Mongoid::Criteria.new(Person)
- reflection = criteria.association_reflection(Person, :game)
- reflection.association.should == Mongoid::Associations::ReferencesOne
- reflection.foreign_key.should == "person_id"
- reflection.name.should == "game"
- end
-
- it "with references_many" do
- criteria = Mongoid::Criteria.new(Person)
- reflection = criteria.association_reflection(Person, :posts)
- reflection.association.should == Mongoid::Associations::ReferencesMany
- reflection.foreign_key.should == "person_id"
- reflection.name.should == "posts"
- end
-
- it "with references_many_as_array" do
- criteria = Mongoid::Criteria.new(Person)
- reflection = criteria.association_reflection(Person, :preferences)
- reflection.association.should == Mongoid::Associations::ReferencesManyAsArray
- reflection.foreign_key.should == "preference_ids"
- reflection.name.should == "preferences"
- end
-
- it "with referenced_in" do
- criteria = Mongoid::Criteria.new(Game)
- reflection = criteria.association_reflection(Game, :person)
- reflection.association.should == Mongoid::Associations::ReferencedIn
- reflection.foreign_key.should == "person_id"
- reflection.name.should == "person"
- end
- end
end

0 comments on commit 85cf957

Please sign in to comment.