Skip to content

Commit

Permalink
Derive strict_helpers_enabled from component-local config
Browse files Browse the repository at this point in the history
  • Loading branch information
boardfish committed Oct 11, 2024
1 parent 5807770 commit 70bed0a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def controller
def helpers
raise HelpersCalledBeforeRenderError if view_context.nil?

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

if child.superclass == ViewComponent::Base
child.define_singleton_method(:component_config) do
@@component_config ||= Rails.application.config.view_component.component_defaults.inheritable_copy
end
else
child.define_singleton_method(:component_config) do
@@component_config ||= superclass.component_config.inheritable_copy
end
end

# Give the child its own personal #render_template_for to protect against the case when
# eager loading is disabled and the parent component is rendered before the child. In
# such a scenario, the parent will override ViewComponent::Base#render_template_for,
Expand Down
5 changes: 4 additions & 1 deletion lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def defaults
test_controller: "ApplicationController",
default_preview_layout: nil,
capture_compatibility_patch_enabled: false,
helpers_enabled: true
helpers_enabled: true,
component_defaults: ActiveSupport::InheritableOptions.new({
strict_helpers_enabled: false
})
})
end

Expand Down
15 changes: 14 additions & 1 deletion test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1259,13 +1259,26 @@ def test_with_format
render_inline(MultipleFormatsComponent.new)

assert_equal(rendered_json["hello"], "world")
end
end

def test_strict_helpers
# Since helpers_enabled is true by default, we assume that the converse case
# is covered. If that default changes, tests should generally be updated.
def test_helpers_proxy_raises_if_helpers_disabled
with_helpers_enabled_config(false) do
assert_raises ViewComponent::StrictHelperError do
render_inline(HelpersProxyComponent.new)
end
end
end

def test_helpers_proxy_raises_if_strict_helpers_enabled
with_helpers_enabled_config(true) do
with_strict_helpers_config(true) do
assert_raises ViewComponent::StrictHelperError do
render_inline(HelpersProxyComponent.new)
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def with_compiler_development_mode(mode)
end

def with_strict_helpers_config(enabled, &block)
with_config_option(:strict_helpers_enabled, enabled, &block)
with_config_option(:strict_helpers_enabled, enabled, config_entrypoint: Rails.application.config.view_component.component_defaults, &block)
end

def with_helpers_enabled_config(enabled, &block)
Expand Down

0 comments on commit 70bed0a

Please sign in to comment.