Class: Shaf::Generator::Migration::CreateTable

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/migration/create_table.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#add_change, #call, #column_def, identified_by, identifier, inherited, #initialize, #table_name, #target, usage

Constructor Details

This class inherits a constructor from Shaf::Generator::Migration::Base

Instance Method Details

#compile_changesObject



18
19
20
# File 'lib/shaf/generator/migration/create_table.rb', line 18

def compile_changes
  add_change create_table_change
end

#compile_migration_nameObject



14
15
16
# File 'lib/shaf/generator/migration/create_table.rb', line 14

def compile_migration_name
  "create_#{table_name}_table"
end

#create_table_changeObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/shaf/generator/migration/create_table.rb', line 22

def create_table_change
  cols = ["primary_key :id"]
  cols += ["DateTime :created_at", "DateTime :updated_at"] if add_timestamp_columns?
  cols += args[1..-1].map { |s| column_def(s) }
  [
    "create_table(:#{table_name}) do",
    *cols.map { |col| col.prepend("  ") }, # indent body with 2 spaces
    "end\n\n"
  ]
end

#validate_argsObject



9
10
11
12
# File 'lib/shaf/generator/migration/create_table.rb', line 9

def validate_args
  return unless table_name.empty?
  raise "Please provide a table name when generation a create table migration"
end