class TestDefaultAutosaveAssociationOnAHasManyAssociationWithAcceptsNestedAttributes

Public Instance Methods

test_errors_details_should_be_indexed_when_global_flag_is_set() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 465
def test_errors_details_should_be_indexed_when_global_flag_is_set
  old_attribute_config = ActiveRecord::Base.index_nested_attribute_errors
  ActiveRecord::Base.index_nested_attribute_errors = true

  molecule = Molecule.new
  valid_electron = Electron.new(name: "electron")
  invalid_electron = Electron.new

  molecule.electrons = [valid_electron, invalid_electron]

  assert_not invalid_electron.valid?
  assert valid_electron.valid?
  assert_not molecule.valid?
  assert_equal [{ error: :blank }], molecule.errors.details[:"electrons[1].name"]
  assert_equal [], molecule.errors.details[:"electrons.name"]
ensure
  ActiveRecord::Base.index_nested_attribute_errors = old_attribute_config
end
test_errors_details_should_be_indexed_when_passed_as_array() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 450
def test_errors_details_should_be_indexed_when_passed_as_array
  guitar = Guitar.new
  tuning_peg_valid = TuningPeg.new
  tuning_peg_valid.pitch = 440.0
  tuning_peg_invalid = TuningPeg.new

  guitar.tuning_pegs = [tuning_peg_valid, tuning_peg_invalid]

  assert_not tuning_peg_invalid.valid?
  assert tuning_peg_valid.valid?
  assert_not guitar.valid?
  assert_equal [{ error: :not_a_number, value: nil }], guitar.errors.details[:"tuning_pegs[1].pitch"]
  assert_equal [], guitar.errors.details[:"tuning_pegs.pitch"]
end
test_errors_details_should_be_set() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 437
def test_errors_details_should_be_set
  molecule = Molecule.new
  valid_electron = Electron.new(name: "electron")
  invalid_electron = Electron.new

  molecule.electrons = [valid_electron, invalid_electron]

  assert_not invalid_electron.valid?
  assert valid_electron.valid?
  assert_not molecule.valid?
  assert_equal [{ error: :blank }], molecule.errors.details[:"electrons.name"]
end
test_errors_should_be_indexed_when_global_flag_is_set() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 418
def test_errors_should_be_indexed_when_global_flag_is_set
  old_attribute_config = ActiveRecord::Base.index_nested_attribute_errors
  ActiveRecord::Base.index_nested_attribute_errors = true

  molecule = Molecule.new
  valid_electron = Electron.new(name: "electron")
  invalid_electron = Electron.new

  molecule.electrons = [valid_electron, invalid_electron]

  assert_not invalid_electron.valid?
  assert valid_electron.valid?
  assert_not molecule.valid?
  assert_equal ["can't be blank"], molecule.errors["electrons[1].name"]
  assert_not_equal ["can't be blank"], molecule.errors["electrons.name"]
ensure
  ActiveRecord::Base.index_nested_attribute_errors = old_attribute_config
end
test_errors_should_be_indexed_when_passed_as_array() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 403
def test_errors_should_be_indexed_when_passed_as_array
  guitar = Guitar.new
  tuning_peg_valid = TuningPeg.new
  tuning_peg_valid.pitch = 440.0
  tuning_peg_invalid = TuningPeg.new

  guitar.tuning_pegs = [tuning_peg_valid, tuning_peg_invalid]

  assert_not tuning_peg_invalid.valid?
  assert tuning_peg_valid.valid?
  assert_not guitar.valid?
  assert_equal ["is not a number"], guitar.errors["tuning_pegs[1].pitch"]
  assert_not_equal ["is not a number"], guitar.errors["tuning_pegs.pitch"]
end
test_invalid_adding_with_nested_attributes() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 390
def test_invalid_adding_with_nested_attributes
  molecule = Molecule.new
  valid_electron = Electron.new(name: "electron")
  invalid_electron = Electron.new

  molecule.electrons = [valid_electron, invalid_electron]
  molecule.save

  assert_not invalid_electron.valid?
  assert valid_electron.valid?
  assert_not molecule.persisted?, "Molecule should not be persisted when its electrons are invalid"
end
test_valid_adding_with_nested_attributes() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 484
def test_valid_adding_with_nested_attributes
  molecule = Molecule.new
  valid_electron = Electron.new(name: "electron")

  molecule.electrons = [valid_electron]
  molecule.save

  assert valid_electron.valid?
  assert molecule.persisted?
  assert_equal 1, molecule.electrons.count
end