From 21eba34718660b96b45cbb594d557b7e84bfed4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 31 Jan 2012 17:46:31 -0200 Subject: Add collection_radio_buttons and collection_check_boxes to FormBuilder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Carlos Antonio da Silva + Rafael Mendonça França] --- .../action_view/helpers/form_collections_helper.rb | 10 ++++++++ .../test/template/form_collections_helper_test.rb | 29 +++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_collections_helper.rb b/actionpack/lib/action_view/helpers/form_collections_helper.rb index 27b214f34c..9280902e7c 100644 --- a/actionpack/lib/action_view/helpers/form_collections_helper.rb +++ b/actionpack/lib/action_view/helpers/form_collections_helper.rb @@ -9,5 +9,15 @@ module ActionView Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block) end end + + class FormBuilder + def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) + @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) + end + + def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) + @template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) + end + end end end diff --git a/actionpack/test/template/form_collections_helper_test.rb b/actionpack/test/template/form_collections_helper_test.rb index 647fe47d04..995cfdae87 100644 --- a/actionpack/test/template/form_collections_helper_test.rb +++ b/actionpack/test/template/form_collections_helper_test.rb @@ -179,6 +179,20 @@ class FormCollectionsHelperTest < ActionView::TestCase assert_select 'label[for=user_active_false] > input#user_active_false[type=radio]' end + test 'collection radio buttons with fields for' do + collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] + concat(fields_for(:post) do |p| + p.collection_radio_buttons :category_id, collection, :id, :name + end) + + assert_select 'input#post_category_id_1[type=radio][value=1]' + assert_select 'input#post_category_id_2[type=radio][value=2]' + + assert_select 'label.collection_radio_buttons[for=post_category_id_1]', 'Category 1' + assert_select 'label.collection_radio_buttons[for=post_category_id_2]', 'Category 2' + end + + # COLLECTION CHECK BOXES test 'collection check boxes accepts a collection and generate a serie of checkboxes for value method' do collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] @@ -281,19 +295,16 @@ class FormCollectionsHelperTest < ActionView::TestCase end test 'collection check boxes with fields for' do - skip "test collection check boxes with fields for (and radio buttons as well)" collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] - concat(form_for(:user) do |f| - f.fields_for(:post) do |p| - p.collection_check_boxes :category_ids, collection, :id, :name - end + concat(fields_for(:post) do |p| + p.collection_check_boxes :category_ids, collection, :id, :name end) - assert_select 'input#user_post_category_ids_1[type=checkbox][value=1]' - assert_select 'input#user_post_category_ids_2[type=checkbox][value=2]' + assert_select 'input#post_category_ids_1[type=checkbox][value=1]' + assert_select 'input#post_category_ids_2[type=checkbox][value=2]' - assert_select 'label.collection_check_boxes[for=user_post_category_ids_1]', 'Category 1' - assert_select 'label.collection_check_boxes[for=user_post_category_ids_2]', 'Category 2' + assert_select 'label.collection_check_boxes[for=post_category_ids_1]', 'Category 1' + assert_select 'label.collection_check_boxes[for=post_category_ids_2]', 'Category 2' end test 'collection check boxeses wraps the collection in the given collection wrapper tag' do -- cgit v1.2.3