Skip to content

Commit

Permalink
Add tests showing the LH issue #6381: fields_for with inline blocks a…
Browse files Browse the repository at this point in the history
…nd nested attributes already persisted

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
carlosantoniodasilva authored and spastorino committed Feb 8, 2011
1 parent cd44023 commit 631e23e
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion actionpack/test/template/form_helper_test.rb
Expand Up @@ -731,7 +731,7 @@ def test_form_for_with_method

assert_dom_equal expected, output_buffer
end

def test_form_for_with_search_field
# Test case for bug which would emit an "object" attribute
# when used with form_for using a search_field form helper
Expand Down Expand Up @@ -1084,6 +1084,44 @@ def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to
assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association_using_erb_and_inline_block
@post.author = Author.new(321)

form_for(@post) do |f|
concat f.text_field(:title)
concat f.fields_for(:author) { |af|
af.text_field(:name)
}
end

expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'put') do
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
'<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />'
end

assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association_using_erb_and_multiline_block
@post.author = Author.new(321)

form_for(@post) do |f|
concat f.text_field(:title)
concat f.fields_for(:author) { |af|
concat af.text_field(:name)
}
end

expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'put') do
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
'<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />'
end

assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement
@post.author = Author.new(321)

Expand Down Expand Up @@ -1127,6 +1165,52 @@ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collecti
assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_using_erb_and_inline_block
@post.comments = Array.new(2) { |id| Comment.new(id + 1) }

form_for(@post) do |f|
concat f.text_field(:title)
@post.comments.each do |comment|
concat f.fields_for(:comments, comment) { |cf|
cf.text_field(:name)
}
end
end

expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'put') do
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
'<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />'
end

assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_using_erb_and_multiline_block
@post.comments = Array.new(2) { |id| Comment.new(id + 1) }

form_for(@post) do |f|
concat f.text_field(:title)
@post.comments.each do |comment|
concat f.fields_for(:comments, comment) { |cf|
concat cf.text_field(:name)
}
end
end

expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'put') do
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
'<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />'
end

assert_dom_equal expected, output_buffer
end

def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement
@post.comments = Array.new(2) { |id| Comment.new(id + 1) }

Expand Down

0 comments on commit 631e23e

Please sign in to comment.