Class: DraftMigrationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/draft_migration_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_draft_migration_fileObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/draft_migration_generator.rb', line 4

def create_draft_migration_file
  table = options['table'].downcase
  id_type = table.capitalize.constantize.columns_hash['id'].type
  id_unsigned = table.capitalize.constantize.columns_hash['id'].sql_type.include?('unsigned')

  filename = "db/migrate/#{Time.current.strftime("%Y%m%d%H%M%S")}_add_draft_to_#{table}.rb"
  table_name = table.pluralize
  create_file(filename, "class AddDraftTo\#{table_name.capitalize} < ActiveRecord::Migration[7.0]\ndef change\n  add_column :\#{table_name}, :draft_status, :string, null: true, default: DraftStatus::DRAFT\n  add_column :\#{table_name}, :draft_source_id, :\#{id_type}, null: true\#{id_unsigned ? ', unsigned: true' : nil }\n  add_foreign_key :\#{table_name}, :\#{table_name}, column: :draft_source_id, name: :fk_\#{table_name}_draft_source_id, on_delete: :cascade, on_update: :cascade\nend\nend\n  FILE\n  )\nend\n"