diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-02-08 19:05:42 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-02-08 19:05:42 -0200 |
commit | 3c2fe5ccc5e51c0c3133f471cb3f0ee882fabf54 (patch) | |
tree | 6ba56d2465d58c58a26509d372358f99a8c9e046 | |
parent | 77577149f71e1fa0df15dbc02ae7c33349dddba8 (diff) | |
parent | 1ea61cb9bc95bf3857012850ffb475836e5d88e8 (diff) | |
download | rails-3c2fe5ccc5e51c0c3133f471cb3f0ee882fabf54.tar.gz rails-3c2fe5ccc5e51c0c3133f471cb3f0ee882fabf54.tar.bz2 rails-3c2fe5ccc5e51c0c3133f471cb3f0ee882fabf54.zip |
Merge pull request #13974 from laurocaetano/test_case_for_autosave_with_accepts_nested_attributes
Add test case for autosave association with nested attributes.
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 29 | ||||
-rw-r--r-- | activerecord/test/models/electron.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/molecule.rb | 2 |
3 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 7a0c335627..c55dd685a1 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -17,6 +17,8 @@ require 'models/tag' require 'models/tagging' require 'models/treasure' require 'models/eye' +require 'models/electron' +require 'models/molecule' class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase def test_should_not_add_the_same_callbacks_multiple_times_for_has_one @@ -343,6 +345,33 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test end end +class TestDefaultAutosaveAssociationOnAHasManyAssociationWithAcceptsNestedAttributes < ActiveRecord::TestCase + 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 + + 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 +end + class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase fixtures :companies, :people diff --git a/activerecord/test/models/electron.rb b/activerecord/test/models/electron.rb index 35af9f679b..6fc270673f 100644 --- a/activerecord/test/models/electron.rb +++ b/activerecord/test/models/electron.rb @@ -1,3 +1,5 @@ class Electron < ActiveRecord::Base belongs_to :molecule + + validates_presence_of :name end diff --git a/activerecord/test/models/molecule.rb b/activerecord/test/models/molecule.rb index 69325b8d29..26870c8f88 100644 --- a/activerecord/test/models/molecule.rb +++ b/activerecord/test/models/molecule.rb @@ -1,4 +1,6 @@ class Molecule < ActiveRecord::Base belongs_to :liquid has_many :electrons + + accepts_nested_attributes_for :electrons end |