Skip to content

Commit 4e5301b

Browse files
committed
Use ActiveSupport::Configurable to cascade config
ViewComponent::Base itself will always use the defaults. Any classes inheriting from ViewComponent::Base will cascade config down using InheritableOptions, only specifying any overrides. Generate options being on their own "layer of config is currently unresolved - it might be that config needs to be a new object inheriting from InheritableOptions that has method accessors for everything in that namespace. It seems like this solution is now applicable to everything, not just strict_helper_enabled?
1 parent c4a0d94 commit 4e5301b

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/view_component/base.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,16 @@ def inherited(child)
520520
# `compile` defines
521521
compile
522522

523-
# Set strict_helpers_enabled from global config
523+
child.include ActiveSupport::Configurable
524+
524525
if child.superclass == ViewComponent::Base
525-
child.__vc_strict_helpers_enabled = Rails.application.config.view_component.strict_helpers_enabled
526+
child.define_singleton_method(:config) do
527+
@@config ||= Rails.application.config.view_component.inheritable_copy
528+
end
529+
else
530+
child.define_singleton_method(:config) do
531+
@@config ||= superclass.config.inheritable_copy
532+
end
526533
end
527534

528535
# Give the child its own personal #render_template_for to protect against the case when
@@ -635,15 +642,6 @@ def strip_trailing_whitespace?
635642
__vc_strip_trailing_whitespace
636643
end
637644

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-
647645
# Ensure the component initializer accepts the
648646
# collection parameter. By default, we don't
649647
# validate that the default parameter name

lib/view_component/config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ 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+
strict_helpers_enabled?: false
3031
})
3132
end
3233

test/sandbox/test/base_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ def test_no_method_error_does_not_reference_missing_helper
157157

158158
def test_strict_helpers_enabled
159159
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"
165160
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
161+
refute ViewComponent::Base.config.strict_helpers_enabled?, ".strict_helpers_enabled? should be false by default"
162+
# refute ViewComponent::Base.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should be false by default"
163+
Rails.application.config.view_component[:strict_helpers_enabled?] = true
164+
refute ViewComponent::Base.config.strict_helpers_enabled?, ".strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
165+
# refute ViewComponent::Base.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should not be changed by global config for ViewComponent::Base"
166+
assert top_level_component_class.config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from global config"
167+
assert top_level_component_class.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from global config"
169168
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"
169+
top_level_component_class.config[:strict_helpers_enabled?] = false
170+
refute inherited_component_class.config.strict_helpers_enabled?, ".strict_helpers_enabled? should inherit from its parent"
171+
refute inherited_component_class.new.config.strict_helpers_enabled?, "#strict_helpers_enabled? should inherit from its parent"
172172
end
173173
end
174174
end

0 commit comments

Comments
 (0)