From a10f4c9c5d2df2384c21f2af44dcc46ffe5d1e1e Mon Sep 17 00:00:00 2001 From: Mike Breen Date: Sun, 29 Mar 2009 10:21:13 -0400 Subject: [PATCH] adds :new_if_blank option to fields_for that will initialize blank associations for models using nested_attributes_for --- actionpack/lib/action_view/helpers/form_helper.rb | 13 +++++++ actionpack/test/template/form_helper_test.rb | 38 ++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a59829b..9e23d62 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1004,11 +1004,24 @@ module ActionView @object.respond_to?("#{association_name}_attributes=") end + def build_nested_association(association_name, times) + klass = association_name.to_s.classify.constantize + if @object.send(association_name).is_a?(Array) + Array.new(times.is_a?(Numeric) ? times : 1) { klass.new } + else + klass.new + end + end + def fields_for_with_nested_attributes(association_name, args, block) name = "#{object_name}[#{association_name}_attributes]" association = @object.send(association_name) explicit_object = args.first if args.first.respond_to?(:new_record?) + if association.blank? && args.last.is_a?(Hash) && (times = args.last[:new_if_blank]) + association = build_nested_association(association_name, times) + end + if association.is_a?(Array) children = explicit_object ? [explicit_object] : association explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 654eee4..10a8522 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -456,7 +456,7 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end - + def test_nested_fields_for_with_nested_collections form_for('post[]', @post) do |f| concat f.text_field(:title) @@ -585,6 +585,42 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + + def test_nested_fields_for_one_to_one_association_with_new_if_blank_option + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:author, :new_if_blank => true) do |af| + concat af.text_field(:name) + end + end + expected = '
' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_new_if_blank_option_for_collection_association + @post.comments = [] + + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:comments, :new_if_blank => 3) do |cf| + concat cf.text_field(:name) + end + end + + expected = '
' + + '' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + + end def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association form_for(:post, @post) do |f| -- 1.6.1.3