From 28def01d54e35a8ac7f27f57a7bad45c65e88424 Mon Sep 17 00:00:00 2001 From: Uwe Kubosch Date: Mon, 11 Sep 2023 09:36:56 +0200 Subject: [PATCH] Add `bootstrap_fields_for` form helper. --- .../action_view_extensions/form_helper.rb | 2 +- test/bootstrap_fields_for_test.rb | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/bootstrap_fields_for_test.rb diff --git a/lib/bootstrap_form/action_view_extensions/form_helper.rb b/lib/bootstrap_form/action_view_extensions/form_helper.rb index aef6d228..334aa466 100644 --- a/lib/bootstrap_form/action_view_extensions/form_helper.rb +++ b/lib/bootstrap_form/action_view_extensions/form_helper.rb @@ -39,7 +39,7 @@ def bootstrap_form_tag(options={}, &block) bootstrap_form_for("", options, &block) end - def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block) + def bootstrap_fields_for(record_name, record_object=nil, options={}, &block) options[:builder] = BootstrapForm::FormBuilder fields_for(record_name, record_object, options, &block) end diff --git a/test/bootstrap_fields_for_test.rb b/test/bootstrap_fields_for_test.rb new file mode 100644 index 00000000..c8c79842 --- /dev/null +++ b/test/bootstrap_fields_for_test.rb @@ -0,0 +1,49 @@ +require_relative "test_helper" + +class BootstrapFieldsForTest < ActionView::TestCase + include BootstrapForm::ActionViewExtensions::FormHelper + + setup :setup_test_fixture + + test "bootstrap_fields_for helper works for associations" do + @user.address = Address.new(street: "123 Main Street") + + output = bootstrap_form_for(@user) do |_f| + bootstrap_fields_for @user.address do |af| + af.text_field(:street) + end + end + + expected = <<~HTML +
+ #{'' unless ::Rails::VERSION::STRING >= '6'} +
+ + +
+
+ HTML + assert_equivalent_html expected, output + end + + test "bootstrap_form_for helper works for serialized hash attributes" do + @user.preferences = { "favorite_color" => "cerulean" } + + output = bootstrap_form_for(@user) do |_f| + bootstrap_fields_for :preferences do |builder| + builder.text_field :favorite_color, value: @user.preferences["favorite_color"] + end + end + + expected = <<~HTML +
+ #{'' unless ::Rails::VERSION::STRING >= '6'} +
+ + +
+
+ HTML + assert_equivalent_html expected, output + end +end