rails g view_component NomeDoComponente
with stimulus:
rails g view_component NomeDoComponente –stimulus
File: - component.html.erb html for the component - component.rb ruby code for internal component logic - preview.rb preview for lookbook - /spec/component_spec.rb spec for component with rspec
# frozen_string_literal: true # Based on https://github.com/github/view_component/blob/master/lib/rails/generators/component/component_generator.rb class ViewComponentGenerator < Rails::Generators::NamedBase source_root File.expand_path("templates", __dir__) class_option :skip_test, type: :boolean, default: false class_option :skip_preview, type: :boolean, default: false class_option :stimulus, type: :boolean, default: false argument :attributes, type: :array, default: [], banner: "attribute" DEFAULT_PATH = "components" DEFAULT_COMPONENT_ROOT = "app/#{DEFAULT_PATH}" def create_component_file template "component.rb", File.join(DEFAULT_COMPONENT_ROOT, class_path, file_name, "component.rb") end def create_template_file template "component.html.erb", File.join(DEFAULT_COMPONENT_ROOT, class_path, file_name, "component.html.erb") end def create_stimulus_controller return unless options[:stimulus] template "controller.js", File.join(DEFAULT_COMPONENT_ROOT, class_path, file_name, "controller.js") end def create_test_file return if options[:skip_test] template "component_spec.rb", File.join("spec/#{DEFAULT_PATH}", class_path, file_name, "component_spec.rb") end def create_preview_file return if options[:skip_preview] template "preview.rb", File.join(DEFAULT_COMPONENT_ROOT, class_path, file_name, "preview.rb") end private def render_signature return if attributes.blank? attributes.map { |attr| %(#{attr.name}: "#{attr.name}") }.join(", ") end def parent_class "ApplicationViewComponent" end def preview_parent_class "ApplicationViewComponentPreview" end def data_attributes " data-controller=\"#{stimulus_controller}\"" if options["stimulus"] end def stimulus_controller if options["stimulus"] File.join(DEFAULT_COMPONENT_ROOT, class_path, file_name, "controller.js") .sub("#{DEFAULT_COMPONENT_ROOT}/", "") .tr("_", "-") .gsub(".js", "") .gsub("/", "--") end end def initialize_signature return if attributes.blank? attributes.map { |attr| "#{attr.name}:" }.join(", ") end def initialize_body attributes.map { |attr| "@#{attr.name} = #{attr.name}" }.join("\n ") end end