Skip to content

Commit 94fa1ed

Browse files
boardfishreeganviljoen
authored andcommitted
Derive strict_helpers_enabled from component-local config
1 parent 5381fc6 commit 94fa1ed

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
@@ -228,7 +228,7 @@ def controller
228228
def helpers
229229
raise HelpersCalledBeforeRenderError if view_context.nil?
230230

231-
raise StrictHelperError if ViewComponent::Base.strict_helpers_enabled
231+
raise StrictHelperError if !GlobalConfig.helpers_enabled || component_config.strict_helpers_enabled
232232
# Attempt to re-use the original view_context passed to the first
233233
# component rendered in the rendering pipeline. This prevents the
234234
# 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
@@ -1277,9 +1277,12 @@ def test_with_format
12771277
render_inline(MultipleFormatsComponent.new)
12781278

12791279
assert_equal(rendered_json["hello"], "world")
1280+
end
12801281
end
12811282

1282-
def test_strict_helpers
1283+
# Since helpers_enabled is true by default, we assume that the converse case
1284+
# is covered. If that default changes, tests should generally be updated.
1285+
def test_helpers_proxy_raises_if_helpers_disabled
12831286
with_helpers_enabled_config(false) do
12841287
assert_raises ViewComponent::StrictHelperError do
12851288
render_inline(HelpersProxyComponent.new)
@@ -1313,4 +1316,14 @@ def test_render_anonymous_component_without_template
13131316
render_inline(mock_component.new)
13141317
end
13151318
end
1319+
1320+
def test_helpers_proxy_raises_if_strict_helpers_enabled
1321+
with_helpers_enabled_config(true) do
1322+
with_strict_helpers_config(true) do
1323+
assert_raises ViewComponent::StrictHelperError do
1324+
render_inline(HelpersProxyComponent.new)
1325+
end
1326+
end
1327+
end
1328+
end
13161329
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)