diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 9a2ec8bb5..65d467522 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -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 @@ -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. @@ -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, @@ -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 diff --git a/test/sandbox/test/base_test.rb b/test/sandbox/test/base_test.rb index 8eea73cd7..7bba16a23 100644 --- a/test/sandbox/test/base_test.rb +++ b/test/sandbox/test/base_test.rb @@ -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