From 9a846c989cc596a65dde8ad09f86b66091c4d590 Mon Sep 17 00:00:00 2001 From: Andrew Bredow Date: Wed, 28 Jan 2015 15:30:39 -0500 Subject: [PATCH] Allow the label option to be passed in as a hash When generating fields, it should be possible to specify a hash in the format of `label: { text: "A label", class: "a-class" }`, as indicated in the documentation. This approach assumes that if the developer has specified the label as a hash, then it trumps `label_class` in the options hash. --- lib/bootstrap_form/form_builder.rb | 15 ++++++++++++--- test/bootstrap_form_group_test.rb | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index fdaf4aa4f..349c641a0 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -309,11 +309,20 @@ def form_group_builder(method, options, html_options = nil) end unless options.delete(:skip_label) - label_class = hide_class if options.delete(:hide_label) + if options[:label].is_a?(Hash) + label_text = options[:label].delete(:text) + label_class = options[:label].delete(:class) + options.delete(:label) + end label_class ||= options.delete(:label_class) + label_class = hide_class if options.delete(:hide_label) + + if options[:label].is_a?(String) + label_text ||= options.delete(:label) + end - form_group_options.reverse_merge!(label: { - text: options.delete(:label), + form_group_options.merge!(label: { + text: label_text, class: label_class }) end diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index 8369b6f53..c6620e57b 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -7,21 +7,36 @@ def setup setup_test_fixture end - test "changing the label text" do + test "changing the label text via the label option parameter" do expected = %{
} assert_equal expected, @builder.text_field(:email, label: 'Email Address') end + test "changing the label text via the html_options label hash" do + expected = %{
} + assert_equal expected, @builder.text_field(:email, label: {text: 'Email Address'}) + end + test "hiding a label" do expected = %{
} assert_equal expected, @builder.text_field(:email, hide_label: true) end - test "adding a custom label class" do + test "adding a custom label class via the label_class parameter" do expected = %{
} assert_equal expected, @builder.text_field(:email, label_class: 'btn') end + test "adding a custom label class via the html_options label hash" do + expected = %{
} + assert_equal expected, @builder.text_field(:email, label: {class: 'btn'}) + end + + test "adding a custom label and changing the label text via the html_options label hash" do + expected = %{
} + assert_equal expected, @builder.text_field(:email, label: {class: 'btn', text: "Email Address"}) + end + test "skipping a label" do expected = %{
} assert_equal expected, @builder.text_field(:email, skip_label: true)