Created
August 29, 2012 21:02
-
-
Save nazarhussain/3518919 to your computer and use it in GitHub Desktop.
Custom Collection Inputs Simple Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment