Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Add ability to pass explicit `preview_path` to preview generator.

*Erinna Chen*

* Add `with_rendered_component_path` helper for writing component system tests.

*Edwin Mak*
Expand Down
11 changes: 9 additions & 2 deletions lib/rails/generators/preview/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

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, maybe preview_paths.first might be a good option here.

Suggested change
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: ViewComponent::Base.config.generate.preview_paths.first

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 existing preview_paths, I think it'd make sense in that context.

Copy link
Member

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?

Copy link
Contributor Author

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.


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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
preview_paths.one? ? preview_paths.first : "test/components/previews"
preview_paths.any? ? preview_paths.first : "test/components/previews"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Expand Down
19 changes: 18 additions & 1 deletion lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Required when there is more than one path defined in preview_paths.
# Required when there is more than one path defined in config.view_component.preview_paths.

# 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]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/sandbox/test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
end

def test_defaults_are_correct
assert_equal @config.generate, {}
assert_equal @config.generate, {preview_path: ""}
assert_equal @config.preview_controller, "ViewComponentsController"
assert_equal @config.preview_route, "/rails/view_components"
assert_equal @config.show_previews_source, false
Expand Down
22 changes: 22 additions & 0 deletions test/sandbox/test/generators/preview_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def test_component_preview
end
end

def test_component_preview_with_preview_path_option
with_preview_paths([]) do
run_generator %w[user --preview --preview-path other/test/components/previews]

assert_file "other/test/components/previews/user_component_preview.rb" do |component|
assert_match(/class UserComponentPreview < /, component)
assert_match(/render\(UserComponent.new\)/, component)
end
end
end

def test_component_preview_with_one_overridden_preview_path
with_preview_paths(%w[spec/components/previews]) do
run_generator %w[user --preview]
Expand All @@ -42,6 +53,17 @@ def test_component_preview_with_two_overridden_preview_paths
end
end

def test_component_preview_with_two_overridden_preview_paths_and_preview_path_option
with_preview_paths(%w[spec/components/previews some/other/directory]) do
run_generator %w[user --preview --preview-path other/test/components/previews]

assert_file "other/test/components/previews/user_component_preview.rb" do |component|
assert_match(/class UserComponentPreview < /, component)
assert_match(/render\(UserComponent.new\)/, component)
end
end
end

def test_component_preview_with_namespace
with_preview_paths([]) do
run_generator %w[admins/user --preview]
Expand Down