Sha256: 8536280698ce9b0805fbbc85569445fb10690e7b82893fd9442ec2c996fa09c7

Contents?: true

Size: 1.98 KB

Versions: 48

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "rails/generators/abstract_generator"

module Rails
  module Generators
    class ComponentGenerator < Rails::Generators::NamedBase
      include ViewComponent::AbstractGenerator

      source_root File.expand_path("templates", __dir__)

      argument :attributes, type: :array, default: [], banner: "attribute"
      check_class_collision suffix: "Component"

      class_option :inline, type: :boolean, default: false
      class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
      class_option :parent, type: :string, desc: "The parent class for the generated component"
      class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
      class_option :sidecar, type: :boolean, default: false
      class_option :stimulus, type: :boolean,
        default: ViewComponent::Base.config.generate.stimulus_controller

      def create_component_file
        template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
      end

      hook_for :test_framework

      hook_for :preview, type: :boolean

      hook_for :stimulus, type: :boolean

      hook_for :locale, type: :boolean

      hook_for :template_engine do |instance, template_engine|
        instance.invoke template_engine, [instance.name]
      end

      private

      def parent_class
        return options[:parent] if options[:parent]

        ViewComponent::Base.config.component_parent_class || default_parent_class
      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

      def initialize_call_method_for_inline?
        options["inline"]
      end

      def default_parent_class
        defined?(ApplicationComponent) ? ApplicationComponent : ViewComponent::Base
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 2 rubygems

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