module SimpleForm::ActionViewExtensions::Builder

def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})


* a block => to generate the label + check box or any other component.

* item_wrapper_class => the CSS class to use for item_wrapper_tag

* item_wrapper_tag => the tag to wrap each item in the collection.

* collection_wrapper_class => the CSS class to use for collection_wrapper_tag

* collection_wrapper_tag => the tag to wrap the entire collection.

item or an array of items.
* disabled => the value or values that should be disabled. Accepts a single

a single item or an array of items. It overrides existing associations.
* checked => the value or values that should be checked initially. Accepts

Collection check box accepts some extra options:

== Options

end
end
b.label { b.check_box + b.text }
) do |b|
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
f.collection_check_boxes(
form_for @user do |f|

label. To wrap the check box with the label, for instance:
It is also possible to give a block that should generate the check box +








end
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
form_for @user do |f|

== Examples

that will be evaluated for each item in the collection.
You can give a symbol or a proc to both value_method and text_method,
convert items in the collection for use as text/value in check boxes.
associated with a clickable label. Use value_method and text_method to
Creates a collection of check boxes for each item in the collection,
def collection_check_boxes(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|
    default_html_options[:multiple] = true
    builder = instantiate_builder(CheckBoxBuilder, attribute, item, value, text, default_html_options)
    if block_given?
      yield builder
    else
      builder.check_box + builder.label(:class => "collection_check_boxes")
    end
  end
  # Append a hidden field to make sure something will be sent back to the
  # server if all checkboxes are unchecked.
  hidden = template.hidden_field_tag("#{object_name}[#{attribute}][]", "", :id => nil)
  wrap_rendered_collection(rendered_collection + hidden, options)
end

def collection_radio(*args, &block)

deprecated
def collection_radio(*args, &block)
  SimpleForm.deprecation_warn "The `collection_radio` helper is deprecated, " \
    "please use `collection_radio_buttons` instead."
  collection_radio_buttons(*args, &block)
end

def collection_radio_buttons(attribute, collection, value_method, text_method, options={}, html_options={})


* a block => to generate the label + radio or any other component.

* item_wrapper_class => the CSS class to use for item_wrapper_tag

* item_wrapper_tag => the tag to wrap each item in the collection.

* collection_wrapper_class => the CSS class to use for collection_wrapper_tag

* collection_wrapper_tag => the tag to wrap the entire collection.

item or an array of items.
* disabled => the value or values that should be disabled. Accepts a single

* checked => the value that should be checked initially.

Collection radio accepts some extra options:

== Options

end
end
b.label { b.radio_button + b.text }
) do |b|
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
f.collection_radio_buttons(
form_for @user do |f|

label. To wrap the radio with the label, for instance:
It is also possible to give a block that should generate the radio +






end
f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
form_for @user do |f|

== Examples

the collection.
value_method and text_method, that will be evaluated for each item in
to convert these text/value. You can give a symbol or a proc to both
text/value option in the collection, using value_method and text_method
helper will create a radio input associated with a label for each
Create a collection of radio inputs for the attribute. Basically this
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_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

def default_html_options_for_collection(item, value, options, html_options) #:nodoc:

:nodoc:
:disabled.
Generate default options for collection helpers, such as :checked and
def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
  html_options = html_options.dup
  [:checked, :selected, :disabled].each do |option|
    next unless current_option = options[option]
    accept = if current_option.respond_to?(:call)
      current_option.call(item)
    else
      Array(current_option).include?(value)
    end
    if accept
      html_options[option] = true
    elsif option == :checked
      html_options[option] = false
    end
  end
  html_options
end

def instantiate_builder(builder_class, attribute, item, value, text, html_options)

def instantiate_builder(builder_class, attribute, item, value, text, html_options)
  builder_class.new(@template, object_name, attribute, item,
                    sanitize_attribute_name(attribute, value), text, value, html_options)
end

def render_collection(collection, value_method, text_method, options={}, html_options={}) #:nodoc:

:nodoc:
def render_collection(collection, value_method, text_method, options={}, html_options={}) #:nodoc:
  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, options, html_options)
    rendered_item = yield item, value, text, default_html_options
    item_wrapper_tag ? @template.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
  end.join.html_safe
end

def sanitize_attribute_name(attribute, value) #:nodoc:

:nodoc:
def sanitize_attribute_name(attribute, value) #:nodoc:
  "#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
end

def simple_fields_for(*args, &block)

end
end
posts_form.input :title
# Here you have all simple_form methods available
f.simple_fields_for :posts do |posts_form|
form_for @user do |f|

Example:
Wrapper for using SimpleForm inside a default rails form.
def simple_fields_for(*args, &block)
  options = args.extract_options!
  options[:wrapper] ||= self.options[:wrapper]
  if self.class < ActionView::Helpers::FormBuilder
    options[:builder] ||= self.class
  else
    options[:builder] ||= SimpleForm::FormBuilder
  end
  fields_for(*(args << options), &block)
end

def value_for_collection(item, value) #:nodoc:

:nodoc:
def value_for_collection(item, value) #:nodoc:
  value.respond_to?(:call) ? value.call(item) : item.send(value)
end

def wrap_rendered_collection(collection, options)

def wrap_rendered_collection(collection, options)
  wrapper_tag = options[:collection_wrapper_tag]
  if wrapper_tag
    wrapper_class = options[:collection_wrapper_class]
    @template.content_tag(wrapper_tag, collection, :class => wrapper_class)
  else
    collection
  end
end