From ec8f04584479aff895b0b511a7ba1e9d33f84067 Mon Sep 17 00:00:00 2001 From: Eloy Duran 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 [#1202 state:committed] --- actionpack/test/template/form_helper_test.rb | 141 ++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 5 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 9454fd7e91..33a542af7e 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -15,21 +15,31 @@ silence_warnings do def new_record? @new_record end + + attr_accessor :author + def author_attributes=(attributes); end + + attr_accessor :comments + def comments_attributes=(attributes); end end class Comment attr_reader :id attr_reader :post_id + def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end def save; @id = 1; @post_id = 1 end def new_record?; @id.nil? end def to_param; @id; end def name - @id.nil? ? 'new comment' : "comment ##{@id}" + @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" end end -end -class Comment::Nested < Comment; end + class Author < Comment + attr_accessor :post + def post_attributes=(attributes); end + end +end class FormHelperTest < ActionView::TestCase tests ActionView::Helpers::FormHelper @@ -479,7 +489,7 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end - def test_nested_fields_for_with_index + def test_form_for_with_index_and_nested_fields_for form_for(:post, @post, :index => 1) do |f| f.fields_for(:comment, @post) do |c| concat c.text_field(:title) @@ -558,6 +568,127 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association + @post.author = Author.new + + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:author) do |af| + concat af.text_field(:name) + end + end + + expected = '
' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association + @post.author = Author.new(321) + + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:author) do |af| + concat af.text_field(:name) + end + end + + expected = '
' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association + @post.comments = Array.new(2) { |id| Comment.new(id + 1) } + + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + f.fields_for(:comments, comment) do |cf| + concat cf.text_field(:name) + end + end + end + + expected = '
' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association + @post.comments = [Comment.new, Comment.new] + + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + f.fields_for(:comments, comment) do |cf| + concat cf.text_field(:name) + end + end + end + + expected = '
' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association + @post.comments = [Comment.new(321), Comment.new] + + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + f.fields_for(:comments, comment) do |cf| + concat cf.text_field(:name) + end + end + end + + expected = '
' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder + @post.comments = [Comment.new(321), Comment.new] + yielded_comments = [] + + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:comments) do |cf| + concat cf.text_field(:name) + yielded_comments << cf.object + end + end + + expected = '
' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + assert_equal yielded_comments, @post.comments + end + def test_fields_for fields_for(:post, @post) do |f| concat f.text_field(:title) @@ -974,4 +1105,4 @@ class FormHelperTest < ActionView::TestCase def protect_against_forgery? false end -end +end \ No newline at end of file -- cgit v1.2.3