Class: EcsRails::Generators::RelationshipGenerator

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

Overview

rails g ecs_rails:relationship OWNER name:Target

rails g ecs_rails:relationship Post author:User

Implements RFC-0012 / ADR-0013. Unlike the component generator, this emits ONLY a migration — relates_to defines the backing component dynamically, so there is no model file to write. It prints a reminder to add the relates_to line to the entity.

The migration creates the owner-scoped backing table post_authors: - uuid PK, - entity_id: not-null, UNIQUE index, ON DELETE CASCADE FK to entities — the owner side (a post has at most one author; destroying the post destroys the link), - author_id: indexed, ON DELETE NULLIFY FK to entities — the target side (destroying the target nullifies, does not cascade to the owner), - timestamps.

The single name:Target argument does not fit Rails’ GeneratedAttribute (there is no :Target column type), so it is parsed by hand — see #parse_relationship!.

Instance Method Summary collapse

Instance Method Details

#create_migration_file

This method returns an undefined value.

Emits the backing relationship table migration.

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



51
52
53
54
55
56
57
# File 'lib/generators/ecs_rails/relationship/relationship_generator.rb', line 51

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

RFC-0012: no component file. The DSL defines the backing component; the developer only has to declare the relationship on the entity.



61
62
63
64
65
# File 'lib/generators/ecs_rails/relationship/relationship_generator.rb', line 61

def print_relates_to_reminder
  entity_path = File.join(EcsRails.config.entities_path, class_path, "#{file_name}.rb")
  say "Add the relationship to #{entity_path}:", :green
  say "    relates_to :#{relation_name}, #{target_class_name}"
end