From fb796f0a85b42807034cbc939c40c11a76acf063 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 26 Jan 2012 15:34:23 -0200 Subject: [PATCH] Allow giving a block to collection radio/check boxes This will allow more control for the user who wants to handle how to render label / check boxes, ie change their order or put the label inside the check box. --- .../action_view_extensions/builder.rb | 16 ++++++++--- test/action_view_extensions/builder_test.rb | 28 ++++++++++++++++--- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/simple_form/action_view_extensions/builder.rb b/lib/simple_form/action_view_extensions/builder.rb index d366a27b..52f7c09a 100644 --- a/lib/simple_form/action_view_extensions/builder.rb +++ b/lib/simple_form/action_view_extensions/builder.rb @@ -41,8 +41,12 @@ module SimpleForm render_collection( attribute, collection, value_method, text_method, options, html_options ) do |value, text, default_html_options| - radio_button(attribute, value, default_html_options) + - label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio") + if block_given? + yield sanitize_attribute_name(attribute, value), text, value, default_html_options + else + radio_button(attribute, value, default_html_options) + + label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio") + end end end @@ -87,8 +91,12 @@ module SimpleForm ) do |value, text, default_html_options| default_html_options[:multiple] = true - check_box(attribute, default_html_options, value, '') + - label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes") + if block_given? + yield sanitize_attribute_name(attribute, value), text, value, default_html_options + else + check_box(attribute, default_html_options, value, '') + + label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes") + end end end diff --git a/test/action_view_extensions/builder_test.rb b/test/action_view_extensions/builder_test.rb index f44dfa3b..2740109d 100644 --- a/test/action_view_extensions/builder_test.rb +++ b/test/action_view_extensions/builder_test.rb @@ -8,15 +8,15 @@ class BuilderTest < ActionView::TestCase end end - def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={}) + def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block) with_concat_form_for(object) do |f| - f.collection_radio attribute, collection, value_method, text_method, options, html_options + f.collection_radio attribute, collection, value_method, text_method, options, html_options, &block end end - def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}) + def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block) with_concat_form_for(object) do |f| - f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options + f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block end end @@ -146,9 +146,19 @@ class BuilderTest < ActionView::TestCase test 'collection radio does not wrap input inside the label' do with_collection_radio @user, :active, [true, false], :to_s, :to_s + assert_select 'form input[type=radio] + label' assert_no_select 'form label input' end + test 'collection radio accepts a block to render the radio and label as required' do + with_collection_radio @user, :active, [true, false], :to_s, :to_s do |label_for, text, value, html_options| + label(:user, label_for, text) { radio_button(:user, :active, value, html_options) } + end + + assert_select 'form label[for=user_active_true] > input#user_active_true[type=radio]' + assert_select 'form label[for=user_active_false] > input#user_active_false[type=radio]' + end + # COLLECTION CHECK BOX test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')] @@ -323,9 +333,19 @@ class BuilderTest < ActionView::TestCase test 'collection check box does not wrap input inside the label' do with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s + assert_select 'form input[type=checkbox] + label' assert_no_select 'form label input' end + test 'collection check box accepts a block to render the radio and label as required' do + with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s do |label_for, text, value, html_options| + label(:user, label_for, text) { check_box(:user, :active, html_options, value) } + end + + assert_select 'form label[for=user_active_true] > input#user_active_true[type=checkbox]' + assert_select 'form label[for=user_active_false] > input#user_active_false[type=checkbox]' + end + # SIMPLE FIELDS test 'simple fields for is available and yields an instance of FormBuilder' do with_concat_form_for(@user) do |f|