diff options
author | Jorge Bejar <jorge@wyeworks.com> | 2012-04-05 13:17:11 -0300 |
---|---|---|
committer | Jorge Bejar <jorge@wyeworks.com> | 2012-04-11 23:11:38 -0300 |
commit | 47cbfbb98a2a4ccb6d434998183f24be4e2bb1c1 (patch) | |
tree | b2031904d8aaf607dd1070d79d974c2d4732bd33 /actionpack/test | |
parent | 7ecd6a731bd60665bc6de94095137f0b2c4ada2a (diff) | |
download | rails-47cbfbb98a2a4ccb6d434998183f24be4e2bb1c1.tar.gz rails-47cbfbb98a2a4ccb6d434998183f24be4e2bb1c1.tar.bz2 rails-47cbfbb98a2a4ccb6d434998183f24be4e2bb1c1.zip |
Add index method to FormBuilder. Useful when you use field_for and need to know the index number into the iteration.
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index f13296a175..3fa3898ec8 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1835,6 +1835,56 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_index_method_with_existing_records_on_a_nested_attributes_collection_association + @post.comments = Array.new(2) { |id| Comment.new(id + 1) } + + form_for(@post) do |f| + expected = 0 + @post.comments.each do |comment| + f.fields_for(:comments, comment) { |cf| + assert_equal cf.index, expected + expected += 1 + } + end + end + end + + def test_nested_fields_for_index_method_with_existing_and_new_records_on_a_nested_attributes_collection_association + @post.comments = [Comment.new(321), Comment.new] + + form_for(@post) do |f| + expected = 0 + @post.comments.each do |comment| + f.fields_for(:comments, comment) { |cf| + assert_equal cf.index, expected + expected += 1 + } + end + end + end + + def test_nested_fields_for_index_method_with_existing_records_on_a_supplied_nested_attributes_collection + @post.comments = Array.new(2) { |id| Comment.new(id + 1) } + + form_for(@post) do |f| + expected = 0 + f.fields_for(:comments, @post.comments) { |cf| + assert_equal cf.index, expected + expected += 1 + } + end + end + + def test_nested_fields_for_index_method_with_child_index_option_override_on_a_nested_attributes_collection_association + @post.comments = [] + + form_for(@post) do |f| + f.fields_for(:comments, Comment.new(321), :child_index => 'abc') { |cf| + assert_equal cf.index, 'abc' + } + end + end + def test_nested_fields_uses_unique_indices_for_different_collection_associations @post.comments = [Comment.new(321)] @post.tags = [Tag.new(123), Tag.new(456)] |