class TestNestedAttributesOnAHasOneAssociation

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 223
def setup
  @pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
  @ship = @pirate.create_ship(name: "Nights Dirty Lightning")
end
test_should_accept_update_only_option() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 366
def test_should_accept_update_only_option
  @pirate.update(update_only_ship_attributes: { id: @pirate.ship.id, name: "Mayflower" })
end
test_should_also_work_with_a_HashWithIndifferentAccess() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 335
def test_should_also_work_with_a_HashWithIndifferentAccess
  @pirate.ship_attributes = ActiveSupport::HashWithIndifferentAccess.new(id: @ship.id, name: "Davy Jones Gold Dagger")

  assert @pirate.ship.persisted?
  assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
end
test_should_automatically_enable_autosave_on_the_association() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 362
def test_should_automatically_enable_autosave_on_the_association
  assert Pirate.reflect_on_association(:ship).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 239
def test_should_build_a_new_record_if_there_is_no_id
  @ship.destroy
  @pirate.reload.ship_attributes = { name: "Davy Jones Gold Dagger" }

  assert !@pirate.ship.persisted?
  assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
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 370
def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
  @ship.delete

  @pirate.reload.update(update_only_ship_attributes: { name: "Mayflower" })

  assert_not_nil @pirate.ship
end
test_should_define_an_attribute_writer_method_for_the_association() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 235
def test_should_define_an_attribute_writer_method_for_the_association
  assert_respond_to @pirate, :ship_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 305
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
  @pirate.ship.destroy

  [1, "1", true, "true"].each do |truth|
    ship = @pirate.reload.create_ship(name: "Mister Pablo")
    @pirate.update(ship_attributes: { id: ship.id, _destroy: truth })

    assert_nil @pirate.reload.ship
    assert_raise(ActiveRecord::RecordNotFound) { Ship.find(ship.id) }
  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 398
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
  Pirate.accepts_nested_attributes_for :update_only_ship, update_only: true, allow_destroy: true
  @ship.delete
  @ship = @pirate.create_update_only_ship(name: "Nights Dirty Lightning")

  @pirate.update(update_only_ship_attributes: { name: "Mayflower", id: @ship.id, _destroy: true })

  assert_nil @pirate.reload.ship
  assert_raise(ActiveRecord::RecordNotFound) { Ship.find(@ship.id) }

  Pirate.accepts_nested_attributes_for :update_only_ship, 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 297
def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
  @ship.stub(:id, "ABC1X") do
    @pirate.ship_attributes = { id: @ship.id, name: "Davy Jones Gold Dagger" }

    assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
  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 276
def test_should_modify_an_existing_record_if_there_is_a_matching_id
  @pirate.reload.ship_attributes = { id: @ship.id, name: "Davy Jones Gold Dagger" }

  assert_equal @ship, @pirate.ship
  assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
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 254
def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false
  @ship.destroy
  @pirate.reload.ship_attributes = {}

  assert_nil @pirate.ship
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 247
def test_should_not_build_a_new_record_if_there_is_no_id_and_destroy_is_truthy
  @ship.destroy
  @pirate.reload.ship_attributes = { name: "Davy Jones Gold Dagger", _destroy: "1" }

  assert_nil @pirate.ship
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 325
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
  Pirate.accepts_nested_attributes_for :ship, allow_destroy: false, reject_if: proc(&:empty?)

  @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: "1" })

  assert_equal @ship, @pirate.reload.ship

  Pirate.accepts_nested_attributes_for :ship, 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 317
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
  [nil, "0", 0, "false", false].each do |not_truth|
    @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: not_truth })

    assert_equal @ship, @pirate.reload.ship
  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 350
def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
  @pirate.attributes = { ship_attributes: { id: @ship.id, _destroy: "1" } }

  assert !@pirate.ship.destroyed?
  assert @pirate.ship.marked_for_destruction?

  @pirate.save

  assert @pirate.ship.destroyed?
  assert_nil @pirate.reload.ship
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 269
def test_should_not_replace_an_existing_record_if_there_is_no_id_and_destroy_is_truthy
  @pirate.reload.ship_attributes = { name: "Davy Jones Gold Dagger", _destroy: "1" }

  assert_equal @ship, @pirate.ship
  assert_equal "Nights Dirty Lightning", @pirate.ship.name
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 283
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
  exception = assert_raise ActiveRecord::RecordNotFound do
    @pirate.ship_attributes = { id: 1234567890 }
  end
  assert_equal "Couldn't find Ship with ID=1234567890 for Pirate with ID=#{@pirate.id}", exception.message
end
test_should_raise_argument_error_if_trying_to_build_polymorphic_belongs_to() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 228
def test_should_raise_argument_error_if_trying_to_build_polymorphic_belongs_to
  exception = assert_raise ArgumentError do
    Treasure.new(name: "pearl", looter_attributes: { catchphrase: "Arrr" })
  end
  assert_equal "Cannot build association `looter'. Are you trying to build a polymorphic one-to-one association?", 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 261
def test_should_replace_an_existing_record_if_there_is_no_id
  @pirate.reload.ship_attributes = { name: "Davy Jones Gold Dagger" }

  assert !@pirate.ship.persisted?
  assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
  assert_equal "Nights Dirty Lightning", @ship.name
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 290
def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
  @pirate.reload.ship_attributes = { "id" => @ship.id, "name" => "Davy Jones Gold Dagger" }

  assert_equal @ship, @pirate.ship
  assert_equal "Davy Jones Gold Dagger", @pirate.ship.name
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 388
def test_should_update_existing_when_update_only_is_true_and_id_is_given
  @ship.delete
  @ship = @pirate.create_update_only_ship(name: "Nights Dirty Lightning")

  @pirate.update(update_only_ship_attributes: { name: "Mayflower", id: @ship.id })

  assert_equal "Mayflower", @ship.reload.name
  assert_equal @ship, @pirate.reload.ship
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 378
def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
  @ship.delete
  @ship = @pirate.create_update_only_ship(name: "Nights Dirty Lightning")

  @pirate.update(update_only_ship_attributes: { name: "Mayflower" })

  assert_equal "Mayflower", @ship.reload.name
  assert_equal @ship, @pirate.reload.ship
end
test_should_work_with_update_as_well() click to toggle source
# File activerecord/test/cases/nested_attributes_test.rb, line 342
def test_should_work_with_update_as_well
  @pirate.update(catchphrase: "Arr", ship_attributes: { id: @ship.id, name: "Mister Pablo" })
  @pirate.reload

  assert_equal "Arr", @pirate.catchphrase
  assert_equal "Mister Pablo", @pirate.ship.name
end