From cb4301ef44b615bef37d9707663457fa1c10206d Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Wed, 1 Jul 2009 14:48:11 -0700 Subject: [PATCH] Save grandparents when saving a parent association [#940 state:resolved] --- lib/dm-core/resource.rb | 2 +- spec/public/shared/resource_shared_spec.rb | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/dm-core/resource.rb b/lib/dm-core/resource.rb index 6e9b65f5..27716fbf 100644 --- a/lib/dm-core/resource.rb +++ b/lib/dm-core/resource.rb @@ -614,7 +614,7 @@ def save_self def save_parents parent_relationships.all? do |relationship| parent = relationship.get!(self) - if parent.save_self + if parent.save_parents && parent.save_self relationship.set(self, parent) # set the FK values end end diff --git a/spec/public/shared/resource_shared_spec.rb b/spec/public/shared/resource_shared_spec.rb index 491a0446..a73aa215 100644 --- a/spec/public/shared/resource_shared_spec.rb +++ b/spec/public/shared/resource_shared_spec.rb @@ -702,6 +702,50 @@ end + describe 'on a new object with unsaved parent and grandparent' do + before :all do + @grandparent = @user_model.new(:name => 'dkubb', :comment => @comment) + @parent = @user_model.new(:name => 'ashleymoran', :comment => @comment, :referrer => @grandparent) + @child = @user_model.new(:name => 'mrship', :comment => @comment, :referrer => @parent) + + @response = @child.save + end + + it 'should return true' do + @response.should be_true + end + + it 'should save the child' do + @child.should be_saved + end + + it 'should save the parent' do + @parent.should be_saved + end + + it 'should save the grandparent' do + @grandparent.should be_saved + end + + it 'should relate the child to the parent' do + pending_if 'TODO', @one_to_one_through do + @child.model.get(*@child.key).referrer.should == @parent + end + end + + it 'should relate the parent to the grandparent' do + pending_if 'TODO', @one_to_one_through do + @parent.model.get(*@parent.key).referrer.should == @grandparent + end + end + + it 'should relate the grandparent to nothing' do + pending_if 'TODO', @one_to_one_through do + @grandparent.model.get(*@grandparent.key).referrer.should be_nil + end + end + end + end it { @user.should respond_to(:saved?) }