Skip to content

Commit 3ef2840

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 1ad9df2 commit 3ef2840

File tree

23 files changed

+63
-49
lines changed

23 files changed

+63
-49
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
@@ -54,12 +54,12 @@ def previews
5454

5555
# :doc:
5656
def default_preview_layout
57-
ViewComponent::Base.config.default_preview_layout
57+
GlobalConfig.default_preview_layout
5858
end
5959

6060
# :doc:
6161
def show_previews?
62-
ViewComponent::Base.config.show_previews
62+
GlobalConfig.show_previews
6363
end
6464

6565
# :doc:
@@ -102,7 +102,7 @@ def prepend_application_view_paths
102102
end
103103

104104
def prepend_preview_examples_view_path
105-
prepend_view_path(ViewComponent::Base.preview_paths)
105+
prepend_view_path(GlobalConfig.preview_paths)
106106
end
107107
end
108108
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_for_preview_source(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: 8 additions & 4 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
* Remove JS and CSS docs as they proved difficult to maintain and lacked consensus.
1418

1519
*Joel Hawksley*
@@ -1410,7 +1414,7 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
14101414
14111415
*Joel Hawksley*
14121416
1413-
* The ViewComponent team at GitHub is hiring! We're looking for a Rails engineer with accessibility experience: [https://boards.greenhouse.io/github/jobs/4020166](https://boards.greenhouse.io/github/jobs/4020166). Reach out to joelhawksley@github.com with any questions!
1417+
* The ViewComponent team at GitHub is hiring! We're looking for a Rails engineer with accessibility experience: [https://boards.greenhouse.io/github/jobs/4020166](https://boards.greenhouse.io/github/jobs/4020166). Reach out to <joelhawksley@github.com> with any questions!
14141418

14151419
* The ViewComponent team is hosting a happy hour at RailsConf. Join us for snacks, drinks, and stickers: [https://www.eventbrite.com/e/viewcomponent-happy-hour-tickets-304168585427](https://www.eventbrite.com/e/viewcomponent-happy-hour-tickets-304168585427)
14161420

@@ -2174,7 +2178,7 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
21742178

21752179
*Matheus Richard*
21762180

2177-
* Are you interested in building the future of ViewComponent? GitHub is looking to hire a Senior Engineer to work on Primer ViewComponents and ViewComponent. Apply here: [US/Canada](https://github.com/careers) / [Europe](https://boards.greenhouse.io/github/jobs/3132294). Feel free to reach out to joelhawksley@github.com with any questions.
2181+
* Are you interested in building the future of ViewComponent? GitHub is looking to hire a Senior Engineer to work on Primer ViewComponents and ViewComponent. Apply here: [US/Canada](https://github.com/careers) / [Europe](https://boards.greenhouse.io/github/jobs/3132294). Feel free to reach out to <joelhawksley@github.com> with any questions.
21782182

21792183
*Joel Hawksley*
21802184

@@ -2192,7 +2196,7 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
21922196

21932197
## 2.31.0
21942198

2195-
_Note: This release includes an underlying change to Slots that may affect incorrect usage of the API, where Slots were set on a line prefixed by `<%=`. The result of setting a Slot shouldn't be returned. (`<%`)_
2199+
*Note: This release includes an underlying change to Slots that may affect incorrect usage of the API, where Slots were set on a line prefixed by `<%=`. The result of setting a Slot shouldn't be returned. (`<%`)*
21962200
21972201
* Add `#with_content` to allow setting content without a block.
21982202
@@ -2640,7 +2644,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
26402644

26412645
* The gem name is now `view_component`.
26422646
* ViewComponent previews are now accessed at `/rails/view_components`.
2643-
* ViewComponents can _only_ be rendered with the instance syntax: `render(MyComponent.new)`. Support for all other syntaxes has been removed.
2647+
* ViewComponents can *only* be rendered with the instance syntax: `render(MyComponent.new)`. Support for all other syntaxes has been removed.
26442648
* ActiveModel::Validations have been removed. ViewComponent generators no longer include validations.
26452649
* In Rails 6.1, no monkey patching is used.
26462650
* `to_component_class` has been removed.

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
class_option :skip_suffix, type: :boolean, default: false
2323

2424
def create_component_file
@@ -42,7 +42,7 @@ def create_component_file
4242
def parent_class
4343
return options[:parent] if options[:parent]
4444

45-
ViewComponent::Base.config.component_parent_class || default_parent_class
45+
ViewComponent::GlobalConfig.component_parent_class || default_parent_class
4646
end
4747

4848
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)