From ec8f04584479aff895b0b511a7ba1e9d33f84067 Mon Sep 17 00:00:00 2001
From: Eloy Duran <eloy.de.enige@gmail.com>
Date: Sun, 1 Feb 2009 14:44:30 +1300
Subject: Add support for nested object forms to ActiveRecord and the helpers
 in ActionPack

Signed-Off-By: Michael Koziarski <michael@koziarski.com>

[#1202 state:committed]
---
 .../test/cases/autosave_association_test.rb        | 386 +++++++++++++++++++++
 activerecord/test/cases/dirty_test.rb              |   2 +-
 activerecord/test/cases/nested_attributes_test.rb  | 359 +++++++++++++++++++
 activerecord/test/cases/reflection_test.rb         |   9 +
 4 files changed, 755 insertions(+), 1 deletion(-)
 create mode 100644 activerecord/test/cases/autosave_association_test.rb
 create mode 100644 activerecord/test/cases/nested_attributes_test.rb

(limited to 'activerecord/test/cases')

diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
new file mode 100644
index 0000000000..3c656b2430
--- /dev/null
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -0,0 +1,386 @@
+require "cases/helper"
+require "models/pirate"
+require "models/ship"
+require "models/ship_part"
+require "models/bird"
+require "models/parrot"
+require "models/treasure"
+
+class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
+  def test_autosave_should_be_a_valid_option_for_has_one
+    assert base.valid_keys_for_has_one_association.include?(:autosave)
+  end
+
+  def test_autosave_should_be_a_valid_option_for_belongs_to
+    assert base.valid_keys_for_belongs_to_association.include?(:autosave)
+  end
+
+  def test_autosave_should_be_a_valid_option_for_has_many
+    assert base.valid_keys_for_has_many_association.include?(:autosave)
+  end
+
+  def test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
+    assert base.valid_keys_for_has_and_belongs_to_many_association.include?(:autosave)
+  end
+
+  private
+
+  def base
+    ActiveRecord::Base
+  end
+end
+
+class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
+  self.use_transactional_fixtures = false
+
+  def setup
+    @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+  end
+
+  # reload
+  def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
+    @pirate.mark_for_destruction
+    @pirate.ship.mark_for_destruction
+
+    assert !@pirate.reload.marked_for_destruction?
+    assert !@pirate.ship.marked_for_destruction?
+  end
+
+  # has_one
+  def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
+    assert !@pirate.ship.marked_for_destruction?
+
+    @pirate.ship.mark_for_destruction
+    id = @pirate.ship.id
+
+    assert @pirate.ship.marked_for_destruction?
+    assert Ship.find_by_id(id)
+
+    @pirate.save
+    assert_nil @pirate.reload.ship
+    assert_nil Ship.find_by_id(id)
+  end
+
+  def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_child
+    # Stub the save method of the @pirate.ship instance to destroy and then raise an exception
+    class << @pirate.ship
+      def save(*args)
+        super
+        destroy
+        raise 'Oh noes!'
+      end
+    end
+
+    assert_raise(RuntimeError) { assert !@pirate.save }
+    assert_not_nil @pirate.reload.ship
+  end
+
+  # belongs_to
+  def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
+    assert !@ship.pirate.marked_for_destruction?
+
+    @ship.pirate.mark_for_destruction
+    id = @ship.pirate.id
+
+    assert @ship.pirate.marked_for_destruction?
+    assert Pirate.find_by_id(id)
+
+    @ship.save
+    assert_nil @ship.reload.pirate
+    assert_nil Pirate.find_by_id(id)
+  end
+
+  def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent
+    # Stub the save method of the @ship.pirate instance to destroy and then raise an exception
+    class << @ship.pirate
+      def save(*args)
+        super
+        destroy
+        raise 'Oh noes!'
+      end
+    end
+
+    assert_raise(RuntimeError) { assert !@ship.save }
+    assert_not_nil @ship.reload.pirate
+  end
+
+  # has_many & has_and_belongs_to
+  %w{ parrots birds }.each do |association_name|
+    define_method("test_should_destroy_#{association_name}_as_part_of_the_save_transaction_if_they_were_marked_for_destroyal") do
+      2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
+
+      assert !@pirate.send(association_name).any? { |child| child.marked_for_destruction? }
+
+      @pirate.send(association_name).each { |child| child.mark_for_destruction }
+      klass = @pirate.send(association_name).first.class
+      ids = @pirate.send(association_name).map(&:id)
+
+      assert @pirate.send(association_name).all? { |child| child.marked_for_destruction? }
+      ids.each { |id| assert klass.find_by_id(id) }
+
+      @pirate.save
+      assert @pirate.reload.send(association_name).empty?
+      ids.each { |id| assert_nil klass.find_by_id(id) }
+    end
+
+    define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
+      2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
+      before = @pirate.send(association_name).map { |c| c }
+
+      # Stub the save method of the first child to destroy and the second to raise an exception
+      class << before.first
+        def save(*args)
+          super
+          destroy
+        end
+      end
+      class << before.last
+        def save(*args)
+          super
+          raise 'Oh noes!'
+        end
+      end
+
+      assert_raise(RuntimeError) { assert !@pirate.save }
+      assert_equal before, @pirate.reload.send(association_name)
+    end
+  end
+end
+
+class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
+  self.use_transactional_fixtures = false
+
+  def setup
+    @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+  end
+
+  def test_should_still_work_without_an_associated_model
+    @ship.destroy
+    @pirate.reload.catchphrase = "Arr"
+    @pirate.save
+    assert 'Arr', @pirate.reload.catchphrase
+  end
+
+  def test_should_automatically_save_the_associated_model
+    @pirate.ship.name = 'The Vile Insanity'
+    @pirate.save
+    assert_equal 'The Vile Insanity', @pirate.reload.ship.name
+  end
+
+  def test_should_automatically_validate_the_associated_model
+    @pirate.ship.name = ''
+    assert !@pirate.valid?
+    assert !@pirate.errors.on(:ship_name).blank?
+  end
+
+  def test_should_still_allow_to_bypass_validations_on_the_associated_model
+    @pirate.catchphrase = ''
+    @pirate.ship.name = ''
+    @pirate.save(false)
+    assert_equal ['', ''], [@pirate.reload.catchphrase, @pirate.ship.name]
+  end
+
+  def test_should_allow_to_bypass_validations_on_associated_models_at_any_depth
+    2.times { |i| @pirate.ship.parts.create!(:name => "part #{i}") }
+
+    @pirate.catchphrase = ''
+    @pirate.ship.name = ''
+    @pirate.ship.parts.each { |part| part.name = '' }
+    @pirate.save(false)
+
+    values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
+    assert_equal ['', '', '', ''], values
+  end
+
+  def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
+    @pirate.ship.name = ''
+    assert_raise(ActiveRecord::RecordInvalid) do
+      @pirate.save!
+    end
+  end
+
+  def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
+    before = [@pirate.catchphrase, @pirate.ship.name]
+
+    @pirate.catchphrase = 'Arr'
+    @pirate.ship.name = 'The Vile Insanity'
+
+    # Stub the save method of the @pirate.ship instance to raise an exception
+    class << @pirate.ship
+      def save(*args)
+        super
+        raise 'Oh noes!'
+      end
+    end
+
+    assert_raise(RuntimeError) { assert !@pirate.save }
+    assert_equal before, [@pirate.reload.catchphrase, @pirate.ship.name]
+  end
+
+  def test_should_not_load_the_associated_model
+    assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
+  end
+end
+
+class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
+  self.use_transactional_fixtures = false
+
+  def setup
+    @ship = Ship.create(:name => 'Nights Dirty Lightning')
+    @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+  end
+
+  def test_should_still_work_without_an_associated_model
+    @pirate.destroy
+    @ship.reload.name = "The Vile Insanity"
+    @ship.save
+    assert 'The Vile Insanity', @ship.reload.name
+  end
+
+  def test_should_automatically_save_the_associated_model
+    @ship.pirate.catchphrase = 'Arr'
+    @ship.save
+    assert_equal 'Arr', @ship.reload.pirate.catchphrase
+  end
+
+  def test_should_automatically_validate_the_associated_model
+    @ship.pirate.catchphrase = ''
+    assert !@ship.valid?
+    assert !@ship.errors.on(:pirate_catchphrase).blank?
+  end
+
+  def test_should_still_allow_to_bypass_validations_on_the_associated_model
+    @ship.pirate.catchphrase = ''
+    @ship.name = ''
+    @ship.save(false)
+    assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase]
+  end
+
+  def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
+    @ship.pirate.catchphrase = ''
+    assert_raise(ActiveRecord::RecordInvalid) do
+      @ship.save!
+    end
+  end
+
+  def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
+    before = [@ship.pirate.catchphrase, @ship.name]
+
+    @ship.pirate.catchphrase = 'Arr'
+    @ship.name = 'The Vile Insanity'
+
+    # Stub the save method of the @ship.pirate instance to raise an exception
+    class << @ship.pirate
+      def save(*args)
+        super
+        raise 'Oh noes!'
+      end
+    end
+
+    assert_raise(RuntimeError) { assert !@ship.save }
+    # TODO: Why does using reload on @ship looses the associated pirate?
+    assert_equal before, [@ship.pirate.reload.catchphrase, @ship.reload.name]
+  end
+
+  def test_should_not_load_the_associated_model
+    assert_queries(1) { @ship.name = 'The Vile Insanity'; @ship.save! }
+  end
+end
+
+module AutosaveAssociationOnACollectionAssociationTests
+  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, @pirate.reload.send(@association_name).map(&:name)
+  end
+
+  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.on("#{@association_name}_name")
+    assert @pirate.errors.on(@association_name).blank?
+  end
+
+  def test_should_still_allow_to_bypass_validations_on_the_associated_models
+    @pirate.catchphrase = ''
+    @pirate.send(@association_name).each { |child| child.name = '' }
+
+    assert @pirate.save(false)
+    assert_equal ['', '', ''], [
+      @pirate.reload.catchphrase,
+      @pirate.send(@association_name).first.name,
+      @pirate.send(@association_name).last.name
+    ]
+  end
+
+  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
+
+  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
+
+  def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
+    assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
+
+    assert_queries(2) 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
+end
+
+class TestAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
+  self.use_transactional_fixtures = false
+
+  def setup
+    @association_name = :birds
+
+    @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @child_1 = @pirate.birds.create(:name => 'Posideons Killer')
+    @child_2 = @pirate.birds.create(:name => 'Killer bandita Dionne')
+  end
+
+  include AutosaveAssociationOnACollectionAssociationTests
+end
+
+class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
+  self.use_transactional_fixtures = false
+
+  def setup
+    @association_name = :parrots
+    @habtm = true
+
+    @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @child_1 = @pirate.parrots.create(:name => 'Posideons Killer')
+    @child_2 = @pirate.parrots.create(:name => 'Killer bandita Dionne')
+  end
+
+  include AutosaveAssociationOnACollectionAssociationTests
+end
\ No newline at end of file
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 1c9e281cc0..5f5707b388 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -166,7 +166,7 @@ class DirtyTest < ActiveRecord::TestCase
 
   def test_association_assignment_changes_foreign_key
     pirate = Pirate.create!(:catchphrase => 'jarl')
-    pirate.parrot = Parrot.create!
+    pirate.parrot = Parrot.create!(:name => 'Lorre')
     assert pirate.changed?
     assert_equal %w(parrot_id), pirate.changed
   end
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
new file mode 100644
index 0000000000..b982adc4cd
--- /dev/null
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -0,0 +1,359 @@
+require "cases/helper"
+require "models/pirate"
+require "models/ship"
+require "models/bird"
+require "models/parrot"
+require "models/treasure"
+
+module AssertRaiseWithMessage
+  def assert_raise_with_message(expected_exception, expected_message)
+    begin
+      error_raised = false
+      yield
+    rescue expected_exception => error
+      error_raised = true
+      actual_message = error.message
+    end
+    assert error_raised
+    assert_equal expected_message, actual_message
+  end
+end
+
+class TestNestedAttributesInGeneral < ActiveRecord::TestCase
+  include AssertRaiseWithMessage
+
+  def teardown
+    Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true
+  end
+
+  def test_base_should_have_an_empty_reject_new_nested_attributes_procs
+    assert_equal Hash.new, ActiveRecord::Base.reject_new_nested_attributes_procs
+  end
+
+  def test_should_add_a_proc_to_reject_new_nested_attributes_procs
+    [:parrots, :birds].each do |name|
+      assert_instance_of Proc, Pirate.reject_new_nested_attributes_procs[name]
+    end
+  end
+
+  def test_should_raise_an_ArgumentError_for_non_existing_associations
+    assert_raise_with_message ArgumentError, "No association found for name `honesty'. Has it been defined yet?" do
+      Pirate.accepts_nested_attributes_for :honesty
+    end
+  end
+
+  def test_should_disable_allow_destroy_by_default
+    Pirate.accepts_nested_attributes_for :ship
+
+    pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    ship = pirate.create_ship(:name => 'Nights Dirty Lightning')
+
+    assert_no_difference('Ship.count') do
+      pirate.update_attributes(:ship_attributes => { '_delete' => true })
+    end
+  end
+
+  def test_a_model_should_respond_to_underscore_delete_and_return_if_it_is_marked_for_destruction
+    ship = Ship.create!(:name => 'Nights Dirty Lightning')
+    assert !ship._delete
+    ship.mark_for_destruction
+    assert ship._delete
+  end
+end
+
+class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
+  def setup
+    @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+  end
+
+  def test_should_define_an_attribute_writer_method_for_the_association
+    assert_respond_to @pirate, :ship_attributes=
+  end
+
+  def test_should_automatically_instantiate_an_associated_model_if_there_is_none
+    @ship.destroy
+    @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
+
+    assert @pirate.ship.new_record?
+    assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+  end
+
+  def test_should_take_a_hash_and_assign_the_attributes_to_the_existing_associated_model
+    @pirate.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
+    assert !@pirate.ship.new_record?
+    assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+  end
+
+  def test_should_also_work_with_a_HashWithIndifferentAccess
+    @pirate.ship_attributes = HashWithIndifferentAccess.new(:name => 'Davy Jones Gold Dagger')
+    assert !@pirate.ship.new_record?
+    assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+  end
+
+  def test_should_work_with_update_attributes_as_well
+    @pirate.update_attributes({ :catchphrase => 'Arr', :ship_attributes => { :name => 'Mister Pablo' } })
+    @pirate.reload
+
+    assert_equal 'Arr', @pirate.catchphrase
+    assert_equal 'Mister Pablo', @pirate.ship.name
+  end
+
+  def test_should_be_possible_to_destroy_the_associated_model
+    @pirate.ship.destroy
+    ['1', 1, 'true', true].each do |true_variable|
+      @pirate.reload.create_ship(:name => 'Mister Pablo')
+      assert_difference('Ship.count', -1) do
+        @pirate.update_attributes(:ship_attributes => { '_delete' => true_variable })
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
+    [nil, '0', 0, 'false', false].each do |false_variable|
+      assert_no_difference('Ship.count') do
+        @pirate.update_attributes(:ship_attributes => { '_delete' => false_variable })
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+    assert_no_difference('Ship.count') do
+      @pirate.attributes = { :ship_attributes => { '_delete' => true } }
+    end
+    assert_difference('Ship.count', -1) { @pirate.save }
+  end
+
+  def test_should_automatically_enable_autosave_on_the_association
+    assert Pirate.reflect_on_association(:ship).options[:autosave]
+  end
+end
+
+class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
+  def setup
+    @ship = Ship.create!(:name => 'Nights Dirty Lightning')
+    @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+  end
+
+  def test_should_define_an_attribute_writer_method_for_the_association
+    assert_respond_to @ship, :pirate_attributes=
+  end
+
+  def test_should_automatically_instantiate_an_associated_model_if_there_is_none
+    @pirate.destroy
+    @ship.reload.pirate_attributes = { :catchphrase => 'Arr' }
+
+    assert @ship.pirate.new_record?
+    assert_equal 'Arr', @ship.pirate.catchphrase
+  end
+
+  def test_should_take_a_hash_and_assign_the_attributes_to_the_existing_associated_model
+    @ship.pirate_attributes = { :catchphrase => 'Arr' }
+    assert !@ship.pirate.new_record?
+    assert_equal 'Arr', @ship.pirate.catchphrase
+  end
+
+  def test_should_also_work_with_a_HashWithIndifferentAccess
+    @ship.pirate_attributes = HashWithIndifferentAccess.new(:catchphrase => 'Arr')
+    assert !@ship.pirate.new_record?
+    assert_equal 'Arr', @ship.pirate.catchphrase
+  end
+
+  def test_should_work_with_update_attributes_as_well
+    @ship.update_attributes({ :name => 'Mister Pablo', :pirate_attributes => { :catchphrase => 'Arr' } })
+    @ship.reload
+
+    assert_equal 'Mister Pablo', @ship.name
+    assert_equal 'Arr', @ship.pirate.catchphrase
+  end
+
+  def test_should_be_possible_to_destroy_the_associated_model
+    @ship.pirate.destroy
+    ['1', 1, 'true', true].each do |true_variable|
+      @ship.reload.create_pirate(:catchphrase => 'Arr')
+      assert_difference('Pirate.count', -1) do
+        @ship.update_attributes(:pirate_attributes => { '_delete' => true_variable })
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
+    [nil, '', '0', 0, 'false', false].each do |false_variable|
+      assert_no_difference('Pirate.count') do
+        @ship.update_attributes(:pirate_attributes => { '_delete' => false_variable })
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+    assert_no_difference('Pirate.count') do
+      @ship.attributes = { :pirate_attributes => { '_delete' => true } }
+    end
+    assert_difference('Pirate.count', -1) { @ship.save }
+  end
+
+  def test_should_automatically_enable_autosave_on_the_association
+    assert Ship.reflect_on_association(:pirate).options[:autosave]
+  end
+end
+
+module NestedAttributesOnACollectionAssociationTests
+  include AssertRaiseWithMessage
+
+  def test_should_define_an_attribute_writer_method_for_the_association
+    assert_respond_to @pirate, association_setter
+  end
+
+  def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models
+    @alternate_params[association_getter].stringify_keys!
+    @pirate.update_attributes @alternate_params
+    assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
+  end
+
+  def test_should_also_work_with_a_HashWithIndifferentAccess
+    @pirate.send(association_setter, HashWithIndifferentAccess.new(@child_1.id => HashWithIndifferentAccess.new(:name => 'Grace OMalley')))
+    @pirate.save
+    assert_equal 'Grace OMalley', @child_1.reload.name
+  end
+
+  def test_should_take_a_hash_with_integer_keys_and_assign_the_attributes_to_the_associated_models
+    @pirate.attributes = @alternate_params
+    assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
+    assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
+  end
+
+  def test_should_automatically_build_new_associated_models_for_each_entry_in_a_hash_where_the_id_starts_with_the_string_new_
+    @pirate.send(@association_name).destroy_all
+    @pirate.reload.attributes = { association_getter => { 'new_1' => { :name => 'Grace OMalley' }, 'new_2' => { :name => 'Privateers Greed' }}}
+
+    assert @pirate.send(@association_name).first.new_record?
+    assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
+
+    assert @pirate.send(@association_name).last.new_record?
+    assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
+  end
+
+  def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models
+    attributes = ActiveSupport::OrderedHash.new
+    attributes['new_123726353'] = { :name => 'Grace OMalley' }
+    attributes['new_2'] = { :name => 'Privateers Greed' } # 2 is lower then 123726353
+    @pirate.send(association_setter, attributes)
+
+    assert_equal ['Posideons Killer', 'Killer bandita Dionne', 'Privateers Greed', 'Grace OMalley'], @pirate.send(@association_name).map(&:name)
+  end
+
+  def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed
+    assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, {}) }
+    assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, ActiveSupport::OrderedHash.new) }
+
+    assert_raise_with_message ArgumentError, 'Hash expected, got String ("foo")' do
+      @pirate.send(association_setter, "foo")
+    end
+    assert_raise_with_message ArgumentError, 'Hash expected, got Array ([:foo, :bar])' do
+      @pirate.send(association_setter, [:foo, :bar])
+    end
+  end
+
+  def test_should_work_with_update_attributes_as_well
+    @pirate.update_attributes({ :catchphrase => 'Arr', association_getter => { @child_1.id => { :name => 'Grace OMalley' }}})
+    assert_equal 'Grace OMalley', @child_1.reload.name
+  end
+
+  def test_should_automatically_reject_any_new_record_if_a_reject_if_proc_exists_and_returns_false
+    @alternate_params[association_getter]["new_12345"] = {}
+    assert_no_difference("@pirate.send(@association_name).length") do
+      @pirate.attributes = @alternate_params
+    end
+  end
+
+  def test_should_update_existing_records_and_add_new_ones_that_have_an_id_that_start_with_the_string_new_
+    @alternate_params[association_getter]['new_12345'] = { :name => 'Buccaneers Servant' }
+    assert_difference('@pirate.send(@association_name).count', +1) do
+      @pirate.update_attributes @alternate_params
+    end
+    assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'], @pirate.reload.send(@association_name).map(&:name)
+  end
+
+  def test_should_be_possible_to_destroy_a_record
+    ['1', 1, 'true', true].each do |true_variable|
+      record = @pirate.reload.send(@association_name).create!(:name => 'Grace OMalley')
+      @pirate.send(association_setter,
+        @alternate_params[association_getter].merge(record.id => { '_delete' => true_variable })
+      )
+
+      assert_difference('@pirate.send(@association_name).count', -1) do
+        @pirate.save
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
+    [nil, '', '0', 0, 'false', false].each do |false_variable|
+      @alternate_params[association_getter][@child_1.id]['_delete'] = false_variable
+      assert_no_difference('@pirate.send(@association_name).count') do
+        @pirate.update_attributes(@alternate_params)
+      end
+    end
+  end
+
+  def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+    assert_no_difference('@pirate.send(@association_name).count') do
+      @pirate.send(association_setter, @alternate_params[association_getter].merge(@child_1.id => { '_delete' => true }))
+    end
+    assert_difference('@pirate.send(@association_name).count', -1) { @pirate.save }
+  end
+
+  def test_should_automatically_enable_autosave_on_the_association
+    assert Pirate.reflect_on_association(@association_name).options[:autosave]
+  end
+
+  private
+
+  def association_setter
+    @association_setter ||= "#{@association_name}_attributes=".to_sym
+  end
+
+  def association_getter
+    @association_getter ||= "#{@association_name}_attributes".to_sym
+  end
+end
+
+class TestNestedAttributesOnAHasManyAssociation < ActiveRecord::TestCase
+  def setup
+    @association_type = :has_many
+    @association_name = :birds
+
+    @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @child_1 = @pirate.birds.create!(:name => 'Posideons Killer')
+    @child_2 = @pirate.birds.create!(:name => 'Killer bandita Dionne')
+
+    @alternate_params = {
+      :birds_attributes => {
+        @child_1.id => { :name => 'Grace OMalley' },
+        @child_2.id => { :name => 'Privateers Greed' }
+      }
+    }
+  end
+
+  include NestedAttributesOnACollectionAssociationTests
+end
+
+class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
+  def setup
+    @association_type = :has_and_belongs_to_many
+    @association_name = :parrots
+
+    @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+    @child_1 = @pirate.parrots.create!(:name => 'Posideons Killer')
+    @child_2 = @pirate.parrots.create!(:name => 'Killer bandita Dionne')
+
+    @alternate_params = {
+      :parrots_attributes => {
+        @child_1.id => { :name => 'Grace OMalley' },
+        @child_2.id => { :name => 'Privateers Greed' }
+      }
+    }
+  end
+
+  include NestedAttributesOnACollectionAssociationTests
+end
\ No newline at end of file
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb
index e0ed3e5886..8b1c714ead 100644
--- a/activerecord/test/cases/reflection_test.rb
+++ b/activerecord/test/cases/reflection_test.rb
@@ -91,6 +91,15 @@ class ReflectionTest < ActiveRecord::TestCase
     assert_equal Money, Customer.reflect_on_aggregation(:balance).klass
   end
 
+  def test_reflect_on_all_autosave_associations
+    expected = Pirate.reflect_on_all_associations.select { |r| r.options[:autosave] }
+    received = Pirate.reflect_on_all_autosave_associations
+
+    assert !received.empty?
+    assert_not_equal Pirate.reflect_on_all_associations.length, received.length
+    assert_equal expected, received
+  end
+
   def test_has_many_reflection
     reflection_for_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :clients, { :order => "id", :dependent => :destroy }, Firm)
 
-- 
cgit v1.2.3