Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

recover one boolean to indicate the association is one or many

  • Loading branch information...
commit 3160bcac153b149967c274068ad3ceb2ded4e477 1 parent d798684
@flyerhzm authored
Showing with 12 additions and 12 deletions.
  1. +12 −12 lib/mongoid-eager-loading/mongoid/criterion/eager_loading.rb
View
24 lib/mongoid-eager-loading/mongoid/criterion/eager_loading.rb
@@ -37,17 +37,17 @@ def association_reflection(document_class, eager_loading)
def setup_associations(documents, reflection)
case reflection.macro
when :references_one
- setup_associations_with_ids(documents, reflection)
+ setup_associations_with_ids(documents, reflection, true)
when :references_many
- setup_associations_with_ids(documents, reflection)
+ setup_associations_with_ids(documents, reflection, false)
when :references_and_referenced_in_many
- setup_associations_with_foreign_keys(documents, reflection)
+ setup_associations_with_foreign_keys(documents, reflection, false)
when :referenced_in
- setup_associations_with_foreign_keys(documents, reflection)
+ setup_associations_with_foreign_keys(documents, reflection, true)
end
end
- def setup_associations_with_ids(documents, reflection)
+ def setup_associations_with_ids(documents, reflection, one)
ids = association_ids(documents, reflection)
ignore_includes
@@ -56,10 +56,10 @@ def setup_associations_with_ids(documents, reflection)
add_id_association(eager_association.send(reflection.foreign_key), eager_association)
end
- assign_associations(documents, reflection)
+ assign_associations(documents, reflection, one)
end
- def setup_associations_with_foreign_keys(documents, reflection)
+ def setup_associations_with_foreign_keys(documents, reflection, one)
ids = association_ids(documents, reflection)
ignore_includes
@@ -68,7 +68,7 @@ def setup_associations_with_foreign_keys(documents, reflection)
add_id_association(eager_association.id, eager_association)
end
- assign_associations(documents, reflection)
+ assign_associations(documents, reflection, one)
end
def association_ids(documents, reflection)
@@ -84,15 +84,15 @@ def association_ids(documents, reflection)
ids
end
- def assign_associations(documents, reflection)
+ def assign_associations(documents, reflection, one)
id_documents_map.each do |id, documents|
documents.each do |document|
key_value = document.send(reflection.key)
associations = \
- if key_value.is_a?(Array)
- key_value.collect { |v| id_associations_map[v] }
- else
+ if one
id_associations_map[key_value] ? id_associations_map[key_value].first : nil
+ else
+ to_array(key_value).collect { |v| id_associations_map[v] }.compact.flatten
end
document.instance_variable_set("@#{reflection.name}", associations)
end
Please sign in to comment.
Something went wrong with that request. Please try again.