Skip to content

Commit 2517012

Browse files
committed
Merge pull request bootstrap-ruby#9 from carloslopes/radio-buttons-collection
Add support for radio_buttons_collection and check_boxes_collection
2 parents 0f35bed + 96039db commit 2517012

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ This gem wraps the following Rails form helpers:
8383
* time_select
8484
* datetime_select
8585
* check_box
86+
* check_boxes_collection
8687
* radio_button
88+
* radio_buttons_collection
8789

8890
### Default Form Style
8991

@@ -210,6 +212,22 @@ To display checkboxes and radios inline, pass the `inline: true` option:
210212
<% end %>
211213
```
212214

215+
#### Collections
216+
217+
BootstrapForms also provide helpful helpers that automatically creates the
218+
`form_group` and the `radio_button`s or `check_box`es for you:
219+
220+
```erb
221+
<%= f.radio_buttons_collection :skill_level, Skill.all, :id, :name %>
222+
<%= f.check_boxes_collection :skills, Skill.all, :id, :name %>
223+
```
224+
225+
Collection methods accept these options:
226+
* `:label`: Customize the `form_group`'s label;
227+
* `:hide-label`: Pass true to hide the `form_group`'s label;
228+
* `:help`: Add a help span to the `form_group`;
229+
* Other options will be forwarded to the `radio_button`/`check_box` method;
230+
213231
### Prepending and Appending Inputs
214232

215233
You can pass `prepend` and/or `append` options to input fields:

lib/bootstrap_form/form_builder.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ def static_control(name, options = {}, &block)
116116
end
117117
end
118118

119+
def radio_buttons_collection(*args)
120+
inputs_collection(*args) do |name, value, options|
121+
radio_button(name, value, options)
122+
end
123+
end
124+
125+
def check_boxes_collection(*args)
126+
inputs_collection(*args) do |name, value, options|
127+
options[:multiple] = true
128+
check_box(name, options, value, nil)
129+
end
130+
end
131+
119132
private
120133

121134
def normalize_args!(method_name, args)
@@ -173,5 +186,24 @@ def generate_help(name, help_text)
173186
help_text = object.errors[name].join(', ') if has_error?(name)
174187
content_tag(:span, help_text, class: 'help-block') if help_text
175188
end
189+
190+
def inputs_collection(name, collection, value, text, options = {}, &block)
191+
options.symbolize_keys!
192+
193+
label = options.delete(:label)
194+
label_class = hide_class if options.delete(:hide_label)
195+
help = options.delete(:help)
196+
197+
form_group(name, label: { text: label, class: label_class }, help: help) do
198+
inputs = ''
199+
200+
collection.each do |obj|
201+
input_options = options.merge(label: obj.send(text))
202+
inputs << block.call(name, obj.send(value), input_options)
203+
end
204+
205+
inputs.html_safe
206+
end
207+
end
176208
end
177209
end

test/bootstrap_form_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,47 @@ def setup
407407
expected = %{<div class="form-group"><label for="other_model_email">Email</label><input class="form-control" id="other_model_email" name="other_model[email]" type="text" /></div>}
408408
assert_equal expected, builder.text_field(:email)
409409
end
410+
411+
test 'radio_buttons_collection renders the form_group correctly' do
412+
collection = [Address.new(id: 1, street: 'Foobar')]
413+
expected = %{<div class="form-group"><label for="user_misc">This is a radio button collection</label><label class="radio" for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foobar</label><span class="help-block">With a help!</span></div>}
414+
415+
assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street, label: 'This is a radio button collection', help: 'With a help!')
416+
end
417+
418+
test 'radio_buttons_collection renders multiple radios correctly' do
419+
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
420+
expected = %{<div class="form-group"><label for="user_misc">Misc</label><label class="radio" for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foo</label><label class="radio" for="user_misc_2"><input id="user_misc_2" name="user[misc]" type="radio" value="2" /> Bar</label></div>}
421+
422+
assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street)
423+
end
424+
425+
test 'radio_buttons_collection renders inline radios correctly' do
426+
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
427+
expected = %{<div class="form-group"><label for="user_misc">Misc</label><label class="radio-inline" for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foo</label><label class="radio-inline" for="user_misc_2"><input id="user_misc_2" name="user[misc]" type="radio" value="2" /> Bar</label></div>}
428+
429+
assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street, inline: true)
430+
end
431+
432+
test 'check_boxes_collection renders the form_group correctly' do
433+
collection = [Address.new(id: 1, street: 'Foobar')]
434+
expected = %{<div class="form-group"><label for="user_misc">This is a checkbox collection</label><div class="checkbox"><label for="user_misc"><input id="user_misc_1" name="user[misc][]" type="checkbox" value="1" /> Foobar</label></div><span class="help-block">With a help!</span></div>}
435+
436+
assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street, label: 'This is a checkbox collection', help: 'With a help!')
437+
end
438+
439+
test 'check_boxes_collection renders multiple checkboxes correctly' do
440+
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
441+
expected = %{<div class="form-group"><label for="user_misc">Misc</label><div class="checkbox"><label for="user_misc"><input id="user_misc_1" name="user[misc][]" type="checkbox" value="1" /> Foo</label></div><div class="checkbox"><label for="user_misc"><input id="user_misc_2" name="user[misc][]" type="checkbox" value="2" /> Bar</label></div></div>}
442+
443+
assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street)
444+
end
445+
446+
test 'check_boxes_collection renders inline checkboxes correctly' do
447+
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
448+
expected = %{<div class="form-group"><label for="user_misc">Misc</label><label class="checkbox-inline" for="user_misc"><input id="user_misc_1" name="user[misc][]" type="checkbox" value="1" /> Foo</label><label class="checkbox-inline" for="user_misc"><input id="user_misc_2" name="user[misc][]" type="checkbox" value="2" /> Bar</label></div>}
449+
450+
assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street, inline: true)
451+
end
452+
410453
end

0 commit comments

Comments
 (0)