Skip to content

Commit c4a0d94

Browse files
committed
WIP: Extract strict_helpers_enabled? to component-local config
1 parent 5a5727a commit c4a0d94

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/view_component/base.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ def sidecar_files(extensions)
474474
# view files in a directory named like the component
475475
directory = File.dirname(source_location)
476476
filename = File.basename(source_location, ".rb")
477+
return [] if name.blank?
477478
component_name = name.demodulize.underscore
478479

479480
# Add support for nested components defined in the same file.
@@ -519,6 +520,11 @@ def inherited(child)
519520
# `compile` defines
520521
compile
521522

523+
# Set strict_helpers_enabled from global config
524+
if child.superclass == ViewComponent::Base
525+
child.__vc_strict_helpers_enabled = Rails.application.config.view_component.strict_helpers_enabled
526+
end
527+
522528
# Give the child its own personal #render_template_for to protect against the case when
523529
# eager loading is disabled and the parent component is rendered before the child. In
524530
# such a scenario, the parent will override ViewComponent::Base#render_template_for,
@@ -629,6 +635,15 @@ def strip_trailing_whitespace?
629635
__vc_strip_trailing_whitespace
630636
end
631637

638+
# TODO
639+
def strict_helpers_enabled=(value = true)
640+
self.__vc_strict_helpers_enabled = value
641+
end
642+
643+
def strict_helpers_enabled?
644+
__vc_strict_helpers_enabled
645+
end
646+
632647
# Ensure the component initializer accepts the
633648
# collection parameter. By default, we don't
634649
# validate that the default parameter name

test/sandbox/test/base_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,21 @@ def test_no_method_error_does_not_reference_missing_helper
154154
MESSAGE
155155
assert !exception_message_regex.match?(exception.message)
156156
end
157+
158+
def test_strict_helpers_enabled
159+
with_config_option(:strict_helpers_enabled, false) do
160+
refute ViewComponent::Base.strict_helpers_enabled?, ".strict_helpers_enabled? should be false by default"
161+
refute ViewComponent::Base.new.strict_helpers_enabled?, "#strict_helpers_enabled? should be false by default"
162+
Rails.application.config.view_component.strict_helpers_enabled = true
163+
refute ViewComponent::Base.strict_helpers_enabled?, ".strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
164+
refute ViewComponent::Base.new.strict_helpers_enabled?, "#strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
165+
top_level_component_class = Class.new(ViewComponent::Base)
166+
assert top_level_component_class.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from global config"
167+
assert top_level_component_class.new.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from global config"
168+
top_level_component_class.strict_helpers_enabled = false
169+
inherited_component_class = Class.new(top_level_component_class)
170+
refute inherited_component_class.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from its parent"
171+
refute inherited_component_class.new.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from its parent"
172+
end
173+
end
157174
end

0 commit comments

Comments
 (0)