From df5db8991698432b27421942e5423d2b068b63f5 Mon Sep 17 00:00:00 2001 From: Jeff Bigler Date: Mon, 5 Apr 2010 13:11:22 -0700 Subject: [PATCH 1/2] Updated nested_attributes_test to include grandchildren --- activerecord/test/cases/nested_attributes_test.rb | 35 ++++++++++++++++----- activerecord/test/models/parrot.rb | 3 ++ activerecord/test/models/phrase.rb | 3 ++ activerecord/test/schema/schema.rb | 5 +++ 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 activerecord/test/models/phrase.rb diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 7ca9c41..34f811a 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -3,6 +3,7 @@ require "models/pirate" require "models/ship" require "models/bird" require "models/parrot" +require "models/phrase" require "models/treasure" require "models/man" require "models/interest" @@ -432,13 +433,19 @@ module NestedAttributesOnACollectionAssociationTests 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] + assert_equal ['Grace OMalley', 'Privateers Greed', 'Davy Jones'], [@child_1.reload.name, @child_2.reload.name, @child_3.reload.name] + if @association_name == :parrots then + assert_equal ['Walk the plank.', 'A pirates life for me.'], [@grandchild_1.reload.phrase, @grandchild_2.reload.phrase] + end end def test_should_take_an_array_and_assign_the_attributes_to_the_associated_models @pirate.send(association_setter, @alternate_params[association_getter].values) @pirate.save - assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name] + assert_equal ['Grace OMalley', 'Privateers Greed', 'Davy Jones'], [@child_1.reload.name, @child_2.reload.name, @child_3.reload.name] + if @association_name == :parrots then + assert_equal ['Walk the plank.', 'A pirates life for me.'], [@grandchild_1.reload.phrase, @grandchild_2.reload.phrase] + end end def test_should_also_work_with_a_HashWithIndifferentAccess @@ -450,7 +457,10 @@ module NestedAttributesOnACollectionAssociationTests def test_should_take_a_hash_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 + assert_equal 'Davy Jones', @pirate.send(@association_name).last.name + if @association_name == :parrots then + assert_equal 'Walk the plank.', @pirate.send(@association_name).last.phrases.first.phrase + end end def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models @@ -518,7 +528,7 @@ module NestedAttributesOnACollectionAssociationTests attributes['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'].to_set, @pirate.send(@association_name).map(&:name).to_set + assert_equal ['Posideons Killer', 'Killer bandita Dionne', 'Davy Jones', 'Privateers Greed', 'Grace OMalley'].to_set, @pirate.send(@association_name).map(&:name).to_set end def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed @@ -542,7 +552,7 @@ module NestedAttributesOnACollectionAssociationTests assert_difference('@pirate.send(@association_name).count', +1) do @pirate.update_attributes @alternate_params end - assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set + assert_equal ['Grace OMalley', 'Privateers Greed', 'Davy Jones', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set end def test_should_be_possible_to_destroy_a_record @@ -632,8 +642,9 @@ class TestNestedAttributesOnAHasManyAssociation < ActiveRecord::TestCase @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") @pirate.birds.create!(:name => 'Posideons Killer') @pirate.birds.create!(:name => 'Killer bandita Dionne') + @pirate.birds.create!(:name => 'Davy Jones') - @child_1, @child_2 = @pirate.birds + @child_1, @child_2, @child_3 = @pirate.birds @alternate_params = { :birds_attributes => { @@ -654,13 +665,21 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::Test @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") @pirate.parrots.create!(:name => 'Posideons Killer') @pirate.parrots.create!(:name => 'Killer bandita Dionne') + @pirate.parrots.create!(:name => 'Davy Jones') - @child_1, @child_2 = @pirate.parrots + @child_1, @child_2, @child_3 = @pirate.parrots + @child_3.phrases.create!(:phrase => 'Polly wants a cracker.') + @child_3.phrases.create!(:phrase => 'Get off my ship.') + @grandchild_1, @grandchild_2 = @child_3.phrases @alternate_params = { :parrots_attributes => { 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' }, - 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' } + 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' }, + 'bit' => { :id => @child_3.id, + :phrases_attributes => { + 'phrase1' => { :id => @grandchild_1.id, :phrase => 'Walk the plank.' }, + 'phrase2' => { :id => @grandchild_2.id, :phrase => 'A pirates life for me.' } } } } } end diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb index 737ef91..02d8677 100644 --- a/activerecord/test/models/parrot.rb +++ b/activerecord/test/models/parrot.rb @@ -3,10 +3,13 @@ class Parrot < ActiveRecord::Base has_and_belongs_to_many :pirates has_and_belongs_to_many :treasures has_many :loots, :as => :looter + has_many :phrases alias_attribute :title, :name validates_presence_of :name + accepts_nested_attributes_for :phrases + attr_accessor :cancel_save_from_callback before_save :cancel_save_callback_method, :if => :cancel_save_from_callback def cancel_save_callback_method diff --git a/activerecord/test/models/phrase.rb b/activerecord/test/models/phrase.rb new file mode 100644 index 0000000..445b6d4 --- /dev/null +++ b/activerecord/test/models/phrase.rb @@ -0,0 +1,3 @@ +class Phrase < ActiveRecord::Base + belongs_to :parrot +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index bec4291..007b179 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -373,6 +373,11 @@ ActiveRecord::Schema.define do t.column :updated_on, :datetime end + create_table :phrases, :force => true do |t| + t.string :phrase + t.integer :parrot_id + end + create_table :posts, :force => true do |t| t.integer :author_id t.string :title, :null => false -- 1.6.3.3 From a734cd7ec56fead0dcee426e799f3456493702d6 Mon Sep 17 00:00:00 2001 From: Jeff Bigler Date: Mon, 5 Apr 2010 13:17:53 -0700 Subject: [PATCH 2/2] Fixed order of schema --- activerecord/test/schema/schema.rb | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 007b179..0be656e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -366,6 +366,11 @@ ActiveRecord::Schema.define do t.integer :owner_id, :integer end + create_table :phrases, :force => true do |t| + t.string :phrase + t.integer :parrot_id + end + create_table :pirates, :force => true do |t| t.column :catchphrase, :string t.column :parrot_id, :integer @@ -373,11 +378,6 @@ ActiveRecord::Schema.define do t.column :updated_on, :datetime end - create_table :phrases, :force => true do |t| - t.string :phrase - t.integer :parrot_id - end - create_table :posts, :force => true do |t| t.integer :author_id t.string :title, :null => false -- 1.6.3.3