class TestNestedAttributesOnABelongsToAssociation
Public Instance Methods
setup()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 413 def setup @ship = Ship.new(name: "Nights Dirty Lightning") @pirate = @ship.build_pirate(catchphrase: "Aye") @ship.save! end
test_should_automatically_enable_autosave_on_the_association()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 545 def test_should_automatically_enable_autosave_on_the_association assert Ship.reflect_on_association(:pirate).options[:autosave] end
test_should_build_a_new_record_if_there_is_no_id()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 423 def test_should_build_a_new_record_if_there_is_no_id @pirate.destroy @ship.reload.pirate_attributes = { catchphrase: "Arr" } assert !@ship.pirate.persisted? assert_equal "Arr", @ship.pirate.catchphrase end
test_should_create_new_model_when_nothing_is_there_and_update_only_is_true()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 549 def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true @pirate.delete @ship.reload.attributes = { update_only_pirate_attributes: { catchphrase: "Arr" } } assert !@ship.update_only_pirate.persisted? end
test_should_define_an_attribute_writer_method_for_the_association()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 419 def test_should_define_an_attribute_writer_method_for_the_association assert_respond_to @ship, :pirate_attributes= end
test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 489 def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy @ship.pirate.destroy [1, "1", true, "true"].each do |truth| pirate = @ship.reload.create_pirate(catchphrase: "Arr") @ship.update(pirate_attributes: { id: pirate.id, _destroy: truth }) assert_raise(ActiveRecord::RecordNotFound) { pirate.reload } end end
test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 575 def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction Ship.accepts_nested_attributes_for :update_only_pirate, update_only: true, allow_destroy: true @pirate.delete @pirate = @ship.create_update_only_pirate(catchphrase: "Aye") @ship.update(update_only_pirate_attributes: { catchphrase: "Arr", id: @pirate.id, _destroy: true }) assert_raise(ActiveRecord::RecordNotFound) { @pirate.reload } Ship.accepts_nested_attributes_for :update_only_pirate, update_only: true, allow_destroy: false end
test_should_modify_an_existing_record_if_there_is_a_matching_composite_id()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 481 def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id @pirate.stub(:id, "ABC1X") do @ship.pirate_attributes = { id: @pirate.id, catchphrase: "Arr" } assert_equal "Arr", @ship.pirate.catchphrase end end
test_should_modify_an_existing_record_if_there_is_a_matching_id()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 460 def test_should_modify_an_existing_record_if_there_is_a_matching_id @ship.reload.pirate_attributes = { id: @pirate.id, catchphrase: "Arr" } assert_equal @pirate, @ship.pirate assert_equal "Arr", @ship.pirate.catchphrase end
test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 438 def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false @pirate.destroy @ship.reload.pirate_attributes = {} assert_nil @ship.pirate end
test_should_not_build_a_new_record_if_there_is_no_id_and_destroy_is_truthy()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 431 def test_should_not_build_a_new_record_if_there_is_no_id_and_destroy_is_truthy @pirate.destroy @ship.reload.pirate_attributes = { catchphrase: "Arr", _destroy: "1" } assert_nil @ship.pirate end
test_should_not_destroy_an_existing_record_if_allow_destroy_is_false()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 519 def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false Ship.accepts_nested_attributes_for :pirate, allow_destroy: false, reject_if: proc(&:empty?) @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: "1" }) assert_nothing_raised { @ship.pirate.reload } ensure Ship.accepts_nested_attributes_for :pirate, allow_destroy: true, reject_if: proc(&:empty?) end
test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 512 def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy [nil, "0", 0, "false", false].each do |not_truth| @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: not_truth }) assert_nothing_raised { @ship.pirate.reload } end end
test_should_not_destroy_the_associated_model_until_the_parent_is_saved()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 536 def test_should_not_destroy_the_associated_model_until_the_parent_is_saved pirate = @ship.pirate @ship.attributes = { pirate_attributes: { :id => pirate.id, "_destroy" => true } } assert_nothing_raised { Pirate.find(pirate.id) } @ship.save assert_raise(ActiveRecord::RecordNotFound) { Pirate.find(pirate.id) } end
test_should_not_replace_an_existing_record_if_there_is_no_id_and_destroy_is_truthy()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 453 def test_should_not_replace_an_existing_record_if_there_is_no_id_and_destroy_is_truthy @ship.reload.pirate_attributes = { catchphrase: "Arr", _destroy: "1" } assert_equal @pirate, @ship.pirate assert_equal "Aye", @ship.pirate.catchphrase end
test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 467 def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record exception = assert_raise ActiveRecord::RecordNotFound do @ship.pirate_attributes = { id: 1234567890 } end assert_equal "Couldn't find Pirate with ID=1234567890 for Ship with ID=#{@ship.id}", exception.message end
test_should_replace_an_existing_record_if_there_is_no_id()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 445 def test_should_replace_an_existing_record_if_there_is_no_id @ship.reload.pirate_attributes = { catchphrase: "Arr" } assert !@ship.pirate.persisted? assert_equal "Arr", @ship.pirate.catchphrase assert_equal "Aye", @pirate.catchphrase end
test_should_take_a_hash_with_string_keys_and_update_the_associated_model()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 474 def test_should_take_a_hash_with_string_keys_and_update_the_associated_model @ship.reload.pirate_attributes = { "id" => @pirate.id, "catchphrase" => "Arr" } assert_equal @pirate, @ship.pirate assert_equal "Arr", @ship.pirate.catchphrase end
test_should_unset_association_when_an_existing_record_is_destroyed()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 498 def test_should_unset_association_when_an_existing_record_is_destroyed original_pirate_id = @ship.pirate.id @ship.update! pirate_attributes: { id: @ship.pirate.id, _destroy: true } assert_empty Pirate.where(id: original_pirate_id) assert_nil @ship.pirate_id assert_nil @ship.pirate @ship.reload assert_empty Pirate.where(id: original_pirate_id) assert_nil @ship.pirate_id assert_nil @ship.pirate end
test_should_update_existing_when_update_only_is_true_and_id_is_given()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 565 def test_should_update_existing_when_update_only_is_true_and_id_is_given @pirate.delete @pirate = @ship.create_update_only_pirate(catchphrase: "Aye") @ship.update(update_only_pirate_attributes: { catchphrase: "Arr", id: @pirate.id }) assert_equal "Arr", @pirate.reload.catchphrase assert_equal @pirate, @ship.reload.update_only_pirate end
test_should_update_existing_when_update_only_is_true_and_no_id_is_given()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 556 def test_should_update_existing_when_update_only_is_true_and_no_id_is_given @pirate.delete @pirate = @ship.create_update_only_pirate(catchphrase: "Aye") @ship.update(update_only_pirate_attributes: { catchphrase: "Arr" }) assert_equal "Arr", @pirate.reload.catchphrase assert_equal @pirate, @ship.reload.update_only_pirate end
test_should_work_with_update_as_well()
click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 528 def test_should_work_with_update_as_well @ship.update(name: "Mister Pablo", pirate_attributes: { catchphrase: "Arr" }) @ship.reload assert_equal "Mister Pablo", @ship.name assert_equal "Arr", @ship.pirate.catchphrase end