Skip to content

Commit 7538b59

Browse files
authored
Delegate config to parent modules (no-op) (#2231)
* Delegate config to parent modules Adds a mechanism for inheriting config from a parent module. There's definitely more that needs to be done to iron this out, including: - if the module does not already respond to `config`, provide a concern that sets it up using `ActiveSupport::Configurable` - if the module _does_ already respond to `config` and doesn't have `view_component` set on it, set it up. - if the module's config does have `view_component` set up, leave it alone? I also need to consider whether the current way of having things refer back to the Rails app config is appropriate. Since `Base` is generally used when referring to "global" config options, this probably doesn't do anything right now, but settings like `test_controller` and `generate` should opt into it. * Provide ViewComponent::Configurable module This can be included on a module to provide module-local configuration for ViewComponent. It avoids overwriting existing configuration that may already exist. * Adjust allocations * Update changelog
1 parent 6a35673 commit 7538b59

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Add unused mechanism for inheriting config from parent modules to enable future engine-local configuration.
14+
15+
*Simon Fish*
16+
1317
* Improve handling of malformed component edge case when mocking components in tests.
1418

1519
*Martin Meyerhoff*, *Joel Hawksley*

lib/view_component/base.rb

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class << self
2525
#
2626
# @return [ActiveSupport::OrderedOptions]
2727
def config
28+
module_parents.each do |m|
29+
config = m.try(:config).try(:view_component)
30+
return config if config
31+
end
2832
ViewComponent::Config.current
2933
end
3034
end

lib/view_component/configurable.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module ViewComponent
4+
module Configurable
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
next if respond_to?(:config) && config.respond_to?(:view_component) && config.respond_to_missing?(:test_controller)
9+
10+
include ActiveSupport::Configurable
11+
12+
configure do |config|
13+
config.view_component ||= ActiveSupport::InheritableOptions.new
14+
end
15+
end
16+
end
17+
end

test/sandbox/test/base_test.rb

+51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "view_component/configurable"
45

56
class ViewComponent::Base::UnitTest < Minitest::Test
67
def test_identifier
@@ -146,4 +147,54 @@ def test_no_method_error_does_not_reference_missing_helper
146147
MESSAGE
147148
assert !exception_message_regex.match?(exception.message)
148149
end
150+
151+
module TestModuleWithoutConfig
152+
class SomeComponent < ViewComponent::Base
153+
end
154+
end
155+
156+
# Config defined on top-level module as opposed to engine.
157+
module TestModuleWithConfig
158+
include ViewComponent::Configurable
159+
160+
configure do |config|
161+
config.view_component.test_controller = "AnotherController"
162+
end
163+
164+
class SomeComponent < ViewComponent::Base
165+
end
166+
end
167+
168+
module TestAlreadyConfigurableModule
169+
include ActiveSupport::Configurable
170+
include ViewComponent::Configurable
171+
172+
configure do |config|
173+
config.view_component.test_controller = "AnotherController"
174+
end
175+
176+
class SomeComponent < ViewComponent::Base
177+
end
178+
end
179+
180+
module TestAlreadyConfiguredModule
181+
include ActiveSupport::Configurable
182+
183+
configure do |config|
184+
config.view_component = ActiveSupport::InheritableOptions[test_controller: "AnotherController"]
185+
end
186+
187+
include ViewComponent::Configurable
188+
189+
class SomeComponent < ViewComponent::Base
190+
end
191+
end
192+
193+
def test_uses_module_configuration
194+
# We override this ourselves in test/sandbox/config/environments/test.rb.
195+
assert_equal "IntegrationExamplesController", TestModuleWithoutConfig::SomeComponent.test_controller
196+
assert_equal "AnotherController", TestModuleWithConfig::SomeComponent.test_controller
197+
assert_equal "AnotherController", TestAlreadyConfigurableModule::SomeComponent.test_controller
198+
assert_equal "AnotherController", TestAlreadyConfiguredModule::SomeComponent.test_controller
199+
end
149200
end

test/sandbox/test/rendering_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_render_inline_allocations
1616
MyComponent.ensure_compiled
1717

1818
allocations = (Rails.version.to_f >= 8.0) ?
19-
{"3.5.0" => 115, "3.4.2" => 117, "3.3.7" => 129} :
20-
{"3.3.7" => 120, "3.3.0" => 132, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 127}
19+
{"3.5.0" => 123, "3.4.2" => 125, "3.3.7" => 137} :
20+
{"3.3.7" => 128, "3.3.0" => 140, "3.2.7" => 126, "3.1.6" => 126, "3.0.7" => 135}
2121

2222
assert_allocations(**allocations) do
2323
render_inline(MyComponent.new)

0 commit comments

Comments
 (0)