Skip to content

Commit

Permalink
Fix collection_radio_buttons tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Dec 27, 2012
1 parent a79e533 commit 995087a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/rails/rails.git
revision: b05819fd28acd069556767a99738a6fd8b0a1965
revision: ceedec7edcadbad824f58e84bc2c1eddbe8539ce
branch: master
specs:
actionpack (4.0.0.beta)
Expand Down
181 changes: 108 additions & 73 deletions lib/simple_form/action_view_extensions/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ def label(label_html_options={}, &block)
end
end

# Handles generating an instance of radio + label for collection_radio_buttons.
class RadioButtonBuilder < BuilderBase #:nodoc:
def radio_button(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
@form_builder.radio_button(@method_name, @value, html_options)
end
end

# Handles generating an instance of check box + label for collection_check_boxes.
class CheckBoxBuilder < BuilderBase #:nodoc:
def check_box(extra_html_options={})
Expand All @@ -40,70 +32,6 @@ def check_box(extra_html_options={})
# A collection of methods required by simple_form but added to rails default form.
# This means that you can use such methods outside simple_form context.
module Builder
# Create a collection of radio inputs for the attribute. Basically this
# helper will create a radio input associated with a label for each
# text/value option in the collection, using value_method and text_method
# to convert these text/value. You can give a symbol or a proc to both
# value_method and text_method, that will be evaluated for each item in
# the collection.
#
# == Examples
#
# form_for @user do |f|
# f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
# end
#
# <input id="user_options_true" name="user[options]" type="radio" value="true" />
# <label class="collection_radio_buttons" for="user_options_true">Yes</label>
# <input id="user_options_false" name="user[options]" type="radio" value="false" />
# <label class="collection_radio_buttons" for="user_options_false">No</label>
#
# It is also possible to give a block that should generate the radio +
# label. To wrap the radio with the label, for instance:
#
# form_for @user do |f|
# f.collection_radio_buttons(
# :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
# ) do |b|
# b.label { b.radio_button + b.text }
# end
# end
#
# == Options
#
# Collection radio accepts some extra options:
#
# * checked => the value that should be checked initially.
#
# * disabled => the value or values that should be disabled. Accepts a single
# item or an array of items.
#
# * collection_wrapper_tag => the tag to wrap the entire collection.
#
# * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
#
# * item_wrapper_class => the CSS class to use for item_wrapper_tag
#
# * a block => to generate the label + radio or any other component.
#
def collection_radio_buttons(attribute, collection, value_method, text_method, options={}, html_options={})
rendered_collection = render_collection(
collection, value_method, text_method, options, html_options
) do |item, value, text, default_html_options|
builder = instantiate_collection_builder(RadioButtonBuilder, attribute, item, value, text, default_html_options)

if block_given?
yield builder
else
builder.radio_button + builder.label(:class => "collection_radio_buttons")
end
end

wrap_rendered_collection(rendered_collection, options)
end

# deprecated
def collection_radio(*args, &block)
SimpleForm.deprecation_warn "The `collection_radio` helper is deprecated, " \
Expand Down Expand Up @@ -274,4 +202,111 @@ def wrap_rendered_collection(collection, options)
end
end

ActionView::Helpers::FormBuilder.send(:include, SimpleForm::ActionViewExtensions::Builder)
module SimpleForm
module Tags
class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
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

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

module ActionView::Helpers
class FormBuilder
include SimpleForm::ActionViewExtensions::Builder

# Create a collection of radio inputs for the attribute. Basically this
# helper will create a radio input associated with a label for each
# text/value option in the collection, using value_method and text_method
# to convert these text/value. You can give a symbol or a proc to both
# value_method and text_method, that will be evaluated for each item in
# the collection.
#
# == Examples
#
# form_for @user do |f|
# f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
# end
#
# <input id="user_options_true" name="user[options]" type="radio" value="true" />
# <label class="collection_radio_buttons" for="user_options_true">Yes</label>
# <input id="user_options_false" name="user[options]" type="radio" value="false" />
# <label class="collection_radio_buttons" for="user_options_false">No</label>
#
# It is also possible to give a block that should generate the radio +
# label. To wrap the radio with the label, for instance:
#
# form_for @user do |f|
# f.collection_radio_buttons(
# :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
# ) do |b|
# b.label { b.radio_button + b.text }
# end
# end
#
# == Options
#
# Collection radio accepts some extra options:
#
# * checked => the value that should be checked initially.
#
# * disabled => the value or values that should be disabled. Accepts a single
# item or an array of items.
#
# * collection_wrapper_tag => the tag to wrap the entire collection.
#
# * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
#
# * item_wrapper_class => the CSS class to use for item_wrapper_tag
#
# * a block => to generate the label + radio or any other component.
def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
SimpleForm::Tags::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
end
end
end

0 comments on commit 995087a

Please sign in to comment.