Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ Metrics/ClassLength:
- "test/**/*"

Metrics/MethodLength:
Max: 12
Exclude:
- "demo/db/migrate/*"
- "demo/test/**/*"
- "test/**/*"
Enabled: false

Naming/MemoizedInstanceVariableName:
EnforcedStyleForLeadingUnderscores: optional
Expand Down
2 changes: 1 addition & 1 deletion .yarnrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# yarn lockfile v1


lastUpdateCheck 1762552807228
lastUpdateCheck 1763256402231
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 4 additions & 0 deletions demo/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 3 additions & 6 deletions lib/bootstrap_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
19 changes: 17 additions & 2 deletions lib/bootstrap_form/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions lib/bootstrap_form/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 37 additions & 5 deletions test/bootstrap_configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 0 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down