File tree 3 files changed +29
-4
lines changed
3 files changed +29
-4
lines changed Original file line number Diff line number Diff line change 3
3
module ViewComponent
4
4
module ComponentLocalConfig
5
5
class Configuration
6
- def initialize
7
- @config = ActiveSupport ::OrderedOptions [
6
+ def self . defaults
7
+ ActiveSupport ::Configurable :: Configuration [
8
8
strip_trailing_whitespace : false
9
9
]
10
10
end
11
11
12
+ def initialize ( config = defaults )
13
+ @config = config
14
+ end
15
+
12
16
delegate_missing_to :@config
13
17
14
18
def inheritable_copy
15
- new . instance_variable_set ( :@config , @config . inheritable_copy )
19
+ self . class . new ( @config . inheritable_copy )
16
20
end
21
+
22
+ private
23
+
24
+ delegate :defaults , to : :class
17
25
end
18
26
19
27
extend ActiveSupport ::Concern
20
28
21
29
included do
30
+ # :nocov:
22
31
def configuration
23
32
@_configuration ||= self . class . configuration . inheritable_copy
24
33
end
@@ -29,6 +38,7 @@ def inherited(child)
29
38
child . instance_variable_set ( :@_configuration , nil )
30
39
super
31
40
end
41
+ # :nocov:
32
42
end
33
43
34
44
class_methods do
@@ -37,7 +47,7 @@ def configuration
37
47
superclass . configuration . inheritable_copy
38
48
else
39
49
# create a new "anonymous" class that will host the compiled reader methods
40
- Class . new ( ActiveSupport :: Configurable ::Configuration ) . new
50
+ ViewComponent :: ComponentLocalConfig ::Configuration . new
41
51
end
42
52
end
43
53
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ class ConfigurableComponent < ViewComponent ::Base
4
+ configure do |config |
5
+ config . strip_trailing_whitespace = true
6
+ end
7
+ end
Original file line number Diff line number Diff line change @@ -146,4 +146,12 @@ def test_no_method_error_does_not_reference_missing_helper
146
146
MESSAGE
147
147
assert !exception_message_regex . match? ( exception . message )
148
148
end
149
+
150
+ def test_component_local_config_is_inheritable
151
+ assert_equal false , ViewComponent ::Base . configuration . strip_trailing_whitespace
152
+ # This component doesn't call configure, so it should inherit the defaults.
153
+ # assert_equal false, AnotherComponent.configuration.strip_trailing_whitespace
154
+ # This component overrides the defaults.
155
+ assert_equal true , ConfigurableComponent . configuration . strip_trailing_whitespace
156
+ end
149
157
end
You can’t perform that action at this time.
0 commit comments