From 42d2c5a03234c104b0d7aca5753cc3214e7b771f Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 12:12:15 -0800 Subject: [PATCH 01/15] Test case for #417. --- test/bootstrap_form_group_test.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index fd4e171cb..d96dda8ed 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -147,6 +147,32 @@ class BootstrapFormGroupTest < ActionView::TestCase assert_equivalent_xml expected, @builder.text_field(:email, prepend: '$', append: '.00') end + test "adding both prepend and append text with validation error" do + @user.email = nil + assert @user.invalid? + + expected = <<-HTML.strip_heredoc +
+ +
+ +
+
+ $
+
+ +
+ .00 +
+
can't be blank, is too short (minimum is 5 characters) +
+
+
+ HTML + # TODO: We should build the @builder properly from `bootstrap_form_for`, so it's easier to test errors. + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.text_field :email, prepend: '$', append: '.00' } + end + test "help messages for default forms" do expected = <<-HTML.strip_heredoc
From d7ef709e2cd8f651bfc1764e7a30996a91f551e5 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 12:45:03 -0800 Subject: [PATCH 02/15] Undoing mess created on wrong branch. --- lib/bootstrap_form/form_builder.rb | 4 ++-- lib/bootstrap_form/helpers/bootstrap.rb | 12 +++++++++- test/bootstrap_checkbox_test.rb | 32 ++++++++++++++++++++++++- test/bootstrap_radio_button_test.rb | 10 ++++---- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 16d6c9639..69cb5bb65 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -38,7 +38,7 @@ def initialize(object_name, object, template, options) define_method(with_method_name) do |name, options = {}| form_group_builder(name, options) do - prepend_and_append_input(options) do + prepend_and_append_input(name, options) do send(without_method_name, name, options) end end @@ -71,7 +71,7 @@ def file_field_with_bootstrap(name, options = {}) def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block) form_group_builder(method, options, html_options) do - prepend_and_append_input(options) do + prepend_and_append_input(method, options) do select_without_bootstrap(method, choices, options, html_options, &block) end end diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 342bb0abc..4ff7862f7 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -66,7 +66,15 @@ def custom_control(*args, &block) form_group_builder(name, options, &block) end - def prepend_and_append_input(options, &block) + ## + # Add prepend and append, if any, and error if any. + # If anything is added, the whole thing is wrapped in an input-group. + def prepend_and_append_and_error_input(name, options, &block) + puts "options[:help]: #{options[:help]}" + prepend_and_append_input(name, options, error_text: generate_help(name, options[:help]).to_s, &block) + end + + def prepend_and_append_input(name, options, error_text: nil, &block) options = options.extract!(:prepend, :append, :input_group_class) input_group_class = ["input-group", options[:input_group_class]].compact.join(' ') @@ -74,6 +82,8 @@ def prepend_and_append_input(options, &block) input = content_tag(:div, input_group_content(options[:prepend]), class: 'input-group-prepend') + input if options[:prepend] input << content_tag(:div, input_group_content(options[:append]), class: 'input-group-append') if options[:append] + input << error_text + # FIXME: TBC The following isn't right yet. Wrap if there were errors. Maybe??? input = content_tag(:div, input, class: input_group_class) unless options.empty? input end diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 048621d36..87d0702dc 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -144,8 +144,8 @@ class BootstrapCheckboxTest < ActionView::TestCase
+ With a help!
- With a help!
HTML @@ -451,4 +451,34 @@ class BootstrapCheckboxTest < ActionView::TestCase HTML assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', inline: true, disabled: true, custom: true}) end + + test 'collection_check_boxes renders error after last check box' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + @user.errors.add(:misc, "a box must be checked") + + expected = <<-HTML.strip_heredoc +
+ + +
+ +
+ + +
+
+ + +
a box must be checked
+
+
+
+ HTML + + actual = bootstrap_form_for(@user) do |f| + f.collection_check_boxes(:misc, collection, :id, :street) + end + + assert_equivalent_xml expected, actual + end end diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index 229526e72..3808dd858 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -99,8 +99,8 @@ class BootstrapRadioButtonTest < ActionView::TestCase + With a help! - With a help! HTML @@ -172,8 +172,8 @@ class BootstrapRadioButtonTest < ActionView::TestCase
+ With a help!
- With a help! HTML @@ -188,8 +188,8 @@ class BootstrapRadioButtonTest < ActionView::TestCase
+ With a help!
- With a help! HTML @@ -242,8 +242,8 @@ class BootstrapRadioButtonTest < ActionView::TestCase
+ With a help!
- With a help! HTML @@ -258,8 +258,8 @@ class BootstrapRadioButtonTest < ActionView::TestCase
+ With a help!
- With a help! HTML From 187aa6939ea04a259caf11a68a85b8689d083a0b Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 16:08:11 -0800 Subject: [PATCH 03/15] Remove test that accidentally leaked in from another branch. --- test/bootstrap_checkbox_test.rb | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 87d0702dc..9d159108f 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -451,34 +451,4 @@ class BootstrapCheckboxTest < ActionView::TestCase HTML assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', inline: true, disabled: true, custom: true}) end - - test 'collection_check_boxes renders error after last check box' do - collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - @user.errors.add(:misc, "a box must be checked") - - expected = <<-HTML.strip_heredoc -
- - -
- -
- - -
-
- - -
a box must be checked
-
-
-
- HTML - - actual = bootstrap_form_for(@user) do |f| - f.collection_check_boxes(:misc, collection, :id, :street) - end - - assert_equivalent_xml expected, actual - end end From b25ab775dccd47c028c2a4c9b45db83814a1b745 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 16:14:05 -0800 Subject: [PATCH 04/15] Working but needs cleanup #417. --- lib/bootstrap_form/form_builder.rb | 19 ++++++++----- lib/bootstrap_form/helpers/bootstrap.rb | 6 +++-- test/bootstrap_form_group_test.rb | 36 ++++++++++++++----------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 69cb5bb65..7930a945e 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -38,7 +38,7 @@ def initialize(object_name, object, template, options) define_method(with_method_name) do |name, options = {}| form_group_builder(name, options) do - prepend_and_append_input(name, options) do + prepend_and_append_and_error_input(name, options) do send(without_method_name, name, options) end end @@ -71,7 +71,7 @@ def file_field_with_bootstrap(name, options = {}) def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block) form_group_builder(method, options, html_options) do - prepend_and_append_input(method, options) do + prepend_and_append_and_error_input(method, options) do select_without_bootstrap(method, choices, options, html_options, &block) end end @@ -128,12 +128,15 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ end label_class = options[:label_class] + error_text = generate_help(name, options.delete(:help)).to_s if options[:custom] div_class = ["custom-control", "custom-checkbox"] div_class.append("custom-control-inline") if options[:inline] content_tag(:div, class: div_class.compact.join(" ")) do - checkbox_html.concat(label(label_name, label_description, class: ["custom-control-label", label_class].compact.join(" "))) + checkbox_html + .concat(label(label_name, label_description, class: ["custom-control-label", label_class].compact.join(" "))) + .concat(error_text) end else wrapper_class = "form-check" @@ -143,6 +146,7 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ .concat(label(label_name, label_description, { class: ["form-check-label", label_class].compact.join(" ") }.merge(options[:id].present? ? { for: options[:id] } : {}))) + .concat(error_text) end end end @@ -162,12 +166,15 @@ def radio_button_with_bootstrap(name, value, *args) disabled_class = " disabled" if options[:disabled] label_class = options[:label_class] + error_text = generate_help(name, options.delete(:help)).to_s if options[:custom] div_class = ["custom-control", "custom-radio"] div_class.append("custom-control-inline") if options[:inline] content_tag(:div, class: div_class.compact.join(" ")) do - radio_html.concat(label(name, options[:label], value: value, class: ["custom-control-label", label_class].compact.join(" "))) + radio_html + .concat(label(name, options[:label], value: value, class: ["custom-control-label", label_class].compact.join(" "))) + .concat(error_text) end else wrapper_class = "form-check" @@ -176,6 +183,7 @@ def radio_button_with_bootstrap(name, value, *args) content_tag(:div, class: "#{wrapper_class}#{disabled_class}") do radio_html .concat(label(name, options[:label], { value: value, class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {}))) + .concat(error_text) end end end @@ -211,7 +219,6 @@ def form_group(*args, &block) content_tag(:div, options.except(:id, :label, :help, :icon, :label_col, :control_col, :layout)) do label = generate_label(options[:id], name, options[:label], options[:label_col], options[:layout]) if options[:label] control = capture(&block).to_s - control.concat(generate_help(name, options[:help]).to_s) if get_group_layout(options[:layout]) == :horizontal control_class = options[:control_col] || control_col @@ -324,7 +331,7 @@ def form_group_builder(method, options, html_options = nil) wrapper_class = css_options.delete(:wrapper_class) wrapper_options = css_options.delete(:wrapper) - help = options.delete(:help) + help = options[:help] icon = options.delete(:icon) label_col = options.delete(:label_col) control_col = options.delete(:control_col) diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 4ff7862f7..0385439fc 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -69,9 +69,11 @@ def custom_control(*args, &block) ## # Add prepend and append, if any, and error if any. # If anything is added, the whole thing is wrapped in an input-group. + # FIXME: I think this has to be the other way around, i.e. we need to be + # able to render fields with error or help, but only sometimes with + # prepend and append. def prepend_and_append_and_error_input(name, options, &block) - puts "options[:help]: #{options[:help]}" - prepend_and_append_input(name, options, error_text: generate_help(name, options[:help]).to_s, &block) + prepend_and_append_input(name, options, error_text: generate_help(name, options.delete(:help)).to_s, &block) end def prepend_and_append_input(name, options, error_text: nil, &block) diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index d96dda8ed..346bc8347 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -329,22 +329,26 @@ class BootstrapFormGroupTest < ActionView::TestCase assert_equivalent_xml expected, output end - test 'form_group renders the "error" class and message corrrectly when object is invalid' do - @user.email = nil - assert @user.invalid? - - output = @builder.form_group :email do - %{

Bar

}.html_safe - end - - expected = <<-HTML.strip_heredoc -
-

Bar

-
can't be blank, is too short (minimum is 5 characters)
-
- HTML - assert_equivalent_xml expected, output - end + # test 'form_group renders the "error" class and message corrrectly when object is invalid' do + # # It could be said that the meaning of "form-group" has changed in Bootstrap 4, + # # and that's why it shouldn't be outputting the error message anymore. Which + # # would make this test case no longer valid. + # # THIS TEST WAS REMOVED FROM v2.7. + # @user.email = nil + # assert @user.invalid? + # + # output = @builder.form_group :email do + # %{

Bar

}.html_safe + # end + # + # expected = <<-HTML.strip_heredoc + #
+ #

Bar

+ #
can't be blank, is too short (minimum is 5 characters)
+ #
+ # HTML + # assert_equivalent_xml expected, output + # end test "adds class to wrapped form_group by a field" do expected = <<-HTML.strip_heredoc From 463486e7b2a495e80781124add377a2f5d942cc5 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 16:53:17 -0800 Subject: [PATCH 05/15] Partially cleaned up. Added method for error and help and optionally prepend and append. --- lib/bootstrap_form/helpers/bootstrap.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 0385439fc..e0134c987 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -77,16 +77,28 @@ def prepend_and_append_and_error_input(name, options, &block) end def prepend_and_append_input(name, options, error_text: nil, &block) - options = options.extract!(:prepend, :append, :input_group_class) + control_error_help(name, + options, + prepend: options.delete(:prepend), + append: options.delete(:append), + error_text: error_text, + &block) + end + + ## + # Render a block, and add its error or help. + # Add prepend and append if provided. + def control_error_help(name, options, prepend: nil, append: nil, error_text: nil, &block) + options = options.extract!(:input_group_class) input_group_class = ["input-group", options[:input_group_class]].compact.join(' ') input = capture(&block) - input = content_tag(:div, input_group_content(options[:prepend]), class: 'input-group-prepend') + input if options[:prepend] - input << content_tag(:div, input_group_content(options[:append]), class: 'input-group-append') if options[:append] + input = content_tag(:div, input_group_content(prepend), class: 'input-group-prepend') + input if prepend + input << content_tag(:div, input_group_content(append), class: 'input-group-append') if append input << error_text # FIXME: TBC The following isn't right yet. Wrap if there were errors. Maybe??? - input = content_tag(:div, input, class: input_group_class) unless options.empty? + input = content_tag(:div, input, class: input_group_class) unless options.empty? && prepend.nil? && append.nil? input end From 9e667061958cd373be47b10d66eae80df4370ea4 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 3 Feb 2018 16:58:37 -0800 Subject: [PATCH 06/15] Remove unneeded method and parameters. --- lib/bootstrap_form/form_builder.rb | 4 ++-- lib/bootstrap_form/helpers/bootstrap.rb | 16 +++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 7930a945e..54f0e0c8b 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -38,7 +38,7 @@ def initialize(object_name, object, template, options) define_method(with_method_name) do |name, options = {}| form_group_builder(name, options) do - prepend_and_append_and_error_input(name, options) do + prepend_and_append_input(name, options) do send(without_method_name, name, options) end end @@ -71,7 +71,7 @@ def file_field_with_bootstrap(name, options = {}) def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block) form_group_builder(method, options, html_options) do - prepend_and_append_and_error_input(method, options) do + prepend_and_append_input(method, options) do select_without_bootstrap(method, choices, options, html_options, &block) end end diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index e0134c987..3e12a5925 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -69,26 +69,20 @@ def custom_control(*args, &block) ## # Add prepend and append, if any, and error if any. # If anything is added, the whole thing is wrapped in an input-group. - # FIXME: I think this has to be the other way around, i.e. we need to be - # able to render fields with error or help, but only sometimes with - # prepend and append. - def prepend_and_append_and_error_input(name, options, &block) - prepend_and_append_input(name, options, error_text: generate_help(name, options.delete(:help)).to_s, &block) - end - - def prepend_and_append_input(name, options, error_text: nil, &block) + def prepend_and_append_input(name, options, &block) control_error_help(name, options, prepend: options.delete(:prepend), append: options.delete(:append), - error_text: error_text, &block) end ## # Render a block, and add its error or help. - # Add prepend and append if provided. - def control_error_help(name, options, prepend: nil, append: nil, error_text: nil, &block) + # Add prepend and append if provided, and wrap if they were, or if + # an input_group_class was provided. + def control_error_help(name, options, prepend: nil, append: nil, &block) + error_text = generate_help(name, options.delete(:help)).to_s options = options.extract!(:input_group_class) input_group_class = ["input-group", options[:input_group_class]].compact.join(' ') From e83bf64b9f1ea00e6cb8d8aa7858a140729a5e24 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:27:28 -0800 Subject: [PATCH 07/15] Test selects error. --- test/bootstrap_selects_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 216119e21..cd6832c20 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -24,6 +24,21 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) assert_equivalent_xml expected, @builder.time_zone_select(:misc) end + test "time zone selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + expected = <<-HTML.strip_heredoc +
+ +
+ + +
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.time_zone_select(:misc) } + end + test "selects are wrapped correctly" do expected = <<-HTML.strip_heredoc
From a2b45868d81ce422b06da5d991cbe3362458c5ae Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:27:28 -0800 Subject: [PATCH 08/15] Test selects error. --- test/bootstrap_selects_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 216119e21..cd6832c20 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -24,6 +24,21 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) assert_equivalent_xml expected, @builder.time_zone_select(:misc) end + test "time zone selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + expected = <<-HTML.strip_heredoc +
+ +
+ + +
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.time_zone_select(:misc) } + end + test "selects are wrapped correctly" do expected = <<-HTML.strip_heredoc
From e48451d2cf461c6d306f8304baaaad56461d1817 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:35:28 -0800 Subject: [PATCH 09/15] time_zone_select works for new error placement. --- lib/bootstrap_form/form_builder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 54f0e0c8b..c5c65c8b2 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -97,7 +97,9 @@ def grouped_collection_select_with_bootstrap(method, collection, group_method, g def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {}) form_group_builder(method, options, html_options) do - time_zone_select_without_bootstrap(method, priority_zones, options, html_options) + control_error_help(method, options) do + time_zone_select_without_bootstrap(method, priority_zones, options, html_options) + end end end From b2d832a9d6347c4f773899f803f6b7db8a3e35f9 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:46:51 -0800 Subject: [PATCH 10/15] Add tests for date, time, datetime selects. --- test/bootstrap_selects_test.rb | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index cd6832c20..9c95defdb 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -245,6 +245,33 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "date selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.date_select(:misc) } + end + end + test "date selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3)) do expected = <<-HTML.strip_heredoc @@ -319,6 +346,34 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "time selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + + + : + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.time_select(:misc) } + end + end + test "time selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do expected = <<-HTML.strip_heredoc @@ -399,6 +454,41 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "datetime selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + + — + + : + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, @builder.datetime_select(:misc) + end + end + test "datetime selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do expected = <<-HTML.strip_heredoc From 073a1e3862aa3cdd0a1da2926ea108365b71a357 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:46:51 -0800 Subject: [PATCH 11/15] Add tests for date, time, datetime selects. --- test/bootstrap_selects_test.rb | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index cd6832c20..9c95defdb 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -245,6 +245,33 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "date selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.date_select(:misc) } + end + end + test "date selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3)) do expected = <<-HTML.strip_heredoc @@ -319,6 +346,34 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "time selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + + + : + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.time_select(:misc) } + end + end + test "time selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do expected = <<-HTML.strip_heredoc @@ -399,6 +454,41 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) end end + test "datetime selects are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do + expected = <<-HTML.strip_heredoc +
+ +
+ +
+ + + + — + + : + +
+
error for test
+
+
+ HTML + assert_equivalent_xml expected, @builder.datetime_select(:misc) + end + end + test "datetime selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do expected = <<-HTML.strip_heredoc From 844e1335a2eb206dcbc8bd1971ccbffee426d5d9 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 4 Feb 2018 16:52:09 -0800 Subject: [PATCH 12/15] date, time, datetime tests pass with old implementation. --- test/bootstrap_selects_test.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 9c95defdb..a746ad24a 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -253,14 +253,14 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
-
- #{options_range(start: 2007, stop: 2017, selected: 2012)} - #{options_range(start: 1, stop: 12, selected: 2, months: true)} - #{options_range(start: 1, stop: 31, selected: 3)}
@@ -354,15 +354,15 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
-
+
- #{options_range(start: "00", stop: "23", selected: "12")} : - #{options_range(start: "00", stop: "59", selected: "00")}
@@ -462,22 +462,22 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
-
- #{options_range(start: 2007, stop: 2017, selected: 2012)} - #{options_range(start: 1, stop: 12, selected: 2, months: true)} - #{options_range(start: 1, stop: 31, selected: 3)} — - #{options_range(start: "00", stop: "23", selected: "12")} : - #{options_range(start: "00", stop: "59", selected: "00")}
@@ -485,7 +485,7 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
HTML - assert_equivalent_xml expected, @builder.datetime_select(:misc) + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.datetime_select(:misc) } end end From 0a73344c00b89c28e2c33653ed7f77917e95abdc Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Mon, 5 Feb 2018 08:01:14 -0800 Subject: [PATCH 13/15] Date, time, and datetime for new implementation. --- lib/bootstrap_form/form_builder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index c5c65c8b2..46183aea3 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -53,7 +53,9 @@ def initialize(object_name, object, template, options) define_method(with_method_name) do |name, options = {}, html_options = {}| form_group_builder(name, options, html_options) do - content_tag(:div, send(without_method_name, name, options, html_options), class: control_specific_class(method_name)) + control_error_help(name, options) do + content_tag(:div, send(without_method_name, name, options, html_options), class: control_specific_class(method_name)) + end end end From 365dbf21e718cae9d7b3892e78124f583d19ce9c Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Mon, 5 Feb 2018 08:33:03 -0800 Subject: [PATCH 14/15] Tests for file and collection selects. --- test/bootstrap_fields_test.rb | 15 +++++++++++++++ test/bootstrap_selects_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index 60c61fff7..98f13801b 100644 --- a/test/bootstrap_fields_test.rb +++ b/test/bootstrap_fields_test.rb @@ -65,6 +65,21 @@ class BootstrapFieldsTest < ActionView::TestCase assert_equivalent_xml expected, @builder.file_field(:misc) end + test "file fields are wrapped correctly with error" do + @user.errors.add(:misc, "error for test") + expected = <<-HTML.strip_heredoc +
+ +
+ + +
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.file_field(:misc) } + end + test "hidden fields are supported" do expected = %{} assert_equivalent_xml expected, @builder.hidden_field(:misc) diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index a746ad24a..2ccfa7c8c 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -165,6 +165,21 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name) end + test "collection_selects are wrapped correctly with error" do + @user.errors.add(:status, "error for test") + expected = <<-HTML.strip_heredoc +
+ +
+ + +
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.collection_select(:status, [], :id, :name) } + end + test "collection_selects with options are wrapped correctly" do expected = <<-HTML.strip_heredoc
@@ -199,6 +214,21 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s) end + test "grouped_collection_selects are wrapped correctly with error" do + @user.errors.add(:status, "error for test") + expected = <<-HTML.strip_heredoc +
+ +
+ + +
error for test
+
+
+ HTML + assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s) } + end + test "grouped_collection_selects with options are wrapped correctly" do expected = <<-HTML.strip_heredoc
From a15d06d0d577af306b1d32a04012401eb28a8a52 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Mon, 5 Feb 2018 08:59:08 -0800 Subject: [PATCH 15/15] File and collection selects with new implementation. --- lib/bootstrap_form/form_builder.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 46183aea3..dfe298b3b 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -65,7 +65,9 @@ def initialize(object_name, object, template, options) def file_field_with_bootstrap(name, options = {}) options = options.reverse_merge(control_class: 'form-control-file') form_group_builder(name, options) do - file_field_without_bootstrap(name, options) + control_error_help(name, options) do + file_field_without_bootstrap(name, options) + end end end @@ -83,7 +85,9 @@ def select_with_bootstrap(method, choices = nil, options = {}, html_options = {} def collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {}) form_group_builder(method, options, html_options) do - collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options) + control_error_help(method, options) do + collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options) + end end end @@ -91,7 +95,9 @@ def collection_select_with_bootstrap(method, collection, value_method, text_meth def grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) form_group_builder(method, options, html_options) do - grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) + control_error_help(method, options) do + grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) + end end end