Skip to content

Commit 7caa69d

Browse files
committed
Refine and test component-local config
1 parent 01d3984 commit 7caa69d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/view_component/component_local_config.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
module ViewComponent
44
module ComponentLocalConfig
55
class Configuration
6-
def initialize
7-
@config = ActiveSupport::OrderedOptions[
6+
def self.defaults
7+
ActiveSupport::Configurable::Configuration[
88
strip_trailing_whitespace: false
99
]
1010
end
1111

12+
def initialize(config = defaults)
13+
@config = config
14+
end
15+
1216
delegate_missing_to :@config
1317

1418
def inheritable_copy
15-
new.instance_variable_set(:@config, @config.inheritable_copy)
19+
self.class.new(@config.inheritable_copy)
1620
end
21+
22+
private
23+
24+
delegate :defaults, to: :class
1725
end
1826

1927
extend ActiveSupport::Concern
@@ -37,7 +45,7 @@ def configuration
3745
superclass.configuration.inheritable_copy
3846
else
3947
# create a new "anonymous" class that will host the compiled reader methods
40-
Class.new(ActiveSupport::Configurable::Configuration).new
48+
ViewComponent::ComponentLocalConfig::Configuration.new
4149
end
4250
end
4351

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

test/sandbox/test/base_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,9 @@ def test_no_method_error_does_not_reference_missing_helper
146146
MESSAGE
147147
assert !exception_message_regex.match?(exception.message)
148148
end
149+
150+
def test_component_local_config_is_inheritable
151+
assert_equal false, ViewComponent::Base.configuration.strip_trailing_whitespace
152+
assert_equal true, ConfigurableComponent.configuration.strip_trailing_whitespace
153+
end
149154
end

0 commit comments

Comments
 (0)