Class: EcsRails::Generators::ComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/ecs_rails/component/component_generator.rb

Overview

rails g ecs_rails:component NAME [field:type ...]

Implements RFC-0008. The whole point of this generator is that the entity_id + UNIQUE index + ON DELETE CASCADE invariant (ADR-0005) becomes impossible to forget, and that every attribute gets an explicit default (RFC-0006).

Attribute parsing is Rails’ own: declaring an attributes argument makes Rails::Generators::NamedBase run parse_attributes!, which turns “address:string” into a Rails::Generators::GeneratedAttribute.

Instance Method Summary collapse

Instance Method Details

#create_migration_file

This method returns an undefined value.

Emits the component’s table migration.

A Thor task: invoked as a generator step, not called directly.



48
49
50
51
52
53
# File 'lib/generators/ecs_rails/component/component_generator.rb', line 48

def create_migration_file
  migration_template(
    "migration.rb.tt",
    File.join(db_migrate_path, "create_#{table_name}.rb")
  )
end

#create_model_fileObject

ADR-0010: the model lands under the configured components_path (entities_path/components), not app/models. class_path is preserved so a namespaced component (rails g ecs_rails:component Billing/Plan) still nests correctly.



59
60
61
62
# File 'lib/generators/ecs_rails/component/component_generator.rb', line 59

def create_model_file
  template "model.rb.tt",
           File.join(EcsRails.config.components_path, class_path, "#{file_name}.rb")
end

#create_spec_fileObject

ADR-0010: the spec mirrors the layout under spec/entities/components. Its template declares type: :model explicitly, because rspec-rails only infers that from spec/models/ — which this path no longer matches.



67
68
69
70
# File 'lib/generators/ecs_rails/component/component_generator.rb', line 67

def create_spec_file
  template "component_spec.rb.tt",
           File.join("spec/entities/components", class_path, "#{file_name}_spec.rb")
end