Skip to content

Commit

Permalink
Merge pull request bootstrap-ruby#9 from carloslopes/radio-buttons-co…
Browse files Browse the repository at this point in the history
…llection

Add support for radio_buttons_collection and check_boxes_collection
  • Loading branch information
carloslopes committed Jan 3, 2014
2 parents 0f35bed + 96039db commit 2517012
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ This gem wraps the following Rails form helpers:
* time_select
* datetime_select
* check_box
* check_boxes_collection
* radio_button
* radio_buttons_collection

### Default Form Style

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

#### Collections

BootstrapForms also provide helpful helpers that automatically creates the
`form_group` and the `radio_button`s or `check_box`es for you:

```erb
<%= f.radio_buttons_collection :skill_level, Skill.all, :id, :name %>
<%= f.check_boxes_collection :skills, Skill.all, :id, :name %>
```

Collection methods accept these options:
* `:label`: Customize the `form_group`'s label;
* `:hide-label`: Pass true to hide the `form_group`'s label;
* `:help`: Add a help span to the `form_group`;
* Other options will be forwarded to the `radio_button`/`check_box` method;

### Prepending and Appending Inputs

You can pass `prepend` and/or `append` options to input fields:
Expand Down
32 changes: 32 additions & 0 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ def static_control(name, options = {}, &block)
end
end

def radio_buttons_collection(*args)
inputs_collection(*args) do |name, value, options|
radio_button(name, value, options)
end
end

def check_boxes_collection(*args)
inputs_collection(*args) do |name, value, options|
options[:multiple] = true
check_box(name, options, value, nil)
end
end

private

def normalize_args!(method_name, args)
Expand Down Expand Up @@ -173,5 +186,24 @@ def generate_help(name, help_text)
help_text = object.errors[name].join(', ') if has_error?(name)
content_tag(:span, help_text, class: 'help-block') if help_text
end

def inputs_collection(name, collection, value, text, options = {}, &block)
options.symbolize_keys!

label = options.delete(:label)
label_class = hide_class if options.delete(:hide_label)
help = options.delete(:help)

form_group(name, label: { text: label, class: label_class }, help: help) do
inputs = ''

collection.each do |obj|
input_options = options.merge(label: obj.send(text))
inputs << block.call(name, obj.send(value), input_options)
end

inputs.html_safe
end
end
end
end
43 changes: 43 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,47 @@ def setup
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>}
assert_equal expected, builder.text_field(:email)
end

test 'radio_buttons_collection renders the form_group correctly' do
collection = [Address.new(id: 1, street: 'Foobar')]
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>}

assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street, label: 'This is a radio button collection', help: 'With a help!')
end

test 'radio_buttons_collection renders multiple radios correctly' do
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
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>}

assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street)
end

test 'radio_buttons_collection renders inline radios correctly' do
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
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>}

assert_equal expected, @builder.radio_buttons_collection(:misc, collection, :id, :street, inline: true)
end

test 'check_boxes_collection renders the form_group correctly' do
collection = [Address.new(id: 1, street: 'Foobar')]
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>}

assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street, label: 'This is a checkbox collection', help: 'With a help!')
end

test 'check_boxes_collection renders multiple checkboxes correctly' do
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
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>}

assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street)
end

test 'check_boxes_collection renders inline checkboxes correctly' do
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
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>}

assert_equal expected, @builder.check_boxes_collection(:misc, collection, :id, :street, inline: true)
end

end

0 comments on commit 2517012

Please sign in to comment.