Skip to content

Commit

Permalink
Add support for radio_buttons_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
carloslopes committed Jan 2, 2014
1 parent 0f35bed commit 2d6d31a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ This gem wraps the following Rails form helpers:
* datetime_select
* check_box
* radio_button
* radio_buttons_collection

### Default Form Style

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

#### Collections

BootstrapForms also provide a helpful helper that automatically creates the
`form_group` and the `radio_button`s for you:

```erb
<%= f.radio_buttons_collection :skill_level, 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`;
* `:inline`: Renders inline `radio_button`s;
* Any other option will be forwarded to the `radio_button`;

### Prepending and Appending Inputs

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

def radio_buttons_collection(name, collection, value, text, options = {})
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|
radio_options = options.merge(label: obj.send(text))
inputs << radio_button(name, obj.send(value), radio_options)
end

inputs.html_safe
end
end

private

def normalize_args!(method_name, args)
Expand Down
21 changes: 21 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,25 @@ 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
end

0 comments on commit 2d6d31a

Please sign in to comment.