Skip to content

Instantly share code, notes, and snippets.

@nazarhussain
Created August 29, 2012 21:02
Show Gist options
  • Save nazarhussain/3518919 to your computer and use it in GitHub Desktop.
Save nazarhussain/3518919 to your computer and use it in GitHub Desktop.
Custom Collection Inputs Simple Form
module SimpleForm
class FormBuilder
def collection_check_boxes_with_hints(attribute, collection, value_method, text_method, options={}, html_options={})
rendered_collection = render_collection(
collection, value_method, text_method, options.merge({:item_wrapper_tag => nil}), html_options
) do |item, value, text, default_html_options|
default_html_options[:multiple] = true
builder = instantiate_builder(SimpleForm::ActionViewExtensions::CheckBoxBuilder, attribute, item, value, text, default_html_options)
label = builder.label(:class => 'inline checkbox checkbox_with_hints').split("</")
[
label[0],
builder.check_box,
"<p class='help-block'> #{options[:hint_method].call(text, value)}</p>".html_safe,
"</#{label[1]}"
].join(' ')
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
end
end
class CheckBoxesWithHintsInput < SimpleForm::Inputs::CollectionCheckBoxesInput
def input
super
end
end
module SimpleForm
class FormBuilder
def collection_radio_buttons_with_hints(attribute, collection, value_method, text_method, options={}, html_options={})
rendered_collection = render_collection(
collection, value_method, text_method, options.merge({:item_wrapper_tag => nil}), html_options
) do |item, value, text, default_html_options|
default_html_options[:multiple] = true
builder = instantiate_builder(SimpleForm::ActionViewExtensions::RadioButtonBuilder, attribute, item, value, text, default_html_options)
label = builder.label(:class => 'inline radio radio_with_hints').split(">")
[
label[0],
">",
builder.radio_button,
"<p class='help-block'> #{options[:hint_method].call(text, value)}</p>".html_safe,
"#{label[1].split("<").join(" <")}>"
].join(' ').gsub("[]","")
end
wrap_rendered_collection(rendered_collection, options)
end
end
end
class RadioButtonsWithHintsInput < SimpleForm::Inputs::CollectionRadioButtonsInput
def input
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment