Extract some common features to a module

This commit is contained in:
Rafael Mendonça França 2012-12-26 23:25:10 -03:00
parent 1f5f897d6e
commit dc212a533d
1 changed files with 27 additions and 45 deletions

View File

@ -37,27 +37,9 @@ end
module SimpleForm module SimpleForm
module Tags module Tags
class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons module CollectionExtensions
def render
rendered_collection = render_collection do |item, value, text, default_html_options|
builder = instantiate_builder(RadioButtonBuilder, item, value, text, default_html_options)
if block_given?
yield builder
else
render_component(builder)
end
end
wrap_rendered_collection(rendered_collection, @options)
end
private private
def render_component(builder)
builder.radio_button + builder.label(:class => "collection_radio_buttons")
end
def render_collection def render_collection
item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span) item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
item_wrapper_class = @options[:item_wrapper_class] item_wrapper_class = @options[:item_wrapper_class]
@ -85,7 +67,33 @@ module SimpleForm
end end
end end
class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
include CollectionExtensions
def render
rendered_collection = render_collection do |item, value, text, default_html_options|
builder = instantiate_builder(RadioButtonBuilder, item, value, text, default_html_options)
if block_given?
yield builder
else
render_component(builder)
end
end
wrap_rendered_collection(rendered_collection, @options)
end
private
def render_component(builder)
builder.radio_button + builder.label(:class => "collection_radio_buttons")
end
end
class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes
include CollectionExtensions
def render def render
rendered_collection = render_collection do |item, value, text, default_html_options| rendered_collection = render_collection do |item, value, text, default_html_options|
default_html_options[:multiple] = true default_html_options[:multiple] = true
@ -110,32 +118,6 @@ module SimpleForm
def render_component(builder) def render_component(builder)
builder.check_box + builder.label(:class => "collection_check_boxes") builder.check_box + builder.label(:class => "collection_check_boxes")
end end
def render_collection
item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
item_wrapper_class = @options[:item_wrapper_class]
@collection.map do |item|
value = value_for_collection(item, @value_method)
text = value_for_collection(item, @text_method)
default_html_options = default_html_options_for_collection(item, value)
rendered_item = yield item, value, text, default_html_options
item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
end.join.html_safe
end
def wrap_rendered_collection(collection, options)
wrapper_tag = options[:collection_wrapper_tag]
if wrapper_tag
wrapper_class = options[:collection_wrapper_class]
@template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
else
collection
end
end
end end
end end
end end