Module: SimpleForm::ActionViewExtensions::Builder
- Included in:
- ActionView::Helpers::FormBuilder
- Defined in:
- lib/simple_form/action_view_extensions/builder.rb
Overview
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.
Instance Method Summary collapse
-
#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Creates a collection of check boxes for each item in the collection, associated with a clickable label.
-
#collection_radio(*args, &block) ⇒ Object
deprecated.
-
#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Create a collection of radio inputs for the attribute.
-
#simple_fields_for(*args, &block) ⇒ Object
Wrapper for using SimpleForm inside a default rails form.
Instance Method Details
#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
Creates a collection of check boxes for each item in the collection, associated with a clickable label. Use value_method and text_method to convert items in the collection for use as text/value in check boxes. 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_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input name="user[options][]" type="hidden" value="" />
<input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
<label class="collection_check_boxes" for="user_options_true">Yes</label>
<input name="user[options][]" type="hidden" value="" />
<input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
<label class="collection_check_boxes" for="user_options_false">No</label>
It is also possible to give a block that should generate the check box + label. To wrap the check box with the label, for instance:
form_for @user do |f|
f.collection_check_boxes(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.check_box + b.text }
end
end
Options
Collection check box accepts some extra options:
* checked => the value or values that should be checked initially. Accepts
a single item or an array of items. It overrides existing associations.
* 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. This option
is ignored if the :collection_wrapper_tag option is blank.
* 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 + check box or any other component.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/simple_form/action_view_extensions/builder.rb', line 165 def collection_check_boxes(attribute, collection, value_method, text_method, ={}, ={}) rendered_collection = render_collection( collection, value_method, text_method, , ) do |item, value, text, | [:multiple] = true builder = instantiate_collection_builder(CheckBoxBuilder, attribute, item, value, text, ) 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_name = [:name] || "#{object_name}[#{attribute}][]" hidden = @template.hidden_field_tag(hidden_name, "", :id => nil) wrap_rendered_collection(rendered_collection + hidden, ) end |
#collection_radio(*args, &block) ⇒ Object
deprecated
108 109 110 111 112 |
# File 'lib/simple_form/action_view_extensions/builder.rb', line 108 def collection_radio(*args, &block) SimpleForm.deprecation_warn "The `collection_radio` helper is deprecated, " \ "please use `collection_radio_buttons` instead." (*args, &block) end |
#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
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.(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b. + 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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/simple_form/action_view_extensions/builder.rb', line 91 def (attribute, collection, value_method, text_method, ={}, ={}) rendered_collection = render_collection( collection, value_method, text_method, , ) do |item, value, text, | builder = instantiate_collection_builder(RadioButtonBuilder, attribute, item, value, text, ) if block_given? yield builder else builder. + builder.label(:class => "collection_radio_buttons") end end wrap_rendered_collection(rendered_collection, ) end |
#simple_fields_for(*args, &block) ⇒ Object
Wrapper for using SimpleForm inside a default rails form. Example:
form_for @user do |f|
f.simple_fields_for :posts do |posts_form|
# Here you have all simple_form methods available
posts_form.input :title
end
end
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/simple_form/action_view_extensions/builder.rb', line 196 def simple_fields_for(*args, &block) = args. [:wrapper] = self.[:wrapper] if [:wrapper].nil? [:defaults] ||= self.[:defaults] if self.class < ActionView::Helpers::FormBuilder [:builder] ||= self.class else [:builder] ||= SimpleForm::FormBuilder end fields_for(*(args << ), &block) end |