Skip to content

Commit eb6a34d

Browse files
authored
Use ViewComponent::Base.config as the internal endpoint for config (#1466)
* Use ViewComponent::Base.config as the internal endpoint for config Tests are in place to ensure that Rails.application.config.view_component refers to the same thing. * Ensure we're testing configs both ways around * Update changelog * Lint
1 parent 4241a14 commit eb6a34d

File tree

12 files changed

+37
-29
lines changed

12 files changed

+37
-29
lines changed

app/controllers/concerns/view_component/preview_actions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ def previews
4343

4444
# :doc:
4545
def default_preview_layout
46-
Rails.application.config.view_component.default_preview_layout
46+
ViewComponent::Base.config.default_preview_layout
4747
end
4848

4949
# :doc:
5050
def show_previews?
51-
Rails.application.config.view_component.show_previews
51+
ViewComponent::Base.config.show_previews
5252
end
5353

5454
# :doc:

app/helpers/preview_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def find_template_data(lookup_context:, template_identifier:)
2222
# Fetch template source via finding it through preview paths
2323
# to accomodate source view when exclusively using templates
2424
# for previews for Rails < 6.1.
25-
all_template_paths = Rails.application.config.view_component.preview_paths.map do |preview_path|
25+
all_template_paths = ViewComponent::Base.config.preview_paths.map do |preview_path|
2626
Dir.glob("#{preview_path}/**/*")
2727
end.flatten
2828

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% if @render_args[:component] %>
2-
<% if Rails.application.config.view_component.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
2+
<% if ViewComponent::Base.config.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
33
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
44
<% else %>
55
<%= render_component(@render_args[:component], &@render_args[:block]) %>
@@ -8,6 +8,6 @@
88
<%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
99
<% end %>
1010

11-
<% if Rails.application.config.view_component.show_previews_source %>
11+
<% if ViewComponent::Base.config.show_previews_source %>
1212
<%= preview_source %>
1313
<% end %>

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ title: Changelog
99

1010
## main
1111

12+
* Use ViewComponent::Base.config as the internal endpoint for config.
13+
14+
*Simon Fish*
15+
1216
* Fix bug where `#with_request_url`, when used with query string, set the incorrect `request.path` and `request.fullpath`.
1317

1418
*Franz Liedke*

lib/rails/generators/abstract_generator.rb

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

3131
def component_path
32-
Rails.application.config.view_component.view_component_path
32+
ViewComponent::Base.config.view_component_path
3333
end
3434

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

4444
def sidecar?
45-
options["sidecar"] || Rails.application.config.view_component.generate.sidecar
45+
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
4646
end
4747
end
4848
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: Rails.application.config.view_component.generate.locale
16+
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
1717
class_option :parent, type: :string, desc: "The parent class for the generated component"
18-
class_option :preview, type: :boolean, default: Rails.application.config.view_component.generate.preview
18+
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
1919
class_option :sidecar, type: :boolean, default: false
2020
class_option :stimulus, type: :boolean,
21-
default: Rails.application.config.view_component.generate.stimulus_controller
21+
default: ViewComponent::Base.config.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-
Rails.application.config.view_component.component_parent_class || default_parent_class
44+
ViewComponent::Base.config.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 Rails.application.config.view_component.generate.distinct_locale_files
15+
if ViewComponent::Base.config.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
99
check_class_collision suffix: "ComponentPreview"
1010

1111
def create_preview_file
12-
preview_paths = Rails.application.config.view_component.preview_paths
12+
preview_paths = ViewComponent::Base.config.preview_paths
1313
return if preview_paths.count > 1
1414

1515
path_prefix = preview_paths.one? ? preview_paths.first : "test/components/previews"

lib/view_component/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class << self
1818
delegate(*ViewComponent::Config.defaults.keys, to: :config)
1919

2020
def config
21-
Rails.application.config.view_component
21+
@config ||= ViewComponent::Config.defaults
2222
end
2323
end
2424

@@ -496,7 +496,7 @@ def render_template_for(variant = nil)
496496

497497
# Removes the first part of the path and the extension.
498498
child.virtual_path = child.source_location.gsub(
499-
/(.*#{Regexp.quote(Rails.application.config.view_component.view_component_path)})|(\.rb)/, ""
499+
/(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, ""
500500
)
501501

502502
# Set collection parameter to the extended component

lib/view_component/engine.rb

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

66
module ViewComponent
77
class Engine < Rails::Engine # :nodoc:
8-
config.view_component = ViewComponent::Config.default
8+
config.view_component = ViewComponent::Base.config
99

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

0 commit comments

Comments
 (0)