Skip to content

Commit 5048f09

Browse files
committed
Merge pull request bootstrap-ruby#95 from mkon/fixed-radio-buttons
fixed generated radio_button html
2 parents e8581b2 + ad8d355 commit 5048f09

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/bootstrap_form/form_builder.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ def radio_button(name, value, *args)
9898

9999
html = super(name, value, *args) + " " + options[:label]
100100

101-
css = "radio"
102-
css << "-inline" if options[:inline]
103-
label(name, html, class: css, for: nil)
101+
if options[:inline]
102+
label(name, html, class: "radio-inline", value: value)
103+
else
104+
content_tag(:div, class: "radio") do
105+
label(name, html, value: value)
106+
end
107+
end
104108
end
105109

106110
def collection_check_boxes(*args)

test/bootstrap_form_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ def setup
333333
end
334334

335335
test "radio_button is wrapped correctly" do
336-
expected = %{<label class="radio"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> This is a radio button</label>}
336+
expected = %{<div class="radio"><label for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> This is a radio button</label></div>}
337337
assert_equal expected, @builder.radio_button(:misc, '1', label: 'This is a radio button')
338338
end
339339

340340
test "radio_button inline label is set correctly" do
341-
expected = %{<label class="radio-inline"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> This is a radio button</label>}
341+
expected = %{<label class="radio-inline" for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> This is a radio button</label>}
342342
assert_equal expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', inline: true)
343343
end
344344

@@ -446,7 +446,7 @@ def setup
446446
expected = %{<div class="form-group foo"><label class="control-label col-sm-2"></label><div class="col-sm-10"><p class="form-control-static">Bar</p></div></div>}
447447
assert_equal expected, output
448448
end
449-
449+
450450
test "form_group accepts class thorugh options hash without needing a name" do
451451
output = @horizontal_builder.form_group class: "foo" do
452452
%{<p class="form-control-static">Bar</p>}.html_safe
@@ -609,28 +609,28 @@ def setup
609609

610610
test 'collection_radio_buttons renders the form_group correctly' do
611611
collection = [Address.new(id: 1, street: 'Foobar')]
612-
expected = %{<div class="form-group"><label class="control-label" for="user_misc">This is a radio button collection</label><label class="radio"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foobar</label><span class="help-block">With a help!</span></div>}
612+
expected = %{<div class="form-group"><label class="control-label" for="user_misc">This is a radio button collection</label><div class="radio"><label for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foobar</label></div><span class="help-block">With a help!</span></div>}
613613

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

617617
test 'collection_radio_buttons renders multiple radios correctly' do
618618
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
619-
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><label class="radio"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foo</label><label class="radio"><input id="user_misc_2" name="user[misc]" type="radio" value="2" /> Bar</label></div>}
619+
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><div class="radio"><label for="user_misc_1"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foo</label></div><div class="radio"><label for="user_misc_2"><input id="user_misc_2" name="user[misc]" type="radio" value="2" /> Bar</label></div></div>}
620620

621621
assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, :street)
622622
end
623623

624624
test 'collection_radio_buttons renders inline radios correctly' do
625625
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
626-
expected = %{<div class="form-group"><label class="control-label" for="user_misc">Misc</label><label class="radio-inline"><input id="user_misc_1" name="user[misc]" type="radio" value="1" /> Foo</label><label class="radio-inline"><input id="user_misc_2" name="user[misc]" type="radio" value="2" /> Bar</label></div>}
626+
expected = %{<div class="form-group"><label class="control-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>}
627627

628628
assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, inline: true)
629629
end
630630

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

635635
assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, checked: 1)
636636
end

0 commit comments

Comments
 (0)