Create a Builder factory class to use with collection helpers + block

This will make it easy for the user to handle how check box/radio and
labels should be generated, abstracting any text/value/default html
options required to make it work.

[Carlos Antonio da Silva + Rafael Mendonça França]
This commit is contained in:
Carlos Antonio da Silva 2012-02-01 16:14:24 -02:00
parent c988aaf690
commit 17d214a1d4
3 changed files with 45 additions and 21 deletions

View File

@ -2,15 +2,22 @@ module ActionView
module Helpers
module Tags
class CollectionCheckBoxes < CollectionRadioButtons
class CheckBoxBuilder < Builder
def check_box(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
@template_object.check_box(@object_name, @method_name, html_options, @value, nil)
end
end
def render
rendered_collection = render_collection do |value, text, default_html_options|
default_html_options[:multiple] = true
builder = instantiate_builder(CheckBoxBuilder, value, text, default_html_options)
if block_given?
yield sanitize_attribute_name(value), text, value, default_html_options
yield builder
else
check_box(value, default_html_options) +
label(value, text, "collection_check_boxes")
builder.check_box + builder.label(:class => "collection_check_boxes")
end
end
@ -20,12 +27,6 @@ module ActionView
rendered_collection + hidden
end
private
def check_box(value, html_options)
@template_object.check_box(@object_name, @method_name, html_options, value, nil)
end
end
end
end

View File

@ -2,25 +2,48 @@ module ActionView
module Helpers
module Tags
class CollectionRadioButtons < CollectionSelect
class Builder
def initialize(template_object, object_name, method_name,
sanitized_attribute_name, text, value, input_html_options)
@template_object = template_object
@object_name = object_name
@method_name = method_name
@sanitized_attribute_name = sanitized_attribute_name
@text = text
@value = value
@input_html_options = input_html_options
end
def label(label_html_options={}, &block)
@template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
end
end
class RadioButtonBuilder < Builder
def radio_button(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
@template_object.radio_button(@object_name, @method_name, @value, html_options)
end
end
def render
render_collection do |value, text, default_html_options|
builder = instantiate_builder(RadioButtonBuilder, value, text, default_html_options)
if block_given?
yield sanitize_attribute_name(value), text, value, default_html_options
yield builder
else
radio_button(value, default_html_options) +
label(value, text, "collection_radio_buttons")
builder.radio_button + builder.label(:class => "collection_radio_buttons")
end
end
end
private
def radio_button(value, html_options)
@template_object.radio_button(@object_name, @method_name, value, html_options)
end
def instantiate_builder(builder_class, value, text, html_options)
builder_class.new(@template_object, @object_name, @method_name,
sanitize_attribute_name(value), text, value, html_options)
def label(value, text, css_class)
@template_object.label(@object_name, sanitize_attribute_name(value), text, :class => css_class)
end
# Generate default options for collection helpers, such as :checked and

View File

@ -84,8 +84,8 @@ class FormCollectionsHelperTest < ActionView::TestCase
end
test 'collection radio accepts a block to render the radio and label as required' do
with_collection_radio_buttons :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) }
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
b.label { b.radio_button }
end
assert_select 'label[for=user_active_true] > input#user_active_true[type=radio]'
@ -229,8 +229,8 @@ class FormCollectionsHelperTest < ActionView::TestCase
end
test 'collection check boxes 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) }
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
b.label { b.check_box }
end
assert_select 'label[for=user_active_true] > input#user_active_true[type=checkbox]'