Skip to content

Commit e92d778

Browse files
committed
Make GlobalConfig a proxy for Rails app config or ViewComponent base config as necessary
1 parent 29eca2a commit e92d778

File tree

16 files changed

+42
-39
lines changed

16 files changed

+42
-39
lines changed

app/controllers/concerns/view_component/preview_actions.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ def previews
4747

4848
# :doc:
4949
def default_preview_layout
50-
ViewComponent::Base.config.default_preview_layout
50+
GlobalConfig.default_preview_layout
5151
end
5252

5353
# :doc:
5454
def show_previews?
55-
ViewComponent::Base.config.show_previews
55+
GlobalConfig.show_previews
5656
end
5757

5858
# :doc:
@@ -95,7 +95,7 @@ def prepend_application_view_paths
9595
end
9696

9797
def prepend_preview_examples_view_path
98-
prepend_view_path(ViewComponent::Base.preview_paths)
98+
prepend_view_path(GlobalConfig.preview_paths)
9999
end
100100
end
101101
end

app/helpers/preview_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def find_template_data(lookup_context:, template_identifier:)
3535
# Fetch template source via finding it through preview paths
3636
# to accomodate source view when exclusively using templates
3737
# for previews for Rails < 6.1.
38-
all_template_paths = ViewComponent::Base.config.preview_paths.map do |preview_path|
38+
all_template_paths = ViewComponent::GlobalConfig.preview_paths.map do |preview_path|
3939
Dir.glob("#{preview_path}/**/*")
4040
end.flatten
4141

@@ -80,6 +80,6 @@ def prism_language_name_by_template_path(template_file_path:)
8080
# :nocov:
8181

8282
def serve_static_preview_assets?
83-
ViewComponent::Base.config.show_previews && Rails.application.config.public_file_server.enabled
83+
ViewComponent::GlobalConfig.show_previews && Rails.application.config.public_file_server.enabled
8484
end
8585
end

lib/rails/generators/abstract_generator.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def file_name
2929
end
3030

3131
def component_path
32-
ViewComponent::Base.config.view_component_path
32+
GlobalConfig.view_component_path
3333
end
3434

3535
def stimulus_controller
@@ -42,15 +42,15 @@ def stimulus_controller
4242
end
4343

4444
def sidecar?
45-
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
45+
options["sidecar"] || GlobalConfig.generate.sidecar
4646
end
4747

4848
def stimulus?
49-
options["stimulus"] || ViewComponent::Base.config.generate.stimulus_controller
49+
options["stimulus"] || GlobalConfig.generate.stimulus_controller
5050
end
5151

5252
def typescript?
53-
options["typescript"] || ViewComponent::Base.config.generate.typescript
53+
options["typescript"] || GlobalConfig.generate.typescript
5454
end
5555
end
5656
end

lib/rails/generators/component/component_generator.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class ComponentGenerator < Rails::Generators::NamedBase
1313
check_class_collision suffix: "Component"
1414

1515
class_option :inline, type: :boolean, default: false
16-
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
16+
class_option :locale, type: :boolean, default: ViewComponent::GlobalConfig.generate.locale
1717
class_option :parent, type: :string, desc: "The parent class for the generated component"
18-
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
18+
class_option :preview, type: :boolean, default: ViewComponent::GlobalConfig.generate.preview
1919
class_option :sidecar, type: :boolean, default: false
2020
class_option :stimulus, type: :boolean,
21-
default: ViewComponent::Base.config.generate.stimulus_controller
21+
default: ViewComponent::GlobalConfig.generate.stimulus_controller
2222

2323
def create_component_file
2424
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
@@ -41,7 +41,7 @@ def create_component_file
4141
def parent_class
4242
return options[:parent] if options[:parent]
4343

44-
ViewComponent::Base.config.component_parent_class || default_parent_class
44+
ViewComponent::GlobalConfig.component_parent_class || default_parent_class
4545
end
4646

4747
def initialize_signature

lib/rails/generators/locale/component_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
1212
class_option :sidecar, type: :boolean, default: false
1313

1414
def create_locale_file
15-
if ViewComponent::Base.config.generate.distinct_locale_files
15+
if ViewComponent::GlobalConfig.generate.distinct_locale_files
1616
I18n.available_locales.each do |locale|
1717
create_file destination(locale), translations_hash([locale]).to_yaml
1818
end

lib/rails/generators/preview/component_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module Preview
44
module Generators
55
class ComponentGenerator < ::Rails::Generators::NamedBase
66
source_root File.expand_path("templates", __dir__)
7-
class_option :preview_path, type: :string, desc: "Path for previews, required when multiple preview paths are configured", default: ViewComponent::Base.config.generate.preview_path
7+
class_option :preview_path, type: :string, desc: "Path for previews, required when multiple preview paths are configured", default: ViewComponent::GlobalConfig.generate.preview_path
88

99
argument :attributes, type: :array, default: [], banner: "attribute"
1010
check_class_collision suffix: "ComponentPreview"
1111

1212
def create_preview_file
13-
preview_paths = ViewComponent::Base.config.preview_paths
13+
preview_paths = ViewComponent::GlobalConfig.preview_paths
1414
optional_path = options[:preview_path]
1515
return if preview_paths.count > 1 && optional_path.blank?
1616

lib/rails/generators/rspec/component_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create_test_file
1616
private
1717

1818
def spec_component_path
19-
return "spec/components" unless ViewComponent::Base.config.generate.use_component_path_for_rspec_tests
19+
return "spec/components" unless ViewComponent::GlobalConfig.generate.use_component_path_for_rspec_tests
2020

2121
configured_component_path = component_path
2222
if configured_component_path.start_with?("app#{File::SEPARATOR}")

lib/view_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module ViewComponent
1313
autoload :ComponentError
1414
autoload :Config
1515
autoload :Deprecation
16+
autoload :GlobalConfig
1617
autoload :InlineTemplate
1718
autoload :Instrumentation
1819
autoload :Preview

lib/view_component/base.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "action_view"
4-
require "active_support/configurable"
54
require "view_component/collection"
65
require "view_component/compile_cache"
76
require "view_component/compiler"
@@ -45,6 +44,8 @@ def config
4544
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
4645
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
4746

47+
delegate :component_config, to: :class
48+
4849
attr_accessor :__vc_original_view_context
4950

5051
# Components render in their own view context. Helpers and other functionality
@@ -231,7 +232,7 @@ def controller
231232
# @return [ActionView::Base]
232233
def helpers
233234
raise HelpersCalledBeforeRenderError if view_context.nil?
234-
raise StrictHelperError unless ViewComponent::Base.config.helpers_enabled
235+
raise StrictHelperError unless GlobalConfig.helpers_enabled
235236
# Attempt to re-use the original view_context passed to the first
236237
# component rendered in the rendering pipeline. This prevents the
237238
# instantiation of a new view_context via `controller.view_context` which
@@ -248,7 +249,7 @@ def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToM
248249
super
249250
rescue => e # rubocop:disable Style/RescueStandardError
250251
e.set_backtrace e.backtrace.tap(&:shift)
251-
if !ViewComponent::Base.config.helpers_enabled
252+
if !GlobalConfig.helpers_enabled
252253
raise e, <<~MESSAGE.chomp if view_context && e.is_a?(NameError) && (__vc_original_view_context.respond_to?(method_name) || controller.view_context.respond_to?(method_name))
253254
#{e.message}
254255
@@ -519,15 +520,13 @@ def inherited(child)
519520
# `compile` defines
520521
compile
521522

522-
child.include ActiveSupport::Configurable
523-
524523
if child.superclass == ViewComponent::Base
525-
child.define_singleton_method(:config) do
526-
@@config ||= Rails.application.config.view_component.inheritable_copy
524+
child.define_singleton_method(:component_config) do
525+
@@component_config ||= Rails.application.config.view_component.inheritable_copy
527526
end
528527
else
529-
child.define_singleton_method(:config) do
530-
@@config ||= superclass.config.inheritable_copy
528+
child.define_singleton_method(:component_config) do
529+
@@component_config ||= superclass.component_config.inheritable_copy
531530
end
532531
end
533532

@@ -563,7 +562,7 @@ def render_template_for(variant = nil, format = nil)
563562
# If Rails application is loaded, removes the first part of the path and the extension.
564563
if defined?(Rails) && Rails.application
565564
child.virtual_path = child.source_location.gsub(
566-
/(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, ""
565+
/(.*#{Regexp.quote(GlobalConfig.view_component_path)})|(\.rb)/, ""
567566
)
568567
end
569568

lib/view_component/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Engine < Rails::Engine # :nodoc:
1515
else
1616
initializer "view_component.stats_directories" do |app|
1717
require "rails/code_statistics"
18-
dir = ViewComponent::Base.view_component_path
18+
dir = GlobalConfig.view_component_path
1919
Rails::CodeStatistics.register_directory("ViewComponents", dir)
2020
end
2121
end

0 commit comments

Comments
 (0)