diff --git a/.rubocop.yml b/.rubocop.yml index c1a991fc..e16064e5 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 diff --git a/.yarnrc b/.yarnrc index 319bc750..adf41e8f 100644 --- a/.yarnrc +++ b/.yarnrc @@ -2,4 +2,4 @@ # yarn lockfile v1 -lastUpdateCheck 1762552807228 +lastUpdateCheck 1763256402231 diff --git a/README.md b/README.md index f652ee93..b233c2d3 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: diff --git a/demo/config/boot.rb b/demo/config/boot.rb index 6d349b91..4145fc8d 100644 --- a/demo/config/boot.rb +++ b/demo/config/boot.rb @@ -3,3 +3,7 @@ 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. +require "bootsnap/setup" if Gem.loaded_specs.has_key?("bootsnap") # Speed up boot time by caching expensive operations. diff --git a/lib/bootstrap_form.rb b/lib/bootstrap_form.rb index b7315ec9..5109147c 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 @@ -25,12 +26,8 @@ def eager_load! BootstrapForm::Inputs.eager_load! end - def config - @config ||= BootstrapForm::Configuration.new - end - - def configure - yield config + def deprecator + @deprecator ||= ActiveSupport::Deprecation.new("a future release", "BootstrapForm") end end diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index 43c4bb3d..fdd2e0d2 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -7,16 +7,31 @@ def default_form_attributes=(attributes) when nil @default_form_attributes = {} when Hash + BootstrapForm.deprecator.warn(<<~MESSAGE.squish) + BootstrapForm::Configuration#default_form_attributes= will be removed in a future release. + Please use BootstrapForm.config.default_form_attributes= instead. + MESSAGE @default_form_attributes = attributes + BootstrapForm.config.default_form_attributes = attributes else raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}" end 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 BootstrapForm.config.default_form_attributes instead. + MESSAGE + BootstrapForm.config.default_form_attributes + end + end + + mattr_accessor :config, default: ActiveSupport::OrderedOptions.new - {} + class << self + def configure + yield(config) if block_given? end end end diff --git a/lib/bootstrap_form/engine.rb b/lib/bootstrap_form/engine.rb index b27bcda6..8f7c7151 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 = 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 end end diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 9987f039..4bf1593b 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -2,25 +2,40 @@ 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. + 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 - 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 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) + 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 @@ -31,4 +46,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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 1f117396..dcfed122 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"