Skip to content

Commit 37e6d0d

Browse files
committed
Make accommodations for component-local config
Introduces a `ViewComponent::GlobalConfig` object that will be the source for global configuration going forward. Ideally in the future, this will only house options that universally affect ViewComponent regardless of whether components are sourced from an engine or not (e.g. enabling the capture compatibility patch), and more options can move to a component-local config. For these options, classes inheriting from `ViewComponent::Base` will want to override configuration themselves. This was initially written to support extracting the incoming strict_helpers_enabled? option, but applies to everything.
1 parent 14446d8 commit 37e6d0d

File tree

23 files changed

+58
-44
lines changed

23 files changed

+58
-44
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace :docs do
9595
require "rails"
9696
require "action_controller"
9797
require "view_component"
98-
ViewComponent::Base.config.view_component_path = "view_component"
98+
ViewComponent::GlobalConfig.view_component_path = "view_component"
9999
require "view_component/docs_builder_component"
100100

101101
error_keys = registry.keys.select { |key| key.to_s.include?("Error::MESSAGE") }.map(&:to_s)

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
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 ViewComponent::Base.config.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
2+
<% if ViewComponent::GlobalConfig.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 ViewComponent::Base.config.show_previews_source %>
11+
<% if ViewComponent::GlobalConfig.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
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Make accommodations for component-local config to be introduced in future.
14+
15+
*Simon Fish*
16+
1317
## 3.18.0
1418

1519
* Enable components to use `@request` and `request` methods/ivars.

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}")

0 commit comments

Comments
 (0)