diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 63a2ede33..bbe5c4196 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 5 ## main +* Add unused mechanism for inheriting config from parent modules to enable future engine-local configuration. + + *Simon Fish* + * Improve handling of malformed component edge case when mocking components in tests. *Martin Meyerhoff*, *Joel Hawksley* diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 2cb42a545..8d80faa1d 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -25,6 +25,10 @@ class << self # # @return [ActiveSupport::OrderedOptions] def config + module_parents.each do |m| + config = m.try(:config).try(:view_component) + return config if config + end ViewComponent::Config.current end end diff --git a/lib/view_component/configurable.rb b/lib/view_component/configurable.rb new file mode 100644 index 000000000..5fe68d21d --- /dev/null +++ b/lib/view_component/configurable.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module ViewComponent + module Configurable + extend ActiveSupport::Concern + + included do + next if respond_to?(:config) && config.respond_to?(:view_component) && config.respond_to_missing?(:test_controller) + + include ActiveSupport::Configurable + + configure do |config| + config.view_component ||= ActiveSupport::InheritableOptions.new + end + end + end +end diff --git a/test/sandbox/test/base_test.rb b/test/sandbox/test/base_test.rb index 46387e981..0e7b26d84 100644 --- a/test/sandbox/test/base_test.rb +++ b/test/sandbox/test/base_test.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "test_helper" +require "view_component/configurable" class ViewComponent::Base::UnitTest < Minitest::Test def test_identifier @@ -146,4 +147,54 @@ def test_no_method_error_does_not_reference_missing_helper MESSAGE assert !exception_message_regex.match?(exception.message) end + + module TestModuleWithoutConfig + class SomeComponent < ViewComponent::Base + end + end + + # Config defined on top-level module as opposed to engine. + module TestModuleWithConfig + include ViewComponent::Configurable + + configure do |config| + config.view_component.test_controller = "AnotherController" + end + + class SomeComponent < ViewComponent::Base + end + end + + module TestAlreadyConfigurableModule + include ActiveSupport::Configurable + include ViewComponent::Configurable + + configure do |config| + config.view_component.test_controller = "AnotherController" + end + + class SomeComponent < ViewComponent::Base + end + end + + module TestAlreadyConfiguredModule + include ActiveSupport::Configurable + + configure do |config| + config.view_component = ActiveSupport::InheritableOptions[test_controller: "AnotherController"] + end + + include ViewComponent::Configurable + + class SomeComponent < ViewComponent::Base + end + end + + def test_uses_module_configuration + # We override this ourselves in test/sandbox/config/environments/test.rb. + assert_equal "IntegrationExamplesController", TestModuleWithoutConfig::SomeComponent.test_controller + assert_equal "AnotherController", TestModuleWithConfig::SomeComponent.test_controller + assert_equal "AnotherController", TestAlreadyConfigurableModule::SomeComponent.test_controller + assert_equal "AnotherController", TestAlreadyConfiguredModule::SomeComponent.test_controller + end end diff --git a/test/sandbox/test/rendering_test.rb b/test/sandbox/test/rendering_test.rb index 356b0d7db..01b7b0580 100644 --- a/test/sandbox/test/rendering_test.rb +++ b/test/sandbox/test/rendering_test.rb @@ -16,8 +16,8 @@ def test_render_inline_allocations MyComponent.ensure_compiled allocations = (Rails.version.to_f >= 8.0) ? - {"3.5.0" => 115, "3.4.2" => 117, "3.3.7" => 129} : - {"3.3.7" => 120, "3.3.0" => 132, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 127} + {"3.5.0" => 123, "3.4.2" => 125, "3.3.7" => 137} : + {"3.3.7" => 128, "3.3.0" => 140, "3.2.7" => 126, "3.1.6" => 126, "3.0.7" => 135} assert_allocations(**allocations) do render_inline(MyComponent.new)