Browse files

Add collection_radio_buttons and collection_check_boxes to FormBuilder

[Carlos Antonio da Silva + Rafael Mendonça França]
  • Loading branch information...
1 parent 1520688 commit 21eba34718660b96b45cbb594d557b7e84bfed4f @rafaelfranca rafaelfranca committed with carlosantoniodasilva Jan 31, 2012
View
10 actionpack/lib/action_view/helpers/form_collections_helper.rb
@@ -9,5 +9,15 @@ def collection_check_boxes(object, method, collection, value_method, text_method
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
View
29 actionpack/test/template/form_collections_helper_test.rb
@@ -179,6 +179,20 @@ def with_collection_check_boxes(*args, &block)
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 @@ def with_collection_check_boxes(*args, &block)
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

0 comments on commit 21eba34

Please sign in to comment.