Skip to content

Commit 465ca45

Browse files
committed
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.
1 parent bb5eb7b commit 465ca45

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

lib/view_component/configurable.rb

Lines changed: 17 additions & 0 deletions
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

Lines changed: 28 additions & 3 deletions
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
@@ -149,27 +150,51 @@ def test_no_method_error_does_not_reference_missing_helper
149150

150151
module TestModuleWithoutConfig
151152
class SomeComponent < ViewComponent::Base
152-
153153
end
154154
end
155155

156156
# Config defined on top-level module as opposed to engine.
157157
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
158169
include ActiveSupport::Configurable
170+
include ViewComponent::Configurable
159171

160172
configure do |config|
161-
# To get this green.
162-
config.view_component = ActiveSupport::InheritableOptions.new
163173
config.view_component.test_controller = "AnotherController"
164174
end
165175

166176
class SomeComponent < ViewComponent::Base
167177
end
168178
end
169179

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+
170193
def test_uses_module_configuration
171194
# We override this ourselves in test/sandbox/config/environments/test.rb.
172195
assert_equal "IntegrationExamplesController", TestModuleWithoutConfig::SomeComponent.test_controller
173196
assert_equal "AnotherController", TestModuleWithConfig::SomeComponent.test_controller
197+
assert_equal "AnotherController", TestAlreadyConfigurableModule::SomeComponent.test_controller
198+
assert_equal "AnotherController", TestAlreadyConfiguredModule::SomeComponent.test_controller
174199
end
175200
end

0 commit comments

Comments
 (0)