Skip to content

Commit 96039db

Browse files
committed
Add support for check_boxes_collection
Closes bootstrap-ruby#2
1 parent 2d6d31a commit 96039db

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This gem wraps the following Rails form helpers:
8383
* time_select
8484
* datetime_select
8585
* check_box
86+
* check_boxes_collection
8687
* radio_button
8788
* radio_buttons_collection
8889

@@ -213,19 +214,19 @@ To display checkboxes and radios inline, pass the `inline: true` option:
213214

214215
#### Collections
215216

216-
BootstrapForms also provide a helpful helper that automatically creates the
217-
`form_group` and the `radio_button`s for you:
217+
BootstrapForms also provide helpful helpers that automatically creates the
218+
`form_group` and the `radio_button`s or `check_box`es for you:
218219

219220
```erb
220221
<%= f.radio_buttons_collection :skill_level, Skill.all, :id, :name %>
222+
<%= f.check_boxes_collection :skills, Skill.all, :id, :name %>
221223
```
222224

223225
Collection methods accept these options:
224226
* `:label`: Customize the `form_group`'s label;
225227
* `:hide-label`: Pass true to hide the `form_group`'s label;
226228
* `:help`: Add a help span to the `form_group`;
227-
* `:inline`: Renders inline `radio_button`s;
228-
* Any other option will be forwarded to the `radio_button`;
229+
* Other options will be forwarded to the `radio_button`/`check_box` method;
229230

230231
### Prepending and Appending Inputs
231232

lib/bootstrap_form/form_builder.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,16 @@ def static_control(name, options = {}, &block)
116116
end
117117
end
118118

119-
def radio_buttons_collection(name, collection, value, text, options = {})
120-
options.symbolize_keys!
121-
122-
label = options.delete(:label)
123-
label_class = hide_class if options.delete(:hide_label)
124-
help = options.delete(:help)
125-
126-
form_group(name, label: { text: label, class: label_class }, help: help) do
127-
inputs = ''
128-
129-
collection.each do |obj|
130-
radio_options = options.merge(label: obj.send(text))
131-
inputs << radio_button(name, obj.send(value), radio_options)
132-
end
119+
def radio_buttons_collection(*args)
120+
inputs_collection(*args) do |name, value, options|
121+
radio_button(name, value, options)
122+
end
123+
end
133124

134-
inputs.html_safe
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)
135129
end
136130
end
137131

@@ -192,5 +186,24 @@ def generate_help(name, help_text)
192186
help_text = object.errors[name].join(', ') if has_error?(name)
193187
content_tag(:span, help_text, class: 'help-block') if help_text
194188
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
195208
end
196209
end

test/bootstrap_form_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,26 @@ def setup
428428

429429
assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street, inline: true)
430430
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+
431453
end

0 commit comments

Comments
 (0)