Skip to content

Commit 56f28b9

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 35017a9 commit 56f28b9

File tree

5 files changed

+26
-35
lines changed

5 files changed

+26
-35
lines changed

lib/view_component/base.rb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ def config
4343
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
4444
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
4545

46-
# Default for class
47-
class_attribute :__vc_strict_helpers_enabled, instance_accessor: false, instance_predicate: false, default: false
48-
# Changeable for instance
49-
attr_writer :__vc_strict_helpers_enabled
50-
51-
def __vc_strict_helpers_enabled
52-
@__vc_strict_helpers_enabled.nil? ? self.class.__vc_strict_helpers_enabled : @__vc_strict_helpers_enabled
53-
end
54-
alias_method :strict_helpers_enabled?, :__vc_strict_helpers_enabled
55-
5646
attr_accessor :__vc_original_view_context
5747

5848
# Components render in their own view context. Helpers and other functionality
@@ -529,9 +519,16 @@ def inherited(child)
529519
# `compile` defines
530520
compile
531521

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

537534
# Give the child its own personal #render_template_for to protect against the case when
@@ -655,15 +652,6 @@ def strip_trailing_whitespace?
655652
__vc_strip_trailing_whitespace
656653
end
657654

658-
# TODO
659-
def strict_helpers_enabled=(value = true)
660-
self.__vc_strict_helpers_enabled = value
661-
end
662-
663-
def strict_helpers_enabled?
664-
__vc_strict_helpers_enabled
665-
end
666-
667655
# Ensure the component initializer accepts the
668656
# collection parameter. By default, we don't
669657
# validate that the default parameter name

lib/view_component/config.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class << self
1111
alias_method :default, :new
1212

1313
def defaults
14-
ActiveSupport::OrderedOptions.new.merge!({
14+
ActiveSupport::InheritableOptions.new({
1515
generate: default_generate_options,
1616
preview_controller: "ViewComponentsController",
1717
preview_route: "/rails/view_components",
@@ -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

lib/view_component/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module ViewComponent
88
class Engine < Rails::Engine # :nodoc:
9-
config.view_component = ViewComponent::Config.current
9+
config.view_component = ViewComponent::Config.defaults
1010

1111
rake_tasks do
1212
load "view_component/rails/tasks/view_component.rake"

lib/view_component/use_helpers.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "view_component/errors"
4+
35
module ViewComponent::UseHelpers
46
extend ActiveSupport::Concern
57

@@ -13,7 +15,7 @@ def use_helper(helper_method, from: nil, prefix: false)
1315

1416
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
1517
def #{helper_method_name}(*args, &block)
16-
raise HelpersCalledBeforeRenderError if view_context.nil?
18+
raise ::ViewComponent::HelpersCalledBeforeRenderError if view_context.nil?
1719
1820
#{define_helper(helper_method: helper_method, source: from)}
1921
end

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)