From 30d45d8bc1bfcebfff9a71041bf4c484af1a7a2f Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Fri, 31 Oct 2025 18:24:44 +0000 Subject: [PATCH 01/10] Use Rails' configuration options --- lib/bootstrap_form.rb | 5 +++++ lib/bootstrap_form/configuration.rb | 13 +++++++++++++ lib/bootstrap_form/engine.rb | 11 +++++++++++ test/bootstrap_configuration_test.rb | 28 +++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/bootstrap_form.rb b/lib/bootstrap_form.rb index b7315ec97..1afdcad12 100644 --- a/lib/bootstrap_form.rb +++ b/lib/bootstrap_form.rb @@ -3,6 +3,7 @@ require "action_view" require "action_pack" require "bootstrap_form/action_view_extensions/form_helper" +require "bootstrap_form/configuration" module BootstrapForm extend ActiveSupport::Autoload @@ -32,6 +33,10 @@ def config def configure yield config end + + def deprecator + @deprecator ||= ActiveSupport::Deprecation.new("a future release", "BootstrapForm") + end end mattr_accessor :field_error_proc diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index 43c4bb3dc..e123cef60 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -7,7 +7,12 @@ def default_form_attributes=(attributes) when nil @default_form_attributes = {} when Hash + BootstrapForm.deprecator.warn(<<~MESSAGE.squish) + BootstrapForm::Configuation#default_form_attributes will be removed in a future release. + Please use Rails.application.config.bootstrap_form.default_form_attributes instead. + MESSAGE @default_form_attributes = attributes + Rails.application.config.bootstrap_form.default_form_attributes = attributes else raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}" end @@ -19,4 +24,12 @@ def default_form_attributes {} end end + + mattr_accessor :configuration, default: nil + + class << self + def configure + yield(configuration) if block_given? + end + end end diff --git a/lib/bootstrap_form/engine.rb b/lib/bootstrap_form/engine.rb index b27bcda6a..9ffcf9a83 100644 --- a/lib/bootstrap_form/engine.rb +++ b/lib/bootstrap_form/engine.rb @@ -6,5 +6,16 @@ module BootstrapForm class Engine < Rails::Engine config.eager_load_namespaces << BootstrapForm config.autoload_paths << File.expand_path("lib", __dir__) + + config.bootstrap_form = ActiveSupport::OrderedOptions.new + config.bootstrap_form.default_form_attributes = {} + + initializer "bootstrap_form.configure" do |app| + BootstrapForm.configuration = app.config.bootstrap_form + end + + initializer "bootstrap_form.deprecator" do |app| + app.deprecators[:bootstrap_form] = BootstrapForm.deprecator + end end end diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 9987f0396..94ba191a3 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -3,6 +3,13 @@ require_relative "test_helper" class BootstrapConfigurationTest < ActionView::TestCase + teardown do + # Unfortunately, it seems we have to manually reset each of the configuration options + # that we change in our test cases. + Rails.application.config.bootstrap_form.default_form_attributes = {} + Rails.application.config.bootstrap_form.bogon = nil + end + test "has default form attributes" do config = BootstrapForm::Configuration.new @@ -11,7 +18,9 @@ class BootstrapConfigurationTest < ActionView::TestCase test "allows to set default_form_attributes with custom value" do config = BootstrapForm::Configuration.new - config.default_form_attributes = { foo: "bar" } + assert_deprecated(BootstrapForm.deprecator) do + config.default_form_attributes = { foo: "bar" } + end assert_equal({ foo: "bar" }, config.default_form_attributes) end @@ -31,4 +40,21 @@ class BootstrapConfigurationTest < ActionView::TestCase end assert_equal("Unsupported default_form_attributes [1, 2, 3]", exception.message) end + + test "Use Rails configuration" do + assert_nil Rails.application.config.bootstrap_form.bogon + Rails.application.config.bootstrap_form.bogon = true + assert Rails.application.config.bootstrap_form.bogon + end + + test "Support legacy configuration from Rails configuration" do + assert_equal({}, Rails.application.config.bootstrap_form.default_form_attributes) + + config = BootstrapForm::Configuration.new + assert_deprecated(BootstrapForm.deprecator) do + config.default_form_attributes = { foo: "bar" } + end + + assert_equal({ foo: "bar" }, Rails.application.config.bootstrap_form.default_form_attributes) + end end From 27d1e55a55aa90d06d5751a65457249c81bf584b Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 1 Nov 2025 21:03:58 +0000 Subject: [PATCH 02/10] Deprecations of old configuration methods --- lib/bootstrap_form.rb | 9 +++++---- lib/bootstrap_form/configuration.rb | 12 +++++++----- lib/bootstrap_form/form_builder.rb | 2 +- test/bootstrap_configuration_test.rb | 12 +++++++++--- test/bootstrap_form_test.rb | 4 ++-- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/bootstrap_form.rb b/lib/bootstrap_form.rb index 1afdcad12..04001935b 100644 --- a/lib/bootstrap_form.rb +++ b/lib/bootstrap_form.rb @@ -27,11 +27,12 @@ def eager_load! end def config - @config ||= BootstrapForm::Configuration.new - end + deprecator.warn(<<~MESSAGE.squish) + BootstrapForm.config will be removed in a future release. + Please use BootstrapForm.configuration instead. + MESSAGE - def configure - yield config + configuration end def deprecator diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index e123cef60..da4f1624d 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -8,8 +8,8 @@ def default_form_attributes=(attributes) @default_form_attributes = {} when Hash BootstrapForm.deprecator.warn(<<~MESSAGE.squish) - BootstrapForm::Configuation#default_form_attributes will be removed in a future release. - Please use Rails.application.config.bootstrap_form.default_form_attributes instead. + BootstrapForm::Configuration#default_form_attributes= will be removed in a future release. + Please use Rails.application.config.bootstrap_form.default_form_attributes= instead. MESSAGE @default_form_attributes = attributes Rails.application.config.bootstrap_form.default_form_attributes = attributes @@ -19,9 +19,11 @@ def default_form_attributes=(attributes) end def default_form_attributes - return @default_form_attributes if defined? @default_form_attributes - - {} + BootstrapForm.deprecator.warn(<<~MESSAGE.squish) + BootstrapForm::Configuration#default_form_attributes will be removed in a future release. + Please use Rails.application.config.bootstrap_form.default_form_attributes instead. + MESSAGE + Rails.application.config.bootstrap_form.default_form_attributes end end diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index a3dab4b1b..96c6c9a39 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -71,7 +71,7 @@ def initialize(object_name, object, template, options) def add_default_form_attributes_and_form_inline(options) options[:html] ||= {} - options[:html].reverse_merge!(BootstrapForm.config.default_form_attributes) + options[:html].reverse_merge!(BootstrapForm.configuration.default_form_attributes) return unless options[:layout] == :inline diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 94ba191a3..72cad225f 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -13,7 +13,9 @@ class BootstrapConfigurationTest < ActionView::TestCase test "has default form attributes" do config = BootstrapForm::Configuration.new - assert_equal({}, config.default_form_attributes) + assert_deprecated(BootstrapForm.deprecator) do + assert_equal({}, config.default_form_attributes) + end end test "allows to set default_form_attributes with custom value" do @@ -22,14 +24,18 @@ class BootstrapConfigurationTest < ActionView::TestCase config.default_form_attributes = { foo: "bar" } end - assert_equal({ foo: "bar" }, config.default_form_attributes) + assert_deprecated(BootstrapForm.deprecator) do + assert_equal({ foo: "bar" }, config.default_form_attributes) + end end test "allows to set default_form_attributes with nil" do config = BootstrapForm::Configuration.new config.default_form_attributes = nil - assert_equal({}, config.default_form_attributes) + assert_deprecated(BootstrapForm.deprecator) do + assert_equal({}, config.default_form_attributes) + end end test "does not allow to set default_form_attributes with unsupported value" do diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 6bd2deb08..bfd0b51ec 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -377,7 +377,7 @@ def warn(message, ...) end test "allows to set blank default form attributes via configuration" do - BootstrapForm.config.stubs(:default_form_attributes).returns({}) + BootstrapForm.configuration.stubs(:default_form_attributes).returns({}) expected = <<~HTML
@@ -386,7 +386,7 @@ def warn(message, ...) end test "allows to set custom default form attributes via configuration" do - BootstrapForm.config.stubs(:default_form_attributes).returns(foo: "bar") + BootstrapForm.configuration.stubs(:default_form_attributes).returns(foo: "bar") expected = <<~HTML
From 84d027ab046c64cbce654428f5269c12db4d185c Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Thu, 6 Nov 2025 18:56:01 +0000 Subject: [PATCH 03/10] Work with initializer The code in initializers/bootstrap_form.rb runs before the engine is initialized, so I had to move a few things around. --- lib/bootstrap_form/configuration.rb | 2 +- lib/bootstrap_form/engine.rb | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index da4f1624d..02103f76d 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -27,7 +27,7 @@ def default_form_attributes end end - mattr_accessor :configuration, default: nil + mattr_accessor :configuration, default: ActiveSupport::OrderedOptions.new class << self def configure diff --git a/lib/bootstrap_form/engine.rb b/lib/bootstrap_form/engine.rb index 9ffcf9a83..676a1fc2e 100644 --- a/lib/bootstrap_form/engine.rb +++ b/lib/bootstrap_form/engine.rb @@ -7,12 +7,8 @@ class Engine < Rails::Engine config.eager_load_namespaces << BootstrapForm config.autoload_paths << File.expand_path("lib", __dir__) - config.bootstrap_form = ActiveSupport::OrderedOptions.new - config.bootstrap_form.default_form_attributes = {} - - initializer "bootstrap_form.configure" do |app| - BootstrapForm.configuration = app.config.bootstrap_form - end + config.bootstrap_form = BootstrapForm.configuration + config.bootstrap_form.default_form_attributes ||= {} initializer "bootstrap_form.deprecator" do |app| app.deprecators[:bootstrap_form] = BootstrapForm.deprecator From 6cab142c536a19e2d04f9ff5237322cd30b6afcf Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Thu, 6 Nov 2025 20:03:19 +0000 Subject: [PATCH 04/10] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f652ee931..b233c2d3b 100644 --- a/README.md +++ b/README.md @@ -226,13 +226,13 @@ Generated HTML: ## Configuration -`bootstrap_form` can be used out-of-the-box without any configuration. However, `bootstrap_form` does have an optional configuration file at `config/initializers/bootstrap_form.rb` for setting options that affect all generated forms in an application. +`bootstrap_form` can be used out-of-the-box without any configuration. However, `bootstrap_form` does have an optional configuration file at `config/initializers/bootstrap_form.rb` for setting options that affect all generated forms in an application, or for enabling or disabling new functionality that might not be compatible with your application. The current configuration options are: | Option | Default value | Description | |---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `default_form_attributes` | | `bootstrap_form` versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. `bootstrap_form` version 5 drops this attribute by default. Set this option to `{ role: "form" }` to make forms non-compliant with W3C, but generate the `role="form"` attribute like `bootstrap_form` versions 3 and 4. | +| `default_form_attributes` | {} | `bootstrap_form` versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a **warning** on forms with a role="form" attribute. `bootstrap_form` version 5 drops this attribute by default. Set this option to `{ role: "form" }` to make forms non-compliant with W3C, but generate the `role="form"` attribute like `bootstrap_form` versions 3 and 4. | Example: From 9ca8944113d956d75ad69a5974e1b08a3aaf0631 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Thu, 6 Nov 2025 22:18:54 +0000 Subject: [PATCH 05/10] Remove unnecessary change to config --- lib/bootstrap_form.rb | 9 --------- lib/bootstrap_form/configuration.rb | 12 ++++++------ lib/bootstrap_form/engine.rb | 2 +- lib/bootstrap_form/form_builder.rb | 2 +- test/bootstrap_form_test.rb | 4 ++-- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/bootstrap_form.rb b/lib/bootstrap_form.rb index 04001935b..5109147cf 100644 --- a/lib/bootstrap_form.rb +++ b/lib/bootstrap_form.rb @@ -26,15 +26,6 @@ def eager_load! BootstrapForm::Inputs.eager_load! end - def config - deprecator.warn(<<~MESSAGE.squish) - BootstrapForm.config will be removed in a future release. - Please use BootstrapForm.configuration instead. - MESSAGE - - configuration - end - def deprecator @deprecator ||= ActiveSupport::Deprecation.new("a future release", "BootstrapForm") end diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index 02103f76d..fdd2e0d25 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -9,10 +9,10 @@ def default_form_attributes=(attributes) when Hash BootstrapForm.deprecator.warn(<<~MESSAGE.squish) BootstrapForm::Configuration#default_form_attributes= will be removed in a future release. - Please use Rails.application.config.bootstrap_form.default_form_attributes= instead. + Please use BootstrapForm.config.default_form_attributes= instead. MESSAGE @default_form_attributes = attributes - Rails.application.config.bootstrap_form.default_form_attributes = attributes + BootstrapForm.config.default_form_attributes = attributes else raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}" end @@ -21,17 +21,17 @@ def default_form_attributes=(attributes) def default_form_attributes BootstrapForm.deprecator.warn(<<~MESSAGE.squish) BootstrapForm::Configuration#default_form_attributes will be removed in a future release. - Please use Rails.application.config.bootstrap_form.default_form_attributes instead. + Please use BootstrapForm.config.default_form_attributes instead. MESSAGE - Rails.application.config.bootstrap_form.default_form_attributes + BootstrapForm.config.default_form_attributes end end - mattr_accessor :configuration, default: ActiveSupport::OrderedOptions.new + mattr_accessor :config, default: ActiveSupport::OrderedOptions.new class << self def configure - yield(configuration) if block_given? + yield(config) if block_given? end end end diff --git a/lib/bootstrap_form/engine.rb b/lib/bootstrap_form/engine.rb index 676a1fc2e..5f822ce78 100644 --- a/lib/bootstrap_form/engine.rb +++ b/lib/bootstrap_form/engine.rb @@ -7,7 +7,7 @@ class Engine < Rails::Engine config.eager_load_namespaces << BootstrapForm config.autoload_paths << File.expand_path("lib", __dir__) - config.bootstrap_form = BootstrapForm.configuration + config.bootstrap_form = BootstrapForm.config config.bootstrap_form.default_form_attributes ||= {} initializer "bootstrap_form.deprecator" do |app| diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 96c6c9a39..a3dab4b1b 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -71,7 +71,7 @@ def initialize(object_name, object, template, options) def add_default_form_attributes_and_form_inline(options) options[:html] ||= {} - options[:html].reverse_merge!(BootstrapForm.configuration.default_form_attributes) + options[:html].reverse_merge!(BootstrapForm.config.default_form_attributes) return unless options[:layout] == :inline diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index bfd0b51ec..6bd2deb08 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -377,7 +377,7 @@ def warn(message, ...) end test "allows to set blank default form attributes via configuration" do - BootstrapForm.configuration.stubs(:default_form_attributes).returns({}) + BootstrapForm.config.stubs(:default_form_attributes).returns({}) expected = <<~HTML
@@ -386,7 +386,7 @@ def warn(message, ...) end test "allows to set custom default form attributes via configuration" do - BootstrapForm.configuration.stubs(:default_form_attributes).returns(foo: "bar") + BootstrapForm.config.stubs(:default_form_attributes).returns(foo: "bar") expected = <<~HTML
From 1c7bfd698558d6cc1e5c11a37a406757571d465e Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sun, 16 Nov 2025 01:30:22 +0000 Subject: [PATCH 06/10] Demo app use demo Gemfile --- .yarnrc | 2 +- demo/config/boot.rb | 10 ++++++---- test/test_helper.rb | 4 ---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.yarnrc b/.yarnrc index 319bc7507..adf41e8f3 100644 --- a/.yarnrc +++ b/.yarnrc @@ -2,4 +2,4 @@ # yarn lockfile v1 -lastUpdateCheck 1762552807228 +lastUpdateCheck 1763256402231 diff --git a/demo/config/boot.rb b/demo/config/boot.rb index 6d349b911..f64f71ce6 100644 --- a/demo/config/boot.rb +++ b/demo/config/boot.rb @@ -1,5 +1,7 @@ -# Set up gems listed in the Gemfile. -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __dir__) +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) -require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) -$LOAD_PATH.unshift File.expand_path("../../lib", __dir__) +require "bundler/setup" # Set up gems listed in the Gemfile. +# Only run the following line if we bootsnap is loaded, because we don't load it for the individual tests. +# The individual tests are the ones in `..`. +# They do load this file, for reasons. +require "bootsnap/setup" if Gem.loaded_specs.has_key?("bootsnap") # Speed up boot time by caching expensive operations. diff --git a/test/test_helper.rb b/test/test_helper.rb index 1f117396e..dcfed122a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,10 +9,6 @@ Warning.ignore(:not_reached, mail_gem_path) Warning.ignore(/warning: assigned but unused variable - testEof/, mail_gem_path) -require "diffy" -require "nokogiri" -require "equivalent-xml" - require_relative "../demo/config/environment" require "rails/test_help" require "mocha/minitest" From d6bf9d1e1b32293a5d7f7686370c5006242e4ff4 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Wed, 19 Nov 2025 22:35:12 +0000 Subject: [PATCH 07/10] Tweaks to demo/config/boot.rb after rebase --- demo/config/boot.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/demo/config/boot.rb b/demo/config/boot.rb index f64f71ce6..4145fc8da 100644 --- a/demo/config/boot.rb +++ b/demo/config/boot.rb @@ -1,6 +1,8 @@ -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) +# Set up gems listed in the Gemfile. +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __dir__) -require "bundler/setup" # Set up gems listed in the Gemfile. +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) +$LOAD_PATH.unshift File.expand_path("../../lib", __dir__) # Only run the following line if we bootsnap is loaded, because we don't load it for the individual tests. # The individual tests are the ones in `..`. # They do load this file, for reasons. From 040de01893b582e642c7a71b7539b770d5e3a9f4 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Fri, 21 Nov 2025 01:42:34 +0000 Subject: [PATCH 08/10] Use the appropriate test base calss --- test/bootstrap_configuration_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 72cad225f..4bf1593ba 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -2,7 +2,7 @@ require_relative "test_helper" -class BootstrapConfigurationTest < ActionView::TestCase +class BootstrapConfigurationTest < ActiveSupport::TestCase teardown do # Unfortunately, it seems we have to manually reset each of the configuration options # that we change in our test cases. From b71c6fa714da20526d58a032846ea6f92c4ce657 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Fri, 21 Nov 2025 01:42:58 +0000 Subject: [PATCH 09/10] Cover initialization --- lib/bootstrap_form/engine.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/bootstrap_form/engine.rb b/lib/bootstrap_form/engine.rb index 5f822ce78..8f7c71513 100644 --- a/lib/bootstrap_form/engine.rb +++ b/lib/bootstrap_form/engine.rb @@ -10,6 +10,10 @@ class Engine < Rails::Engine config.bootstrap_form = BootstrapForm.config config.bootstrap_form.default_form_attributes ||= {} + initializer "bootstrap_form.configure" do |app| + BootstrapForm.config = app.config.bootstrap_form + end + initializer "bootstrap_form.deprecator" do |app| app.deprecators[:bootstrap_form] = BootstrapForm.deprecator end From 3a69f7e45b826c1e294b94ca3c4d1dcbc1b47538 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Sat, 22 Nov 2025 22:08:31 +0000 Subject: [PATCH 10/10] Tell RuboCop we don't care about method length --- .rubocop.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c1a991fc6..e16064e5e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -54,11 +54,7 @@ Metrics/ClassLength: - "test/**/*" Metrics/MethodLength: - Max: 12 - Exclude: - - "demo/db/migrate/*" - - "demo/test/**/*" - - "test/**/*" + Enabled: false Naming/MemoizedInstanceVariableName: EnforcedStyleForLeadingUnderscores: optional