Skip to content

Commit

Permalink
Don't add class="" to label if no classes specified. (#383)
Browse files Browse the repository at this point in the history
* Don't add class="" to label if no classes specified.

* Resolve conflicts.
  • Loading branch information
lcreid authored and mattbrictson committed Jan 17, 2018
1 parent 6fde0fc commit 6082396
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 68 deletions.
3 changes: 2 additions & 1 deletion lib/bootstrap_form/form_builder.rb
Expand Up @@ -393,7 +393,8 @@ def generate_label(id, name, options, custom_label_col, group_layout)
classes << "required" if required_attribute?(object, name)
end

options[:class] = classes.compact.join(" ")
options[:class] = classes.compact.join(" ").strip
options.delete(:class) if options[:class].empty?

if label_errors && has_error?(name)
error_messages = get_error_messages(name)
Expand Down
38 changes: 19 additions & 19 deletions test/bootstrap_fields_test.rb
Expand Up @@ -8,7 +8,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "color fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="color" value="#000000" />
</div>
HTML
Expand All @@ -18,7 +18,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "date fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="date" />
</div>
HTML
Expand All @@ -28,7 +28,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "date time fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="datetime" />
</div>
HTML
Expand All @@ -38,7 +38,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "date time local fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="datetime-local" />
</div>
HTML
Expand All @@ -48,7 +48,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "email fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="email" />
</div>
HTML
Expand All @@ -58,7 +58,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "file fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input id="user_misc" name="user[misc]" type="file" />
</div>
HTML
Expand All @@ -73,7 +73,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "month local fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="month" />
</div>
HTML
Expand All @@ -83,7 +83,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "number fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="number" />
</div>
HTML
Expand All @@ -93,7 +93,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "password fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_password">Password</label>
<label for="user_password">Password</label>
<input class="form-control" id="user_password" name="user[password]" type="password" />
<small class="form-text text-muted">A good password should be at least six characters long</small>
</div>
Expand All @@ -104,7 +104,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "phone/telephone fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="tel" />
</div>
HTML
Expand All @@ -115,7 +115,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "range fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="range" />
</div>
HTML
Expand All @@ -125,7 +125,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "search fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="search" />
</div>
HTML
Expand All @@ -135,7 +135,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "text areas are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_comments">Comments</label>
<label for="user_comments">Comments</label>
<textarea class="form-control" id="user_comments" name="user[comments]">\nmy comment</textarea>
</div>
HTML
Expand All @@ -155,7 +155,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "time fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="time" />
</div>
HTML
Expand All @@ -165,7 +165,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "url fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="url" />
</div>
HTML
Expand All @@ -175,7 +175,7 @@ class BootstrapFieldsTest < ActionView::TestCase
test "week fields are wrapped correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="week" />
</div>
HTML
Expand All @@ -195,7 +195,7 @@ class BootstrapFieldsTest < ActionView::TestCase
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="form-group">
<label class="" for="user_address_attributes_street">Street</label>
<label for="user_address_attributes_street">Street</label>
<input class="form-control" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="123 Main Street" />
</div>
</form>
Expand All @@ -216,7 +216,7 @@ class BootstrapFieldsTest < ActionView::TestCase
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="form-group">
<label class="" for="user_preferences_favorite_color">Favorite color</label>
<label for="user_preferences_favorite_color">Favorite color</label>
<input class="form-control" id="user_preferences_favorite_color" name="user[preferences][favorite_color]" type="text" value="cerulean" />
</div>
</form>
Expand Down Expand Up @@ -260,7 +260,7 @@ class BootstrapFieldsTest < ActionView::TestCase
<form accept-charset="UTF-8" action="/users" class="form-inline" id="new_user" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="form-group">
<label class="" for="user_address_attributes_street">Street</label>
<label for="user_address_attributes_street">Street</label>
<input class="form-control" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="123 Main Street" />
</div>
</form>
Expand Down
14 changes: 7 additions & 7 deletions test/bootstrap_form_group_test.rb
Expand Up @@ -77,7 +77,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "preventing a label from having the required class" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_email">Email</label>
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" />
</div>
HTML
Expand Down Expand Up @@ -174,7 +174,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "help messages to look up I18n automatically" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_password">Password</label>
<label for="user_password">Password</label>
<input class="form-control" id="user_password" name="user[password]" type="text" value="secret" />
<small class="form-text text-muted">A good password should be at least six characters long</small>
</div>
Expand Down Expand Up @@ -203,7 +203,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "help messages to ignore translation when user disables help" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_password">Password</label>
<label for="user_password">Password</label>
<input class="form-control" id="user_password" name="user[password]" type="text" value="secret" />
</div>
HTML
Expand Down Expand Up @@ -323,7 +323,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "adds class to wrapped form_group by a field" do
expected = <<-HTML.strip_heredoc
<div class="form-group none-margin">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="search" />
</div>
HTML
Expand Down Expand Up @@ -402,7 +402,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "adding an icon to a field" do
expected = <<-HTML.strip_heredoc
<div class="form-group has-feedback">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="email" />
<span class="glyphicon glyphicon-ok invalid-feedback"></span>
</div>
Expand Down Expand Up @@ -435,7 +435,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "adds data-attributes (or any other options) to wrapper" do
expected = <<-HTML.strip_heredoc
<div class="form-group" data-foo="bar">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="search" />
</div>
HTML
Expand All @@ -459,7 +459,7 @@ class BootstrapFormGroupTest < ActionView::TestCase

expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_nil">Foo</label>
<label for="user_nil">Foo</label>
</div>
HTML
assert_equivalent_xml expected, output
Expand Down
6 changes: 3 additions & 3 deletions test/bootstrap_form_test.rb
Expand Up @@ -67,7 +67,7 @@ class BootstrapFormTest < ActionView::TestCase
<form accept-charset="UTF-8" action="/users" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="form-group">
<label class="" for="email">Your Email</label>
<label for="email">Your Email</label>
<input class="form-control" id="email" name="email" type="text" />
</div>
</form>
Expand All @@ -80,7 +80,7 @@ class BootstrapFormTest < ActionView::TestCase
<form accept-charset="UTF-8" action="/users" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;" />
<div class="form-group">
<label class="" for="ID">Email</label>
<label for="ID">Email</label>
<input class="form-control" id="ID" name="NAME" type="text" />
</div>
</form>
Expand Down Expand Up @@ -418,7 +418,7 @@ class BootstrapFormTest < ActionView::TestCase
builder = BootstrapForm::FormBuilder.new :other_model, nil, self, {}
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="other_model_email">Email</label>
<label for="other_model_email">Email</label>
<input class="form-control" id="other_model_email" name="other_model[email]" type="text" />
</div>
HTML
Expand Down
24 changes: 12 additions & 12 deletions test/bootstrap_radio_button_test.rb
Expand Up @@ -75,7 +75,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foobar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">This is a radio button collection</label>
<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" />
Expand All @@ -93,7 +93,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<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
Expand All @@ -114,7 +114,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<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>
Expand All @@ -131,7 +131,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<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
Expand All @@ -152,7 +152,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foobar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">This is a radio button collection</label>
<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" /> rabooF
Expand All @@ -169,7 +169,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foobar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">This is a radio button collection</label>
<label for="user_misc">This is a radio button collection</label>
<div class="radio">
<label for="user_misc_address_1">
<input id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" /> Foobar
Expand All @@ -186,7 +186,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<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" /> ooF
Expand All @@ -207,7 +207,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<div class="radio">
<label for="user_misc_address_1">
<input id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" /> Foo
Expand All @@ -228,7 +228,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foobar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">This is a radio button collection</label>
<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" /> rabooF
Expand All @@ -245,7 +245,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foobar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">This is a radio button collection</label>
<label for="user_misc">This is a radio button collection</label>
<div class="radio">
<label for="user_misc_address_1">
<input id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" /> Foobar
Expand All @@ -262,7 +262,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<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" /> ooF
Expand All @@ -283,7 +283,7 @@ class BootstrapRadioButtonTest < ActionView::TestCase
collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')]
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="" for="user_misc">Misc</label>
<label for="user_misc">Misc</label>
<div class="radio">
<label for="user_misc_address_1">
<input id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" /> Foo
Expand Down

0 comments on commit 6082396

Please sign in to comment.