module AutosaveAssociationOnACollectionAssociationTests

Public Instance Methods

test_should_allow_to_bypass_validations_on_the_associated_models_on_create() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1471
def test_should_allow_to_bypass_validations_on_the_associated_models_on_create
  assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", 2) do
    2.times { @pirate.send(@association_name).build }
    @pirate.save(validate: false)
  end
end
test_should_allow_to_bypass_validations_on_the_associated_models_on_update() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1443
def test_should_allow_to_bypass_validations_on_the_associated_models_on_update
  @pirate.catchphrase = ""
  @pirate.send(@association_name).each { |child| child.name = "" }

  assert @pirate.save(validate: false)
  # Oracle saves empty string as NULL
  if current_adapter?(:OracleAdapter)
    assert_equal [nil, nil, nil], [
      @pirate.reload.catchphrase,
      @pirate.send(@association_name).first.name,
      @pirate.send(@association_name).last.name
    ]
  else
    assert_equal ["", "", ""], [
      @pirate.reload.catchphrase,
      @pirate.send(@association_name).first.name,
      @pirate.send(@association_name).last.name
    ]
  end
end
test_should_automatically_save_bang_the_associated_models() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1377
def test_should_automatically_save_bang_the_associated_models
  new_names = ["Grace OMalley", "Privateers Greed"]
  @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }

  @pirate.save!
  assert_equal new_names.sort, @pirate.reload.send(@association_name).map(&:name).sort
end
test_should_automatically_save_the_associated_models() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1369
def test_should_automatically_save_the_associated_models
  new_names = ["Grace OMalley", "Privateers Greed"]
  @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }

  @pirate.save
  assert_equal new_names.sort, @pirate.reload.send(@association_name).map(&:name).sort
end
test_should_automatically_validate_the_associated_models() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1403
def test_should_automatically_validate_the_associated_models
  @pirate.send(@association_name).each { |child| child.name = "" }

  assert !@pirate.valid?
  assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
  assert @pirate.errors[@association_name].empty?
end
test_should_default_invalid_error_from_i18n() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1419
def test_should_default_invalid_error_from_i18n
  I18n.backend.store_translations(:en, activerecord: { errors: { models:
    { @associated_model_name.to_s.to_sym => { blank: "cannot be blank" } }
  } })

  @pirate.send(@association_name).build(name: "")

  assert !@pirate.valid?
  assert_equal ["cannot be blank"], @pirate.errors["#{@association_name}.name"]
  assert_equal ["#{@association_name.to_s.humanize} name cannot be blank"], @pirate.errors.full_messages
  assert @pirate.errors[@association_name].empty?
ensure
  I18n.backend = I18n::Backend::Simple.new
end
test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1434
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
  @pirate.send(@association_name).each { |child| child.name = "" }
  @pirate.catchphrase = nil

  assert !@pirate.valid?
  assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
  assert @pirate.errors[:catchphrase].any?
end
test_should_not_load_the_associated_models_if_they_were_not_loaded_yet() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1524
def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
  assert_queries(1) { @pirate.catchphrase = "Arr"; @pirate.save! }

  @pirate.send(@association_name).load_target

  assert_queries(3) do
    @pirate.catchphrase = "Yarr"
    new_names = ["Grace OMalley", "Privateers Greed"]
    @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
    @pirate.save!
  end
end
test_should_not_save_and_return_false_if_a_callback_cancelled_saving_in_either_create_or_update() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1478
def test_should_not_save_and_return_false_if_a_callback_cancelled_saving_in_either_create_or_update
  @pirate.catchphrase = "Changed"
  @child_1.name = "Changed"
  @child_1.cancel_save_from_callback = true

  assert !@pirate.save
  assert_equal "Don' botharrr talkin' like one, savvy?", @pirate.reload.catchphrase
  assert_equal "Posideons Killer", @child_1.reload.name

  new_pirate = Pirate.new(catchphrase: "Arr")
  new_child = new_pirate.send(@association_name).build(name: "Grace OMalley")
  new_child.cancel_save_from_callback = true

  assert_no_difference "Pirate.count" do
    assert_no_difference "#{new_child.class.name}.count" do
      assert !new_pirate.save
    end
  end
end
test_should_not_update_children_when_parent_creation_with_no_reason() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1395
def test_should_not_update_children_when_parent_creation_with_no_reason
  parrot = Parrot.create!(name: "Polly")
  assert_equal 0, parrot.updated_count

  Pirate.create!(parrot_ids: [parrot.id], catchphrase: "Arrrr")
  assert_equal 0, parrot.reload.updated_count
end
test_should_not_use_default_invalid_error_on_associated_models() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1411
def test_should_not_use_default_invalid_error_on_associated_models
  @pirate.send(@association_name).build(name: "")

  assert !@pirate.valid?
  assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
  assert @pirate.errors[@association_name].empty?
end
test_should_rollback_any_changes_if_an_exception_occurred_while_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1498
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
  before = [@pirate.catchphrase, *@pirate.send(@association_name).map(&:name)]
  new_names = ["Grace OMalley", "Privateers Greed"]

  @pirate.catchphrase = "Arr"
  @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }

  # Stub the save method of the first child instance to raise an exception
  class << @pirate.send(@association_name).first
    def save(*args)
      super
      raise "Oh noes!"
    end
  end

  assert_raise(RuntimeError) { assert !@pirate.save }
  assert_equal before, [@pirate.reload.catchphrase, *@pirate.send(@association_name).map(&:name)]
end
test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1517
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
  @pirate.send(@association_name).each { |child| child.name = "" }
  assert_raise(ActiveRecord::RecordInvalid) do
    @pirate.save!
  end
end
test_should_update_children_when_autosave_is_true_and_parent_is_new_but_child_is_not() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1385
def test_should_update_children_when_autosave_is_true_and_parent_is_new_but_child_is_not
  parrot = Parrot.create!(name: "Polly")
  parrot.name = "Squawky"
  pirate = Pirate.new(parrots: [parrot], catchphrase: "Arrrr")

  pirate.save!

  assert_equal "Squawky", parrot.reload.name
end
test_should_validation_the_associated_models_on_create() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1464
def test_should_validation_the_associated_models_on_create
  assert_no_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count") do
    2.times { @pirate.send(@association_name).build }
    @pirate.save
  end
end