Skip to content

Commit dbfc516

Browse files
committed
Derive strict_helpers_enabled from component-local config
1 parent b7a6ad0 commit dbfc516

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/view_component/base.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def controller
233233
def helpers
234234
raise HelpersCalledBeforeRenderError if view_context.nil?
235235

236-
raise StrictHelperError if ViewComponent::Base.strict_helpers_enabled
236+
raise StrictHelperError if !GlobalConfig.helpers_enabled || component_config.strict_helpers_enabled
237237
# Attempt to re-use the original view_context passed to the first
238238
# component rendered in the rendering pipeline. This prevents the
239239
# instantiation of a new view_context via `controller.view_context` which
@@ -520,6 +520,16 @@ def inherited(child)
520520
# `compile` defines
521521
compile
522522

523+
if child.superclass == ViewComponent::Base
524+
child.define_singleton_method(:component_config) do
525+
@@component_config ||= Rails.application.config.view_component.component_defaults.inheritable_copy
526+
end
527+
else
528+
child.define_singleton_method(:component_config) do
529+
@@component_config ||= superclass.component_config.inheritable_copy
530+
end
531+
end
532+
523533
# Give the child its own personal #render_template_for to protect against the case when
524534
# eager loading is disabled and the parent component is rendered before the child. In
525535
# such a scenario, the parent will override ViewComponent::Base#render_template_for,

lib/view_component/config.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def defaults
2626
test_controller: "ApplicationController",
2727
default_preview_layout: nil,
2828
capture_compatibility_patch_enabled: false,
29-
helpers_enabled: true
29+
helpers_enabled: true,
30+
component_defaults: ActiveSupport::InheritableOptions.new({
31+
strict_helpers_enabled: false
32+
})
3033
})
3134
end
3235

test/sandbox/test/rendering_test.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -1259,13 +1259,26 @@ def test_with_format
12591259
render_inline(MultipleFormatsComponent.new)
12601260

12611261
assert_equal(rendered_json["hello"], "world")
1262+
end
12621263
end
12631264

1264-
def test_strict_helpers
1265+
# Since helpers_enabled is true by default, we assume that the converse case
1266+
# is covered. If that default changes, tests should generally be updated.
1267+
def test_helpers_proxy_raises_if_helpers_disabled
12651268
with_helpers_enabled_config(false) do
12661269
assert_raises ViewComponent::StrictHelperError do
12671270
render_inline(HelpersProxyComponent.new)
12681271
end
12691272
end
12701273
end
1274+
1275+
def test_helpers_proxy_raises_if_strict_helpers_enabled
1276+
with_helpers_enabled_config(true) do
1277+
with_strict_helpers_config(true) do
1278+
assert_raises ViewComponent::StrictHelperError do
1279+
render_inline(HelpersProxyComponent.new)
1280+
end
1281+
end
1282+
end
1283+
end
12711284
end

test/test_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def with_compiler_development_mode(mode)
179179
end
180180

181181
def with_strict_helpers_config(enabled, &block)
182-
with_config_option(:strict_helpers_enabled, enabled, &block)
182+
with_config_option(:strict_helpers_enabled, enabled, config_entrypoint: Rails.application.config.view_component.component_defaults, &block)
183183
end
184184

185185
def with_helpers_enabled_config(enabled, &block)

0 commit comments

Comments
 (0)