Skip to content

Commit e941e60

Browse files
authored
Merge pull request #776 from lcreid/760-use-rails-configuration
Use Rails Configuration
2 parents 273db0c + 3a69f7e commit e941e60

File tree

9 files changed

+76
-25
lines changed

9 files changed

+76
-25
lines changed

.rubocop.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ Metrics/ClassLength:
5454
- "test/**/*"
5555

5656
Metrics/MethodLength:
57-
Max: 12
58-
Exclude:
59-
- "demo/db/migrate/*"
60-
- "demo/test/**/*"
61-
- "test/**/*"
57+
Enabled: false
6258

6359
Naming/MemoizedInstanceVariableName:
6460
EnforcedStyleForLeadingUnderscores: optional

.yarnrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# yarn lockfile v1
33

44

5-
lastUpdateCheck 1762552807228
5+
lastUpdateCheck 1763256402231

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ Generated HTML:
226226

227227
## Configuration
228228

229-
`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.
229+
`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.
230230

231231
The current configuration options are:
232232

233233
| Option | Default value | Description |
234234
|---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
235-
| `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. |
235+
| `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. |
236236

237237
Example:
238238

demo/config/boot.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33

44
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
55
$LOAD_PATH.unshift File.expand_path("../../lib", __dir__)
6+
# Only run the following line if we bootsnap is loaded, because we don't load it for the individual tests.
7+
# The individual tests are the ones in `..`.
8+
# They do load this file, for reasons.
9+
require "bootsnap/setup" if Gem.loaded_specs.has_key?("bootsnap") # Speed up boot time by caching expensive operations.

lib/bootstrap_form.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "action_view"
44
require "action_pack"
55
require "bootstrap_form/action_view_extensions/form_helper"
6+
require "bootstrap_form/configuration"
67

78
module BootstrapForm
89
extend ActiveSupport::Autoload
@@ -25,12 +26,8 @@ def eager_load!
2526
BootstrapForm::Inputs.eager_load!
2627
end
2728

28-
def config
29-
@config ||= BootstrapForm::Configuration.new
30-
end
31-
32-
def configure
33-
yield config
29+
def deprecator
30+
@deprecator ||= ActiveSupport::Deprecation.new("a future release", "BootstrapForm")
3431
end
3532
end
3633

lib/bootstrap_form/configuration.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@ def default_form_attributes=(attributes)
77
when nil
88
@default_form_attributes = {}
99
when Hash
10+
BootstrapForm.deprecator.warn(<<~MESSAGE.squish)
11+
BootstrapForm::Configuration#default_form_attributes= will be removed in a future release.
12+
Please use BootstrapForm.config.default_form_attributes= instead.
13+
MESSAGE
1014
@default_form_attributes = attributes
15+
BootstrapForm.config.default_form_attributes = attributes
1116
else
1217
raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}"
1318
end
1419
end
1520

1621
def default_form_attributes
17-
return @default_form_attributes if defined? @default_form_attributes
22+
BootstrapForm.deprecator.warn(<<~MESSAGE.squish)
23+
BootstrapForm::Configuration#default_form_attributes will be removed in a future release.
24+
Please use BootstrapForm.config.default_form_attributes instead.
25+
MESSAGE
26+
BootstrapForm.config.default_form_attributes
27+
end
28+
end
29+
30+
mattr_accessor :config, default: ActiveSupport::OrderedOptions.new
1831

19-
{}
32+
class << self
33+
def configure
34+
yield(config) if block_given?
2035
end
2136
end
2237
end

lib/bootstrap_form/engine.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ module BootstrapForm
66
class Engine < Rails::Engine
77
config.eager_load_namespaces << BootstrapForm
88
config.autoload_paths << File.expand_path("lib", __dir__)
9+
10+
config.bootstrap_form = BootstrapForm.config
11+
config.bootstrap_form.default_form_attributes ||= {}
12+
13+
initializer "bootstrap_form.configure" do |app|
14+
BootstrapForm.config = app.config.bootstrap_form
15+
end
16+
17+
initializer "bootstrap_form.deprecator" do |app|
18+
app.deprecators[:bootstrap_form] = BootstrapForm.deprecator
19+
end
920
end
1021
end

test/bootstrap_configuration_test.rb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@
22

33
require_relative "test_helper"
44

5-
class BootstrapConfigurationTest < ActionView::TestCase
5+
class BootstrapConfigurationTest < ActiveSupport::TestCase
6+
teardown do
7+
# Unfortunately, it seems we have to manually reset each of the configuration options
8+
# that we change in our test cases.
9+
Rails.application.config.bootstrap_form.default_form_attributes = {}
10+
Rails.application.config.bootstrap_form.bogon = nil
11+
end
12+
613
test "has default form attributes" do
714
config = BootstrapForm::Configuration.new
815

9-
assert_equal({}, config.default_form_attributes)
16+
assert_deprecated(BootstrapForm.deprecator) do
17+
assert_equal({}, config.default_form_attributes)
18+
end
1019
end
1120

1221
test "allows to set default_form_attributes with custom value" do
1322
config = BootstrapForm::Configuration.new
14-
config.default_form_attributes = { foo: "bar" }
23+
assert_deprecated(BootstrapForm.deprecator) do
24+
config.default_form_attributes = { foo: "bar" }
25+
end
1526

16-
assert_equal({ foo: "bar" }, config.default_form_attributes)
27+
assert_deprecated(BootstrapForm.deprecator) do
28+
assert_equal({ foo: "bar" }, config.default_form_attributes)
29+
end
1730
end
1831

1932
test "allows to set default_form_attributes with nil" do
2033
config = BootstrapForm::Configuration.new
2134
config.default_form_attributes = nil
2235

23-
assert_equal({}, config.default_form_attributes)
36+
assert_deprecated(BootstrapForm.deprecator) do
37+
assert_equal({}, config.default_form_attributes)
38+
end
2439
end
2540

2641
test "does not allow to set default_form_attributes with unsupported value" do
@@ -31,4 +46,21 @@ class BootstrapConfigurationTest < ActionView::TestCase
3146
end
3247
assert_equal("Unsupported default_form_attributes [1, 2, 3]", exception.message)
3348
end
49+
50+
test "Use Rails configuration" do
51+
assert_nil Rails.application.config.bootstrap_form.bogon
52+
Rails.application.config.bootstrap_form.bogon = true
53+
assert Rails.application.config.bootstrap_form.bogon
54+
end
55+
56+
test "Support legacy configuration from Rails configuration" do
57+
assert_equal({}, Rails.application.config.bootstrap_form.default_form_attributes)
58+
59+
config = BootstrapForm::Configuration.new
60+
assert_deprecated(BootstrapForm.deprecator) do
61+
config.default_form_attributes = { foo: "bar" }
62+
end
63+
64+
assert_equal({ foo: "bar" }, Rails.application.config.bootstrap_form.default_form_attributes)
65+
end
3466
end

test/test_helper.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
Warning.ignore(:not_reached, mail_gem_path)
1010
Warning.ignore(/warning: assigned but unused variable - testEof/, mail_gem_path)
1111

12-
require "diffy"
13-
require "nokogiri"
14-
require "equivalent-xml"
15-
1612
require_relative "../demo/config/environment"
1713
require "rails/test_help"
1814
require "mocha/minitest"

0 commit comments

Comments
 (0)