Skip to content

Commit

Permalink
WIP: Extract strict_helpers_enabled? to component-local config
Browse files Browse the repository at this point in the history
  • Loading branch information
boardfish committed Sep 8, 2024
1 parent 313a922 commit 35017a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def config
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2

# Default for class
class_attribute :__vc_strict_helpers_enabled, instance_accessor: false, instance_predicate: false, default: false
# Changeable for instance
attr_writer :__vc_strict_helpers_enabled

def __vc_strict_helpers_enabled
@__vc_strict_helpers_enabled.nil? ? self.class.__vc_strict_helpers_enabled : @__vc_strict_helpers_enabled
end
alias_method :strict_helpers_enabled?, :__vc_strict_helpers_enabled

attr_accessor :__vc_original_view_context

# Components render in their own view context. Helpers and other functionality
Expand Down Expand Up @@ -466,6 +476,7 @@ def sidecar_files(extensions)
# view files in a directory named like the component
directory = File.dirname(source_location)
filename = File.basename(source_location, ".rb")
return [] if name.blank?
component_name = name.demodulize.underscore

# Add support for nested components defined in the same file.
Expand Down Expand Up @@ -518,6 +529,11 @@ def inherited(child)
# `compile` defines
compile

# Set strict_helpers_enabled from global config
if child.superclass == ViewComponent::Base
child.__vc_strict_helpers_enabled = Rails.application.config.view_component.strict_helpers_enabled
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 Expand Up @@ -639,6 +655,15 @@ def strip_trailing_whitespace?
__vc_strip_trailing_whitespace
end

# TODO
def strict_helpers_enabled=(value = true)
self.__vc_strict_helpers_enabled = value
end

def strict_helpers_enabled?
__vc_strict_helpers_enabled
end

# Ensure the component initializer accepts the
# collection parameter. By default, we don't
# validate that the default parameter name
Expand Down
17 changes: 17 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,21 @@ def test_no_method_error_does_not_reference_missing_helper
MESSAGE
assert !exception_message_regex.match?(exception.message)
end

def test_strict_helpers_enabled
with_config_option(:strict_helpers_enabled, false) do
refute ViewComponent::Base.strict_helpers_enabled?, ".strict_helpers_enabled? should be false by default"
refute ViewComponent::Base.new.strict_helpers_enabled?, "#strict_helpers_enabled? should be false by default"
Rails.application.config.view_component.strict_helpers_enabled = true
refute ViewComponent::Base.strict_helpers_enabled?, ".strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
refute ViewComponent::Base.new.strict_helpers_enabled?, "#strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
top_level_component_class = Class.new(ViewComponent::Base)
assert top_level_component_class.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from global config"
assert top_level_component_class.new.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from global config"
top_level_component_class.strict_helpers_enabled = false
inherited_component_class = Class.new(top_level_component_class)
refute inherited_component_class.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from its parent"
refute inherited_component_class.new.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from its parent"
end
end
end

0 comments on commit 35017a9

Please sign in to comment.