Last active
September 10, 2023 00:16
-
-
Save jonathandean/43219590bdefc4acf90e635a96f6de6f to your computer and use it in GitHub Desktop.
ViewComponent Lookbook preview generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ComponentPreviewGenerator < Rails::Generators::NamedBase | |
source_root File.expand_path("templates", __dir__) | |
class_option :preview_names, type: :array, default: ["default"], desc: "A list of different previews to add to the configuration" | |
class_option :grouped, type: :string, default: 'true', desc: "Should the previews be grouped into one view?" | |
class_option :erb_files, type: :string, default: 'true', desc: "Should the previews use erb templates? If false, preview methods must render the component." | |
desc "Create a Lookbook preview for a ViewComponent" | |
def create_lookbook_preview | |
template "view_component_preview.rb", "spec/components/previews/#{file_path}_preview.rb" | |
if options[:erb_files] == 'true' | |
options[:preview_names].each do |preview_name| | |
create_file "spec/components/previews/#{file_path}_preview/#{preview_name}.html.erb" | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description: | |
Make ViewComponent previews for Lookbook | |
Example: | |
bin/rails generate component_preview my_new_component | |
This will create: | |
spec/components/previews/my_new_component_preview.rb | |
spec/components/previews/my_new_component_preview/default.html.erb | |
Options: | |
bin/rails generate component_preview --help |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class <%= class_name %>Preview < ViewComponent::Preview | |
layout "view_component_preview" | |
<%= options[:grouped] == 'true' ? '# @!group' : '' %> | |
<% options[:preview_names].each do |preview_name| %> | |
def <%= preview_name %> | |
end | |
<% end %> | |
<%= options[:grouped] == 'true' ? '# @!endgroup' : '' %> | |
end |
Author
jonathandean
commented
Sep 10, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment