Skip to content

Commit e8bc4ef

Browse files
committed
Make GlobalConfig a proxy for Rails app config or ViewComponent base config as necessary
1 parent 4e5301b commit e8bc4ef

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lib/view_component/base.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def controller
232232
# @return [ActionView::Base]
233233
def helpers
234234
raise HelpersCalledBeforeRenderError if view_context.nil?
235-
raise StrictHelperError unless ViewComponent::Base.config.helpers_enabled
235+
raise StrictHelperError unless GlobalConfig.helpers_enabled
236236
# Attempt to re-use the original view_context passed to the first
237237
# component rendered in the rendering pipeline. This prevents the
238238
# instantiation of a new view_context via `controller.view_context` which
@@ -249,7 +249,7 @@ def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToM
249249
super
250250
rescue => e # rubocop:disable Style/RescueStandardError
251251
e.set_backtrace e.backtrace.tap(&:shift)
252-
if !ViewComponent::Base.config.helpers_enabled
252+
if !GlobalConfig.helpers_enabled
253253
raise e, <<~MESSAGE.chomp if view_context && e.is_a?(NameError) && (__vc_original_view_context.respond_to?(method_name) || controller.view_context.respond_to?(method_name))
254254
#{e.message}
255255
@@ -520,15 +520,13 @@ def inherited(child)
520520
# `compile` defines
521521
compile
522522

523-
child.include ActiveSupport::Configurable
524-
525523
if child.superclass == ViewComponent::Base
526-
child.define_singleton_method(:config) do
527-
@@config ||= Rails.application.config.view_component.inheritable_copy
524+
child.define_singleton_method(:component_config) do
525+
@@component_config ||= Rails.application.config.view_component.inheritable_copy
528526
end
529527
else
530-
child.define_singleton_method(:config) do
531-
@@config ||= superclass.config.inheritable_copy
528+
child.define_singleton_method(:component_config) do
529+
@@component_config ||= superclass.component_config.inheritable_copy
532530
end
533531
end
534532

test/sandbox/test/base_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ def test_strict_helpers_enabled
163163
Rails.application.config.view_component[:strict_helpers_enabled?] = true
164164
refute ViewComponent::Base.config.strict_helpers_enabled?, ".strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
165165
# refute ViewComponent::Base.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
166-
assert top_level_component_class.config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from global config"
167-
assert top_level_component_class.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from global config"
166+
assert top_level_component_class.component_config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from global config"
167+
assert top_level_component_class.new.component_config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from global config"
168168
inherited_component_class = Class.new(top_level_component_class)
169-
top_level_component_class.config[:strict_helpers_enabled?] = false
170-
refute inherited_component_class.config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from its parent"
171-
refute inherited_component_class.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from its parent"
169+
top_level_component_class.component_config[:strict_helpers_enabled?] = false
170+
refute inherited_component_class.component_config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from its parent"
171+
refute inherited_component_class.new.component_config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from its parent"
172172
end
173173
end
174174
end

0 commit comments

Comments
 (0)