Skip to content

Commit

Permalink
Support for _parent_document tracking on EmbeddedDocuments added to a…
Browse files Browse the repository at this point in the history
… collection
  • Loading branch information
Bodaniel Jeanes and Les Hill authored and jnunemaker committed Oct 2, 2009
1 parent 1bc21e4 commit 0563315
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/mongomapper/associations/many_embedded_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ def replace(v)
reset
end

def <<(*docs)
if load_target
parent = @owner._parent_document || @owner
docs.each do |doc|
doc._parent_document = parent
@target << doc
end
end
end
alias_method :push, :<<
alias_method :concat, :<<

protected
def find_target
(@_values || []).map do |e|
Expand Down
13 changes: 13 additions & 0 deletions test/functional/associations/test_many_embedded_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def setup
from_db.people.first.pets[1].species.should == "Dog"
end

should "create a reference to the parent document for all embedded documents before save" do
meg = Person.new(:name => "Meg")
sparky = Pet.new(:name => "Sparky", :species => "Dog")

doc = @document.new

doc.people << meg
meg.pets << sparky

doc.people.first._parent_document.should == doc
doc.people.first.pets.first._parent_document.should == doc
end

should "create a reference to the parent document for all embedded documents" do
meg = Person.new(:name => "Meg")
sparky = Pet.new(:name => "Sparky", :species => "Dog")
Expand Down

0 comments on commit 0563315

Please sign in to comment.