Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace :docs do

instance_methods_to_document = meths.select { |method| method.scope != :class }
class_methods_to_document = meths.select { |method| method.scope == :class }
configuration_methods_to_document = registry.get("ViewComponent::Config").meths.select(&:reader?)
configuration_methods_to_document = registry.get("ViewComponent::ApplicationConfig").meths.select(&:reader?)
test_helper_methods_to_document = registry
.get("ViewComponent::TestHelpers")
.meths
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/concerns/view_component/preview_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def previews

# :doc:
def default_preview_layout
ViewComponent::Base.config.default_preview_layout
Rails.application.config.view_component.previews.default_layout
end

# :doc:
def show_previews?
ViewComponent::Base.config.show_previews
Rails.application.config.view_component.previews.show
end

# :doc:
Expand Down Expand Up @@ -102,7 +102,7 @@ def prepend_application_view_paths
end

def prepend_preview_examples_view_path
prepend_view_path(ViewComponent::Base.preview_paths)
prepend_view_path(Rails.application.config.view_component.previews.paths)
end
end
end
2 changes: 1 addition & 1 deletion app/helpers/preview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def prism_language_name_by_template_path(template_file_path:)
# :nocov:

def serve_static_preview_assets?
ViewComponent::Base.config.show_previews && Rails.application.config.public_file_server.enabled
Rails.application.config.view_component.show_previews && Rails.application.config.public_file_server.enabled
end
end
2 changes: 1 addition & 1 deletion app/views/view_components/preview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
<% end %>

<% if ViewComponent::Base.config.show_previews_source %>
<% if Rails.application.config.view_component.previews.show_source %>
<%= preview_source %>
<% end %>
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ nav_order: 5

## 4.0.0

* BREAKING: Make most configuration local to component instances.

Simon Fish

* BREAKING: Require [non-EOL](https://endoflife.date/rails) Rails (`>= 7.1.0`).

*Joel Hawksley*
Expand Down
9 changes: 5 additions & 4 deletions lib/rails/generators/abstract_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def file_name
end

def component_path
ViewComponent::Base.config.view_component_path
# FIXME: Doesn't yet handle multiple component paths
Rails.application.config.view_component.generate.view_component_paths!.first
end

def stimulus_controller
Expand All @@ -42,15 +43,15 @@ def stimulus_controller
end

def sidecar?
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
options["sidecar"] || Rails.application.config.view_component.generate.sidecar
end

def stimulus?
options["stimulus"] || ViewComponent::Base.config.generate.stimulus_controller
options["stimulus"] || Rails.application.config.view_component.generate.stimulus_controller
end

def typescript?
options["typescript"] || ViewComponent::Base.config.generate.typescript
options["typescript"] || Rails.application.config.view_component.generate.typescript
end
end
end
13 changes: 7 additions & 6 deletions lib/rails/generators/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class ComponentGenerator < Rails::Generators::NamedBase
check_class_collision suffix: "Component"

class_option :inline, type: :boolean, default: false
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
class_option :parent, type: :string, desc: "The parent class for the generated component"
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
class_option :sidecar, type: :boolean, default: false
class_option :locale, type: :boolean, default: Rails.application.config.view_component.generate.locale
class_option :parent, type: :string, desc: "The parent class for the generated component",
default: Rails.application.config.view_component.generate.component_parent_class
class_option :preview, type: :boolean, default: Rails.application.config.view_component.generate.preview
class_option :sidecar, type: :boolean, default: Rails.application.config.view_component.generate.sidecar
class_option :stimulus, type: :boolean,
default: ViewComponent::Base.config.generate.stimulus_controller
default: Rails.application.config.view_component.generate.stimulus_controller
class_option :skip_suffix, type: :boolean, default: false

def create_component_file
Expand All @@ -42,7 +43,7 @@ def create_component_file
def parent_class
return options[:parent] if options[:parent]

ViewComponent::Base.config.component_parent_class || default_parent_class
Rails.application.config.view_component.generate.component_parent_class || default_parent_class
end

def initialize_signature
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/generators/locale/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
class_option :sidecar, type: :boolean, default: false

def create_locale_file
if ViewComponent::Base.config.generate.distinct_locale_files
if Rails.application.config.view_component.generate.distinct_locale_files
I18n.available_locales.each do |locale|
create_file destination(locale), translations_hash([locale]).to_yaml
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rails/generators/preview/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Preview
module Generators
class ComponentGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)
class_option :preview_path, type: :string, desc: "Path for previews, required when multiple preview paths are configured", default: ViewComponent::Base.config.generate.preview_path
class_option :preview_path, type: :string, desc: "Path for previews, required when multiple preview paths are configured", default: Rails.application.config.view_component.generate.preview_path

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

def create_preview_file
preview_paths = ViewComponent::Base.config.preview_paths
preview_paths = Rails.application.config.view_component.previews.paths
optional_path = options[:preview_path]
return if preview_paths.count > 1 && optional_path.blank?

Expand Down
2 changes: 1 addition & 1 deletion lib/rails/generators/rspec/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create_test_file
private

def spec_component_path
return "spec/components" unless ViewComponent::Base.config.generate.use_component_path_for_rspec_tests
return "spec/components" unless Rails.application.config.view_component.generate.use_component_path_for_rspec_tests

configured_component_path = component_path
if configured_component_path.start_with?("app#{File::SEPARATOR}")
Expand Down
4 changes: 2 additions & 2 deletions lib/rails/generators/stimulus/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
include ViewComponent::AbstractGenerator

source_root File.expand_path("templates", __dir__)
class_option :sidecar, type: :boolean, default: false
class_option :typescript, type: :boolean, default: false
class_option :sidecar, type: :boolean, default: Rails.application.config.view_component.generate.sidecar
class_option :typescript, type: :boolean, default: Rails.application.config.view_component.generate.typescript

def create_stimulus_controller
template "component_controller.#{filetype}", destination
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
module ViewComponent
extend ActiveSupport::Autoload

autoload :ApplicationConfig
autoload :Base
autoload :CaptureCompatibility
autoload :Compiler
autoload :CompileCache
autoload :ComponentError
autoload :Config
autoload :Deprecation
autoload :InlineTemplate
autoload :Instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@
require "view_component/deprecation"

module ViewComponent
class Config
class ApplicationConfig
class << self
# `new` without any arguments initializes the default configuration, but
# it's important to differentiate in case that's no longer the case in
# future.
alias_method :default, :new

def defaults
ActiveSupport::OrderedOptions.new.merge!({
generate: default_generate_options,
preview_controller: "ViewComponentsController",
preview_route: "/rails/view_components",
show_previews_source: false,
ActiveSupport::OrderedOptions[
generate: ActiveSupport::OrderedOptions[
sidecar: false,
stimulus_controller: false,
typescript: false,
locale: false,
distinct_locale_files: false,
preview: false,
preview_path: "",
use_component_path_for_rspec_tests: false,
view_component_paths: ["app/components"],
component_parent_class: nil
],
previews: ActiveSupport::OrderedOptions[
show: (Rails.env.development? || Rails.env.test?),
controller: "ViewComponentsController",
route: "/rails/view_components",
show_source: (Rails.env.development? || Rails.env.test?),
paths: default_preview_paths,
default_layout: nil
],
instrumentation_enabled: false,
use_deprecated_instrumentation_name: true,
view_component_path: "app/components",
component_parent_class: nil,
show_previews: Rails.env.development? || Rails.env.test?,
preview_paths: default_preview_paths,
test_controller: "ApplicationController",
default_preview_layout: nil,
capture_compatibility_patch_enabled: false
})
capture_compatibility_patch_enabled: false,
]
end

# @!attribute generate
Expand Down Expand Up @@ -97,68 +107,71 @@ def defaults
# For example, if the `view_component_path` is
# `app/views/components`, then the generator will create a new spec file
# in `spec/views/components/` rather than the default `spec/components/`.
#
# #### `#view_component_path``
#
# The path in which components, their templates, and their sidecars should
# be stored:
#
# config.view_component.generate.view_component_paths = "app/components"
#
# Defaults to `"app/components"`.
# TODO: It looks like this was actually the default path generators would use.
# I think it's used elsewhere inside `base.rb` for some reason, though.
#
# #### `#component_parent_class`
#
# The parent class from which generated components will inherit.
# Defaults to `nil`. If this is falsy, generators will use
# `"ApplicationComponent"` if defined, `"ViewComponent::Base"` otherwise.

# @!attribute preview_controller

# @!attribute previews
# @return [String]
# The subset of configuration options relating to previews.
#
# #### `#show`
#
# Whether component previews are enabled.
# Defaults to `true` in development and test environments.
#
# #### `#controller`
#
# The controller used for previewing components.
# Defaults to `ViewComponentsController`.

# @!attribute preview_route
# @return [String]
#
# #### `route`
#
# The entry route for component previews.
# Defaults to `"/rails/view_components"`.

# @!attribute show_previews_source
# @return [Boolean]
#
# #### `show_source`
#
# Whether to display source code previews in component previews.
# Defaults to `false`.
#
# #### `paths`
#
# The locations in which component previews will be looked up.
# Defaults to `['test/components/previews']` relative to your Rails root.
#
# #### `#default_layout`
#
# A custom default layout used for the previews index page and individual
# previews.
# Defaults to `nil`. If this is falsy, `"component_preview"` is used.

# @!attribute instrumentation_enabled
# @return [Boolean]
# Whether ActiveSupport notifications are enabled.
# Defaults to `false`.

# @!attribute use_deprecated_instrumentation_name
# @return [Boolean]
# Whether ActiveSupport Notifications use the private name `"!render.view_component"`
# or are made more publicly available via `"render.view_component"`.
# Will default to `false` in next major version.
# Defaults to `true`.

# @!attribute view_component_path
# @return [String]
# The path in which components, their templates, and their sidecars should
# be stored.
# Defaults to `"app/components"`.

# @!attribute component_parent_class
# @return [String]
# The parent class from which generated components will inherit.
# Defaults to `nil`. If this is falsy, generators will use
# `"ApplicationComponent"` if defined, `"ViewComponent::Base"` otherwise.

# @!attribute show_previews
# @return [Boolean]
# Whether component previews are enabled.
# Defaults to `true` in development and test environments.

# @!attribute preview_paths
# @return [Array<String>]
# The locations in which component previews will be looked up.
# Defaults to `['test/components/previews']` relative to your Rails root.

# @!attribute test_controller
# @return [String]
# The controller used for testing components.
# Can also be configured on a per-test basis using `#with_controller_class`.
# Defaults to `ApplicationController`.

# @!attribute default_preview_layout
# @return [String]
# A custom default layout used for the previews index page and individual
# previews.
# Defaults to `nil`. If this is falsy, `"component_preview"` is used.

# @!attribute capture_compatibility_patch_enabled
# @return [Boolean]
# Enables the experimental capture compatibility patch that makes ViewComponent
Expand All @@ -171,8 +184,6 @@ def default_preview_paths
end

def default_rails_preview_paths
return [] unless defined?(Rails.root) && Dir.exist?("#{Rails.root}/test/components/previews")

["#{Rails.root}/test/components/previews"]
end

Expand All @@ -189,22 +200,8 @@ def registered_rails_engines_with_previews
defined?(descendant.root) && Dir.exist?("#{descendant.root}/test/components/previews")
end
end

def default_generate_options
options = ActiveSupport::OrderedOptions.new(false)
options.preview_path = ""
options
end
end

# @!attribute current
# @return [ViewComponent::Config]
# Returns the current ViewComponent::Config. This is persisted against this
# class so that config options remain accessible before the rest of
# ViewComponent has loaded. Defaults to an instance of ViewComponent::Config
# with all other documented defaults set.
class_attribute :current, default: defaults, instance_predicate: false

def initialize
@config = self.class.defaults
end
Expand Down
Loading