Sha256: decc7e5166cbf15f7a69f325cd953ec8d95276b825eed3f0d9f447efb1ff8f36

Contents?: true

Size: 1.08 KB

Versions: 35

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Rails
  module Generators
    class ComponentGenerator < Rails::Generators::NamedBase
      source_root File.expand_path("templates", __dir__)

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

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

      hook_for :test_framework

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

      private

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

      def parent_class
        defined?(ApplicationComponent) ? "ApplicationComponent" : "ViewComponent::Base"
      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
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
view_component-2.24.0 lib/rails/generators/component/component_generator.rb
view_component-2.23.2 lib/rails/generators/component/component_generator.rb
view_component-2.23.1 lib/rails/generators/component/component_generator.rb
view_component-2.23.0 lib/rails/generators/component/component_generator.rb
view_component-2.22.1 lib/rails/generators/component/component_generator.rb
view_component-2.22.0 lib/rails/generators/component/component_generator.rb
view_component-2.21.0 lib/rails/generators/component/component_generator.rb
view_component-2.20.0 lib/rails/generators/component/component_generator.rb
view_component-2.19.1 lib/rails/generators/component/component_generator.rb
view_component-2.19.0 lib/rails/generators/component/component_generator.rb
view_component-2.18.2 lib/rails/generators/component/component_generator.rb
view_component-2.18.1 lib/rails/generators/component/component_generator.rb
view_component-2.18.0 lib/rails/generators/component/component_generator.rb
view_component-2.17.1 lib/rails/generators/component/component_generator.rb
view_component-2.17.0 lib/rails/generators/component/component_generator.rb
view_component-2.16.0 lib/rails/generators/component/component_generator.rb
view_component-2.15.0 lib/rails/generators/component/component_generator.rb
view_component-2.14.1 lib/rails/generators/component/component_generator.rb
view_component-2.14.0 lib/rails/generators/component/component_generator.rb
view_component-2.13.0 lib/rails/generators/component/component_generator.rb