Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
4 changes: 4 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/view_component/configurable.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "test_helper"
require "view_component/configurable"

class ViewComponent::Base::UnitTest < Minitest::Test
def test_identifier
Expand Down Expand Up @@ -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
Comment on lines +151 to +191
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to move this setup out into other files if you'd rather.


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
4 changes: 2 additions & 2 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading