Class: Shaf::Generator::Migration::RenameColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/migration/rename_column.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



22
23
24
# File 'lib/shaf/generator/migration/rename_column.rb', line 22

def compile_changes
  add_change rename_column_change
end

#compile_migration_nameObject



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

def compile_migration_name
  "rename_#{table_name}_#{from_col}_to_#{to_col}"
end

#from_colObject



26
27
28
# File 'lib/shaf/generator/migration/rename_column.rb', line 26

def from_col
  args[1] || ""
end

#rename_column_changeObject



34
35
36
37
38
39
40
# File 'lib/shaf/generator/migration/rename_column.rb', line 34

def rename_column_change
  [
    "alter_table(:#{table_name}) do",
    "  rename_column :#{from_col}, :#{to_col}",
    "end\n"
  ]
end

#to_colObject



30
31
32
# File 'lib/shaf/generator/migration/rename_column.rb', line 30

def to_col
  args[2] || ""
end

#validate_argsObject



9
10
11
12
13
14
15
16
# File 'lib/shaf/generator/migration/rename_column.rb', line 9

def validate_args
  if table_name.empty?
    raise "Please provide a table and at least " \
      "one column when generation add column migration"
  elsif from_col.empty? || to_col.empty?
    raise "Please provide the old column name and the new column name"
  end
end