-
Notifications
You must be signed in to change notification settings - Fork 472
Add support for explicit preview_path when generating component #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,15 +4,22 @@ 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 | ||||||
|
||||||
argument :attributes, type: :array, default: [], banner: "attribute" | ||||||
check_class_collision suffix: "ComponentPreview" | ||||||
|
||||||
def create_preview_file | ||||||
preview_paths = ViewComponent::Base.config.preview_paths | ||||||
return if preview_paths.count > 1 | ||||||
optional_path = options[:preview_path] | ||||||
return if preview_paths.count > 1 && optional_path.blank? | ||||||
|
||||||
path_prefix = if optional_path.present? | ||||||
optional_path | ||||||
else | ||||||
preview_paths.one? ? preview_paths.first : "test/components/previews" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a style change given the guard clause above: https://github.com/ViewComponent/view_component/pull/1598/files/73161e44b6f236249f39d07acca4f321129e9554#diff-4b0687f08bdb74ba50af06742f361ab33a8df5e60742b694564b4ce7a3689643R15 |
||||||
end | ||||||
|
||||||
path_prefix = preview_paths.one? ? preview_paths.first : "test/components/previews" | ||||||
template "component_preview.rb", File.join(path_prefix, class_path, "#{file_name}_component_preview.rb") | ||||||
end | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,7 +12,7 @@ class << self | |||||
|
||||||
def defaults | ||||||
ActiveSupport::OrderedOptions.new.merge!({ | ||||||
generate: ActiveSupport::OrderedOptions.new(false), | ||||||
generate: default_generate_options, | ||||||
preview_controller: "ViewComponentsController", | ||||||
preview_route: "/rails/view_components", | ||||||
show_previews_source: false, | ||||||
|
@@ -66,6 +66,17 @@ def defaults | |||||
# Always generate a preview alongside the component: | ||||||
# | ||||||
# config.view_component.generate.preview = true | ||||||
# | ||||||
# #### #preview_path | ||||||
# | ||||||
# Path to generate preview: | ||||||
# | ||||||
# config.view_component.generate.preview_path = "test/components/previews" | ||||||
# | ||||||
# Required when there is more than one path defined in preview_paths. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# Defaults to `""`. If this is blank, the generator will use | ||||||
# `ViewComponent.config.preview_paths` if defined, | ||||||
# `"test/components/previews"` otherwise | ||||||
|
||||||
# @!attribute preview_controller | ||||||
# @return [String] | ||||||
|
@@ -135,6 +146,12 @@ def default_preview_paths | |||||
|
||||||
["#{Rails.root}/test/components/previews"] | ||||||
end | ||||||
|
||||||
def default_generate_options | ||||||
options = ActiveSupport::OrderedOptions.new(false) | ||||||
options.preview_path = "" | ||||||
options | ||||||
end | ||||||
end | ||||||
|
||||||
def initialize | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given
preview_path
is due for deprecation, maybepreview_paths.first
might be a good option here.It might also be worth experimenting with using
preview_paths!.first
instead if the behavior feels right. Since this option appears to be for choosing from the existingpreview_paths
, I think it'd make sense in that context.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boardfish the deprecated
preview_path
option is for which previews VC looks up, not where previews are generated, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hesitated to used preview_paths.first since its behavior is dependent on how your app is configured.