Sha256: 850db52fc620f72e5cbaef146076df7235f04c013c1d049e12e510705eb23389

Contents?: true

Size: 1.25 KB

Versions: 35

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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
        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"
        end

        template "component_preview.rb", File.join(path_prefix, class_path, "#{file_name}_component_preview.rb")
      end

      private

      def file_name
        @_file_name ||= super.sub(/_component\z/i, "")
      end

      def render_signature
        return if attributes.blank?

        attributes.map { |attr| %(#{attr.name}: "#{attr.name}") }.join(", ")
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
view_component-3.20.0 lib/rails/generators/preview/component_generator.rb
view_component-3.19.0 lib/rails/generators/preview/component_generator.rb
view_component-3.18.0 lib/rails/generators/preview/component_generator.rb
view_component-3.17.0 lib/rails/generators/preview/component_generator.rb
view_component-3.16.0 lib/rails/generators/preview/component_generator.rb
view_component-3.15.1 lib/rails/generators/preview/component_generator.rb
view_component-3.15.0 lib/rails/generators/preview/component_generator.rb
view_component-3.14.0 lib/rails/generators/preview/component_generator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/view_component-2.83.0/lib/rails/generators/preview/component_generator.rb
view_component-3.13.0 lib/rails/generators/preview/component_generator.rb
view_component-3.12.1 lib/rails/generators/preview/component_generator.rb
view_component-3.12.0 lib/rails/generators/preview/component_generator.rb
view_component-3.11.0 lib/rails/generators/preview/component_generator.rb
view_component-3.10.0 lib/rails/generators/preview/component_generator.rb
view_component-2.83.0 lib/rails/generators/preview/component_generator.rb
view_component-3.9.0 lib/rails/generators/preview/component_generator.rb
view_component-3.8.0 lib/rails/generators/preview/component_generator.rb
view_component-3.7.0 lib/rails/generators/preview/component_generator.rb
view_component-3.6.0 lib/rails/generators/preview/component_generator.rb
view_component-3.5.0 lib/rails/generators/preview/component_generator.rb