Skip to content
This repository
Browse code

Add more tests for the various ways we can assign objects to associat…

…ions. [#3513 state:resolved]

Get rid of a duplicate set_inverse_instance call if you use new_record(true) (e.g. you want to replace the existing instance).

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
commit 592085be83ed1b8ff0065a92490e54fcb1a8bc87 1 parent ed5b894
Murray Steele authored December 17, 2009 alloy committed December 28, 2009
3  activerecord/lib/active_record/associations/has_one_association.rb
@@ -121,10 +121,9 @@ def new_record(replace_existing)
121 121
           else
122 122
             record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
123 123
             self.target = record
  124
+            set_inverse_instance(record, @owner)
124 125
           end
125 126
 
126  
-          set_inverse_instance(record, @owner)
127  
-
128 127
           record
129 128
         end
130 129
 
170  activerecord/test/cases/associations/inverse_associations_test.rb
@@ -134,19 +134,84 @@ def test_parent_instance_should_be_shared_with_newly_created_child
134 134
     assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
135 135
   end
136 136
 
137  
-  def test_parent_instance_should_be_shared_with_replaced_child
138  
-    man = Man.find(:first)
139  
-    old_face = man.face
140  
-    new_face = Face.new
  137
+  def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method
  138
+    m = Man.find(:first)
  139
+    f = m.face.create!(:description => 'haunted')
  140
+    assert_not_nil f.man
  141
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  142
+    m.name = 'Bongo'
  143
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  144
+    f.man.name = 'Mungo'
  145
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  146
+  end
  147
+
  148
+  def test_parent_instance_should_be_shared_with_newly_built_child_when_we_dont_replace_existing
  149
+    m = Man.find(:first)
  150
+    f = m.build_face({:description => 'haunted'}, false)
  151
+    assert_not_nil f.man
  152
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  153
+    m.name = 'Bongo'
  154
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  155
+    f.man.name = 'Mungo'
  156
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
  157
+  end
  158
+
  159
+  def test_parent_instance_should_be_shared_with_newly_created_child_when_we_dont_replace_existing
  160
+    m = Man.find(:first)
  161
+    f = m.create_face({:description => 'haunted'}, false)
  162
+    assert_not_nil f.man
  163
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  164
+    m.name = 'Bongo'
  165
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  166
+    f.man.name = 'Mungo'
  167
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  168
+  end
  169
+
  170
+  def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method_when_we_dont_replace_existing
  171
+    m = Man.find(:first)
  172
+    f = m.face.create!({:description => 'haunted'}, false)
  173
+    assert_not_nil f.man
  174
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  175
+    m.name = 'Bongo'
  176
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  177
+    f.man.name = 'Mungo'
  178
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  179
+  end
141 180
 
142  
-    assert_not_nil man.face
143  
-    man.face.replace(new_face)
  181
+  def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
  182
+    m = Man.find(:first)
  183
+    f = Face.new(:description => 'haunted')
  184
+    m.face = f
  185
+    assert_not_nil f.man
  186
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  187
+    m.name = 'Bongo'
  188
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  189
+    f.man.name = 'Mungo'
  190
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
  191
+  end
144 192
 
145  
-    assert_equal man.name, new_face.man.name, "Name of man should be the same before changes to parent instance"
146  
-    man.name = 'Bongo'
147  
-    assert_equal man.name, new_face.man.name, "Name of man should be the same after changes to parent instance"
148  
-    new_face.man.name = 'Mungo'
149  
-    assert_equal man.name, new_face.man.name, "Name of man should be the same after changes to replaced-parent-owned instance"
  193
+  def test_parent_instance_should_be_shared_with_replaced_via_method_child
  194
+    m = Man.find(:first)
  195
+    f = Face.new(:description => 'haunted')
  196
+    m.face.replace(f)
  197
+    assert_not_nil f.man
  198
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  199
+    m.name = 'Bongo'
  200
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  201
+    f.man.name = 'Mungo'
  202
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
  203
+  end
  204
+
  205
+  def test_parent_instance_should_be_shared_with_replaced_via_method_child_when_we_dont_replace_existing
  206
+    m = Man.find(:first)
  207
+    f = Face.new(:description => 'haunted')
  208
+    m.face.replace(f, false)
  209
+    assert_not_nil f.man
  210
+    assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
  211
+    m.name = 'Bongo'
  212
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
  213
+    f.man.name = 'Mungo'
  214
+    assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
150 215
   end
151 216
 
152 217
   def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
@@ -203,6 +268,18 @@ def test_parent_instance_should_be_shared_with_newly_built_child
203 268
     assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
204 269
   end
205 270
 
  271
+  def test_parent_instance_should_be_shared_with_newly_block_style_built_child
  272
+    m = Man.find(:first)
  273
+    i = m.interests.build {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
  274
+    assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
  275
+    assert_not_nil i.man
  276
+    assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
  277
+    m.name = 'Bongo'
  278
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
  279
+    i.man.name = 'Mungo'
  280
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
  281
+  end
  282
+
206 283
   def test_parent_instance_should_be_shared_with_newly_created_child
207 284
     m = Man.find(:first)
208 285
     i = m.interests.create(:topic => 'Industrial Revolution Re-enactment')
@@ -214,6 +291,29 @@ def test_parent_instance_should_be_shared_with_newly_created_child
214 291
     assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
215 292
   end
216 293
 
  294
+  def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
  295
+    m = Man.find(:first)
  296
+    i = m.interests.create!(:topic => 'Industrial Revolution Re-enactment')
  297
+    assert_not_nil i.man
  298
+    assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
  299
+    m.name = 'Bongo'
  300
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
  301
+    i.man.name = 'Mungo'
  302
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  303
+  end
  304
+
  305
+  def test_parent_instance_should_be_shared_with_newly_block_style_created_child
  306
+    m = Man.find(:first)
  307
+    i = m.interests.create {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
  308
+    assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
  309
+    assert_not_nil i.man
  310
+    assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
  311
+    m.name = 'Bongo'
  312
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
  313
+    i.man.name = 'Mungo'
  314
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  315
+  end
  316
+
217 317
   def test_parent_instance_should_be_shared_with_poked_in_child
218 318
     m = Man.find(:first)
219 319
     i = Interest.create(:topic => 'Industrial Revolution Re-enactment')
@@ -226,6 +326,30 @@ def test_parent_instance_should_be_shared_with_poked_in_child
226 326
     assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
227 327
   end
228 328
 
  329
+  def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
  330
+    m = Man.find(:first)
  331
+    i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
  332
+    m.interests = [i]
  333
+    assert_not_nil i.man
  334
+    assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
  335
+    m.name = 'Bongo'
  336
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
  337
+    i.man.name = 'Mungo'
  338
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
  339
+  end
  340
+
  341
+  def test_parent_instance_should_be_shared_with_replaced_via_method_children
  342
+    m = Man.find(:first)
  343
+    i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
  344
+    m.interests.replace([i])
  345
+    assert_not_nil i.man
  346
+    assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
  347
+    m.name = 'Bongo'
  348
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
  349
+    i.man.name = 'Mungo'
  350
+    assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
  351
+  end
  352
+
229 353
   def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
230 354
     assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).secret_interests }
231 355
   end
@@ -298,6 +422,30 @@ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
298 422
     assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
299 423
   end
300 424
 
  425
+  def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
  426
+    f = Face.find(:first)
  427
+    m = Man.new(:name => 'Charles')
  428
+    f.man = m
  429
+    assert_not_nil m.face
  430
+    assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  431
+    f.description = 'gormless'
  432
+    assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  433
+    m.face.description = 'pleasing'
  434
+    assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
  435
+  end
  436
+
  437
+  def test_child_instance_should_be_shared_with_replaced_via_method_parent
  438
+    f = Face.find(:first)
  439
+    m = Man.new(:name => 'Charles')
  440
+    f.man.replace(m)
  441
+    assert_not_nil m.face
  442
+    assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
  443
+    f.description = 'gormless'
  444
+    assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
  445
+    m.face.description = 'pleasing'
  446
+    assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
  447
+  end
  448
+
301 449
   def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
302 450
     assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_man }
303 451
   end

0 notes on commit 592085b

Please sign in to comment.
Something went wrong with that request. Please try again.