Skip to content

Commit afa75ec

Browse files
authored
Merge branch 'main' into component-local-config
2 parents cc16f0f + 7538b59 commit afa75ec

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
@@ -14,6 +14,10 @@ nav_order: 5
1414

1515
*Simon Fish*
1616

17+
* Add unused mechanism for inheriting config from parent modules to enable future engine-local configuration.
18+
19+
*Simon Fish*
20+
1721
* Improve handling of malformed component edge case when mocking components in tests.
1822

1923
*Martin Meyerhoff*, *Joel Hawksley*

lib/view_component/base.rb

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class << self
2626
#
2727
# @return [ActiveSupport::OrderedOptions]
2828
def config
29+
module_parents.each do |m|
30+
config = m.try(:config).try(:view_component)
31+
return config if config
32+
end
2933
ViewComponent::Config.current
3034
end
3135
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
@@ -154,4 +155,54 @@ def test_component_local_config_is_inheritable
154155
# This component overrides the defaults.
155156
assert_equal true, ConfigurableComponent.configuration.strip_trailing_whitespace
156157
end
158+
159+
module TestModuleWithoutConfig
160+
class SomeComponent < ViewComponent::Base
161+
end
162+
end
163+
164+
# Config defined on top-level module as opposed to engine.
165+
module TestModuleWithConfig
166+
include ViewComponent::Configurable
167+
168+
configure do |config|
169+
config.view_component.test_controller = "AnotherController"
170+
end
171+
172+
class SomeComponent < ViewComponent::Base
173+
end
174+
end
175+
176+
module TestAlreadyConfigurableModule
177+
include ActiveSupport::Configurable
178+
include ViewComponent::Configurable
179+
180+
configure do |config|
181+
config.view_component.test_controller = "AnotherController"
182+
end
183+
184+
class SomeComponent < ViewComponent::Base
185+
end
186+
end
187+
188+
module TestAlreadyConfiguredModule
189+
include ActiveSupport::Configurable
190+
191+
configure do |config|
192+
config.view_component = ActiveSupport::InheritableOptions[test_controller: "AnotherController"]
193+
end
194+
195+
include ViewComponent::Configurable
196+
197+
class SomeComponent < ViewComponent::Base
198+
end
199+
end
200+
201+
def test_uses_module_configuration
202+
# We override this ourselves in test/sandbox/config/environments/test.rb.
203+
assert_equal "IntegrationExamplesController", TestModuleWithoutConfig::SomeComponent.test_controller
204+
assert_equal "AnotherController", TestModuleWithConfig::SomeComponent.test_controller
205+
assert_equal "AnotherController", TestAlreadyConfigurableModule::SomeComponent.test_controller
206+
assert_equal "AnotherController", TestAlreadyConfiguredModule::SomeComponent.test_controller
207+
end
157208
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)