Skip to content

Commit 3955276

Browse files
committed
Merge pull request bootstrap-ruby#202 from andrewbredow/label_options
Allow label options to be passed in as a hash
2 parents c7af9f1 + 9a846c9 commit 3955276

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/bootstrap_form/form_builder.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,20 @@ def form_group_builder(method, options, html_options = nil)
318318
end
319319

320320
unless options.delete(:skip_label)
321-
label_class = hide_class if options.delete(:hide_label)
321+
if options[:label].is_a?(Hash)
322+
label_text = options[:label].delete(:text)
323+
label_class = options[:label].delete(:class)
324+
options.delete(:label)
325+
end
322326
label_class ||= options.delete(:label_class)
327+
label_class = hide_class if options.delete(:hide_label)
328+
329+
if options[:label].is_a?(String)
330+
label_text ||= options.delete(:label)
331+
end
323332

324-
form_group_options.reverse_merge!(label: {
325-
text: options.delete(:label),
333+
form_group_options.merge!(label: {
334+
text: label_text,
326335
class: label_class
327336
})
328337
end

test/bootstrap_form_group_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@ def setup
77
setup_test_fixture
88
end
99

10-
test "changing the label text" do
10+
test "changing the label text via the label option parameter" do
1111
expected = %{<div class="form-group"><label class="control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
1212
assert_equal expected, @builder.text_field(:email, label: 'Email Address')
1313
end
1414

15+
test "changing the label text via the html_options label hash" do
16+
expected = %{<div class="form-group"><label class="control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
17+
assert_equal expected, @builder.text_field(:email, label: {text: 'Email Address'})
18+
end
19+
1520
test "hiding a label" do
1621
expected = %{<div class="form-group"><label class="sr-only control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
1722
assert_equal expected, @builder.text_field(:email, hide_label: true)
1823
end
1924

20-
test "adding a custom label class" do
25+
test "adding a custom label class via the label_class parameter" do
2126
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
2227
assert_equal expected, @builder.text_field(:email, label_class: 'btn')
2328
end
2429

30+
test "adding a custom label class via the html_options label hash" do
31+
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
32+
assert_equal expected, @builder.text_field(:email, label: {class: 'btn'})
33+
end
34+
35+
test "adding a custom label and changing the label text via the html_options label hash" do
36+
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
37+
assert_equal expected, @builder.text_field(:email, label: {class: 'btn', text: "Email Address"})
38+
end
39+
2540
test "skipping a label" do
2641
expected = %{<div class="form-group"><input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" /></div>}
2742
assert_equal expected, @builder.text_field(:email, skip_label: true)

0 commit comments

Comments
 (0)