Skip to content

Commit c271c5a

Browse files
boardfishSponejoelhawksley
authored
Centralize ViewComponent::Base config (#1291)
* Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
1 parent 8ed02f9 commit c271c5a

File tree

22 files changed

+325
-198
lines changed

22 files changed

+325
-198
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
/app/**/* @juanmanuelramallo
2727
/docs/guide/previews.md @juanmanuelramallo
2828
/lib/view_component/preview.rb @juanmanuelramallo
29-
/lib/view_component/previewable.rb @juanmanuelramallo
3029
/lib/view_component/preview_template_error.rb @juanmanuelramallo
3130
/test/sandbox/test/components/previews/preview_source_from_layout_component_preview.rb @juanmanuelramallo
3231
/test/view_component/default_preview_layout_integration_test.rb @juanmanuelramallo
@@ -46,3 +45,5 @@
4645
/script/replicate-bug @boardfish
4746
/lib/view_component/docs_builder_component.rb @boardfish
4847
/lib/view_component/docs_builder_component.html.erb @boardfish
48+
/lib/view_component/config.rb @boardfish
49+
/test/view_component/config_test.rb @boardfish

Rakefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ namespace :docs do
7070

7171
instance_methods_to_document = meths.select { |method| method.scope != :class }
7272
class_methods_to_document = meths.select { |method| method.scope == :class }
73-
configuration_methods_to_document = registry.get("ViewComponent::Base").meths.select { |method|
74-
method[:mattr_accessor]
75-
}
73+
configuration_methods_to_document = registry.get("ViewComponent::Config").meths.select(&:reader?)
7674
test_helper_methods_to_document = registry
7775
.get("ViewComponent::TestHelpers")
7876
.meths

app/controllers/concerns/view_component/preview_actions.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ module PreviewActions
1010
around_action :set_locale, only: :previews
1111
before_action :require_local!, unless: :show_previews?
1212

13-
if respond_to?(:content_security_policy)
14-
content_security_policy(false)
15-
end
13+
content_security_policy(false) if respond_to?(:content_security_policy)
1614
end
1715

1816
def index
@@ -45,18 +43,18 @@ def previews
4543

4644
# :doc:
4745
def default_preview_layout
48-
ViewComponent::Base.default_preview_layout
46+
Rails.application.config.view_component.default_preview_layout
4947
end
5048

5149
# :doc:
5250
def show_previews?
53-
ViewComponent::Base.show_previews
51+
Rails.application.config.view_component.show_previews
5452
end
5553

5654
# :doc:
5755
def find_preview
5856
candidates = []
59-
params[:path].to_s.scan(%r{/|$}) { candidates << $` }
57+
params[:path].to_s.scan(%r{/|$}) { candidates << Regexp.last_match.pre_match }
6058
preview = candidates.detect { |candidate| ViewComponent::Preview.exists?(candidate) }
6159

6260
if preview
@@ -66,10 +64,8 @@ def find_preview
6664
end
6765
end
6866

69-
def set_locale
70-
I18n.with_locale(params[:locale] || I18n.default_locale) do
71-
yield
72-
end
67+
def set_locale(&block)
68+
I18n.with_locale(params[:locale] || I18n.default_locale, &block)
7369
end
7470

7571
# Returns either {} or {layout: value} depending on configuration

app/helpers/preview_helper.rb

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

33
module PreviewHelper
4-
AVAILABLE_PRISM_LANGUAGES = ["ruby", "erb", "haml"]
4+
AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml]
55
FALLBACK_LANGUAGE = "ruby"
66

77
def preview_source
@@ -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 = ViewComponent::Base.preview_paths.map do |preview_path|
25+
all_template_paths = Rails.application.config.view_component.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 ViewComponent::Base.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
2+
<% if Rails.application.config.view_component.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.show_previews_source %>
11+
<% if Rails.application.config.view_component.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+
* Add missing `generate.sidecar`, `generate.stimulus_controller`, `generate.locale`, `generate.distinct_locale_files`, `generate.preview` config options to `config.view_component`.
13+
14+
*Simon Fish*
15+
1216
## 2.65.0
1317

1418
* Raise `ArgumentError` when conflicting Slots are defined.

lib/rails/generators/abstract_generator.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
module ViewComponent
44
module AbstractGenerator
55
def copy_view_file
6-
unless options["inline"]
7-
template "component.html.#{engine_name}", destination
8-
end
6+
template "component.html.#{engine_name}", destination unless options["inline"]
97
end
108

119
private
@@ -31,7 +29,7 @@ def file_name
3129
end
3230

3331
def component_path
34-
ViewComponent::Base.view_component_path
32+
Rails.application.config.view_component.view_component_path
3533
end
3634

3735
def stimulus_controller
@@ -44,7 +42,7 @@ def stimulus_controller
4442
end
4543

4644
def sidecar?
47-
options["sidecar"] || ViewComponent::Base.generate.sidecar
45+
options["sidecar"] || Rails.application.config.view_component.generate.sidecar
4846
end
4947
end
5048
end

lib/rails/generators/component/component_generator.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +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.generate.locale
16+
class_option :locale, type: :boolean, default: Rails.application.config.view_component.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.generate.preview
18+
class_option :preview, type: :boolean, default: Rails.application.config.view_component.generate.preview
1919
class_option :sidecar, type: :boolean, default: false
20-
class_option :stimulus, type: :boolean, default: ViewComponent::Base.generate.stimulus_controller
20+
class_option :stimulus, type: :boolean,
21+
default: Rails.application.config.view_component.generate.stimulus_controller
2122

2223
def create_component_file
2324
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
@@ -40,7 +41,7 @@ def create_component_file
4041
def parent_class
4142
return options[:parent] if options[:parent]
4243

43-
ViewComponent::Base.component_parent_class || default_parent_class
44+
Rails.application.config.view_component.component_parent_class || default_parent_class
4445
end
4546

4647
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.generate.distinct_locale_files
15+
if Rails.application.config.view_component.generate.distinct_locale_files
1616
I18n.available_locales.each do |locale|
1717
create_file destination(locale), translations_hash([locale]).to_yaml
1818
end

lib/view_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module ViewComponent
1010
autoload :Compiler
1111
autoload :CompileCache
1212
autoload :ComponentError
13+
autoload :Config
1314
autoload :Deprecation
1415
autoload :Instrumentation
1516
autoload :Preview

0 commit comments

Comments
 (0)