From e94caf0788df87b139e575f33cdeea12b06f2609 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 9 Oct 2009 15:37:10 +0100 Subject: Store entire options hash in the class var rather than just the reject_if proc for the nested attributes --- activerecord/test/cases/nested_attributes_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index e57e361520..2a2a19fd4c 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -29,13 +29,13 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } end - def test_base_should_have_an_empty_reject_new_nested_attributes_procs - assert_equal Hash.new, ActiveRecord::Base.reject_new_nested_attributes_procs + def test_base_should_have_an_empty_nested_attributes_options + assert_equal Hash.new, ActiveRecord::Base.nested_attributes_options end - def test_should_add_a_proc_to_reject_new_nested_attributes_procs + def test_should_add_a_proc_to_nested_attributes_options [:parrots, :birds, :birds_with_reject_all_blank].each do |name| - assert_instance_of Proc, Pirate.reject_new_nested_attributes_procs[name] + assert_instance_of Proc, Pirate.nested_attributes_options[name][:reject_if] end end -- cgit v1.2.3 From 68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 9 Oct 2009 16:08:11 +0100 Subject: Add a :limit option to specify the maximum number of records that can be processed by accepts_nested_attributes_for --- activerecord/test/cases/nested_attributes_test.rb | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 2a2a19fd4c..53fd168e1b 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -603,3 +603,33 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::Test include NestedAttributesOnACollectionAssociationTests end + +class TestNestedAttributesLimit < ActiveRecord::TestCase + def setup + Pirate.accepts_nested_attributes_for :parrots, :limit => 2 + + @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") + end + + def teardown + Pirate.accepts_nested_attributes_for :parrots, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } + end + + def test_limit_with_less_records + @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Big Big Love' } } } + assert_difference('Parrot.count') { @pirate.save! } + end + + def test_limit_with_number_exact_records + @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Lovely Day' }, 'bar' => { :name => 'Blown Away' } } } + assert_difference('Parrot.count', 2) { @pirate.save! } + end + + def test_limit_with_exceeding_records + assert_raises(ActiveRecord::NestedAttributes::TooManyRecords) do + @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Lovely Day' }, + 'bar' => { :name => 'Blown Away' }, + 'car' => { :name => 'The Happening' }} } + end + end +end -- cgit v1.2.3