From 36b1a0fc31d495872ef55629c31a2b8d09a55737 Mon Sep 17 00:00:00 2001 From: Alexander Tipugin Date: Tue, 2 Sep 2014 12:36:30 +0400 Subject: [PATCH 1/3] Allow to pass any attributes to wrapper (not just class) --- README.md | 10 +++++----- lib/bootstrap_form/form_builder.rb | 18 +++++++++++++++--- test/bootstrap_form_test.rb | 11 ++++++++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b8b2b4ee3..8e6177214 100644 --- a/README.md +++ b/README.md @@ -189,19 +189,19 @@ You can also prepend and append buttons. Note: The buttons must contain the <%= f.text_field :search, append: link_to("Go", "#", class: "btn btn-default") %> ``` -### Additional Form Group Classes +### Additional Form Group Attributes -If you want to add an additional css class to the form group div, you can use -the `wrapper_class: 'additional-class'` option. +If you want to add an additional css class or any other attribute to the form group div, you can use +the `wrapper_options: { class: 'additional-class', data: { foo: 'bar' } }` option. ```erb -<%= f.text_field :name, wrapper_class: 'has-warning' %> +<%= f.text_field :name, wrapper_options: { class: 'has-warning', data: { foo: 'bar' } } %> ``` Which produces the following output: ```erb -
+
diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 0877d7d58..35e090f51 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -255,14 +255,26 @@ def form_group_builder(method, options, html_options = nil) label = options.delete(:label) label_class = hide_class if options.delete(:hide_label) - wrapper_class = options.delete(:wrapper_class) + wrapper_options = options.delete(:wrapper_options) || {} help = options.delete(:help) icon = options.delete(:icon) label_col = options.delete(:label_col) control_col = options.delete(:control_col) layout = get_group_layout(options.delete(:layout)) - - form_group(method, id: options[:id], label: { text: label, class: label_class }, help: help, icon: icon, label_col: label_col, control_col: control_col, layout: layout, class: wrapper_class) do + form_group_options = { + id: options[:id], + label: { + text: label, + class: label_class + }, + help: help, + icon: icon, + label_col: label_col, + control_col: control_col, + layout: layout + }.merge(wrapper_options) + + form_group(method, form_group_options) do yield end end diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 300549ec1..89874afe6 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -729,7 +729,7 @@ def setup test "adds class to wrapped form_group by a field" do expected = %{
} - assert_equal expected, @builder.search_field(:misc, wrapper_class: 'none-margin') + assert_equal expected, @builder.search_field(:misc, wrapper_options: { class: 'none-margin' }) end test "adds class to wrapped form_group by a field with errors" do @@ -737,7 +737,7 @@ def setup @user.valid? expected = %{
can't be blank, is too short (minimum is 5 characters)
} - assert_equal expected, @builder.email_field(:email, wrapper_class: 'none-margin') + assert_equal expected, @builder.email_field(:email, wrapper_options: { class: 'none-margin' }) end test "adds class to wrapped form_group by a field with errors when bootstrap_form_for is used" do @@ -745,7 +745,7 @@ def setup @user.valid? output = bootstrap_form_for(@user) do |f| - f.text_field(:email, help: 'This is required', wrapper_class: 'none-margin') + f.text_field(:email, help: 'This is required', wrapper_options: { class: 'none-margin' }) end expected = %{
can't be blank, is too short (minimum is 5 characters)
} @@ -786,4 +786,9 @@ def setup expected = %{
Hallo
} assert_equal expected, output end + + test "adds data-attributes (or any other options) to wrapper" do + expected = %{
} + assert_equal expected, @builder.search_field(:misc, wrapper_options: { data: { foo: 'bar' } }) + end end From 7743fa6d0c4a5b5dc7e873d3c22bd3186005fac9 Mon Sep 17 00:00:00 2001 From: Alexander Tipugin Date: Tue, 2 Sep 2014 12:47:10 +0400 Subject: [PATCH 2/3] Use #reverse_merge instead of #merge --- lib/bootstrap_form/form_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 35e090f51..f57b3dc69 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -272,7 +272,7 @@ def form_group_builder(method, options, html_options = nil) label_col: label_col, control_col: control_col, layout: layout - }.merge(wrapper_options) + }.reverse_merge(wrapper_options) form_group(method, form_group_options) do yield From d658009b3be722f398137b21062c15ba5c84f0b0 Mon Sep 17 00:00:00 2001 From: Alexander Tipugin Date: Thu, 11 Sep 2014 17:08:46 +0400 Subject: [PATCH 3/3] Support both "wrapper" and "wrapper_class" options --- README.md | 6 ++++-- lib/bootstrap_form/form_builder.rb | 12 +++++++++--- test/bootstrap_form_test.rb | 8 ++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8e6177214..b113ff16e 100644 --- a/README.md +++ b/README.md @@ -192,10 +192,10 @@ You can also prepend and append buttons. Note: The buttons must contain the ### Additional Form Group Attributes If you want to add an additional css class or any other attribute to the form group div, you can use -the `wrapper_options: { class: 'additional-class', data: { foo: 'bar' } }` option. +the `wrapper: { class: 'additional-class', data: { foo: 'bar' } }` option. ```erb -<%= f.text_field :name, wrapper_options: { class: 'has-warning', data: { foo: 'bar' } } %> +<%= f.text_field :name, wrapper: { class: 'has-warning', data: { foo: 'bar' } } %> ``` Which produces the following output: @@ -207,6 +207,8 @@ Which produces the following output:
``` +You still can use `wrapper_class` option to set only a css class. This is just a short form of `wrapper: { class: 'additional-class' }`. + ### Checkboxes and Radios Checkboxes and radios should be placed inside of a `form_group` to render diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index f57b3dc69..c5bf0c23d 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -255,7 +255,8 @@ def form_group_builder(method, options, html_options = nil) label = options.delete(:label) label_class = hide_class if options.delete(:hide_label) - wrapper_options = options.delete(:wrapper_options) || {} + wrapper_class = options.delete(:wrapper_class) + wrapper_options = options.delete(:wrapper) help = options.delete(:help) icon = options.delete(:icon) label_col = options.delete(:label_col) @@ -271,8 +272,13 @@ def form_group_builder(method, options, html_options = nil) icon: icon, label_col: label_col, control_col: control_col, - layout: layout - }.reverse_merge(wrapper_options) + layout: layout, + class: wrapper_class + } + + if wrapper_options.is_a?(Hash) + form_group_options.reverse_merge!(wrapper_options) + end form_group(method, form_group_options) do yield diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 89874afe6..52e8bf051 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -729,7 +729,7 @@ def setup test "adds class to wrapped form_group by a field" do expected = %{
} - assert_equal expected, @builder.search_field(:misc, wrapper_options: { class: 'none-margin' }) + assert_equal expected, @builder.search_field(:misc, wrapper_class: 'none-margin') end test "adds class to wrapped form_group by a field with errors" do @@ -737,7 +737,7 @@ def setup @user.valid? expected = %{
can't be blank, is too short (minimum is 5 characters)
} - assert_equal expected, @builder.email_field(:email, wrapper_options: { class: 'none-margin' }) + assert_equal expected, @builder.email_field(:email, wrapper_class: 'none-margin') end test "adds class to wrapped form_group by a field with errors when bootstrap_form_for is used" do @@ -745,7 +745,7 @@ def setup @user.valid? output = bootstrap_form_for(@user) do |f| - f.text_field(:email, help: 'This is required', wrapper_options: { class: 'none-margin' }) + f.text_field(:email, help: 'This is required', wrapper_class: 'none-margin') end expected = %{
can't be blank, is too short (minimum is 5 characters)
} @@ -789,6 +789,6 @@ def setup test "adds data-attributes (or any other options) to wrapper" do expected = %{
} - assert_equal expected, @builder.search_field(:misc, wrapper_options: { data: { foo: 'bar' } }) + assert_equal expected, @builder.search_field(:misc, wrapper: { data: { foo: 'bar' } }) end end