Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Features:

- Added the ability to append/prepend buttons (@retoo)
- Added support for time_zone_select
- Accept multiple values, and objects as well, on `collection_check_boxes`
checked option (#114)

## 2.1.1 (2014-04-20)

Expand Down
8 changes: 7 additions & 1 deletion lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ def inputs_collection(name, collection, value, text, options = {}, &block)

collection.each do |obj|
input_options = options.merge(label: obj.send(text))
input_options[:checked] = input_options[:checked] == obj.send(value) if input_options[:checked]

if checked = input_options[:checked]
input_options[:checked] = checked == obj.send(value) ||
checked.try(:include?, obj.send(value)) ||
checked == obj ||
checked.try(:include?, obj)
end

input_options.delete(:class)
inputs << block.call(name, obj.send(value), input_options)
Expand Down
9 changes: 9 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@ def setup
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><div class="checkbox"><label for="user_misc_1"><input checked="checked" id="user_misc_1" name="user[misc][]" type="checkbox" value="1" /> Foo</label></div><div class="checkbox"><label for="user_misc_2"><input id="user_misc_2" name="user[misc][]" type="checkbox" value="2" /> Bar</label></div></div>}

assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: 1)
assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: collection.first)
end

test 'collection_check_boxes renders with multiple checked options correctly' do
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><div class="checkbox"><label for="user_misc_1"><input checked="checked" id="user_misc_1" name="user[misc][]" type="checkbox" value="1" /> Foo</label></div><div class="checkbox"><label for="user_misc_2"><input checked="checked" id="user_misc_2" name="user[misc][]" type="checkbox" value="2" /> Bar</label></div></div>}

assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: [1, 2])
assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: collection)
end

test 'errors_on hide attribute name in message' do
Expand Down