Class: ScaffoldPlus::Generators::MigrationGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/scaffold_plus/migration/migration_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_migrationObject



65
66
67
68
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 65

def add_migration
  return unless @the_lines.any?
  migration_template "change_migration.rb", "db/migrate/#{migration_name}.rb"
end

#prepare_change_columnObject



42
43
44
45
46
47
48
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 42

def prepare_change_column
  return unless options.change.present?
  options.change.each do |column|
    column, new_type = column.split(':')
    @the_lines << "    change_column :#{table_name}, :#{column}, :#{new_type}"
  end
end

#prepare_change_tableObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 25

def prepare_change_table
  return unless options.remove.present? or options.rename.present?
  @the_lines << "    change_table :#{table_name} do |t|"
  if options.remove.present?
    options.remove.each do |column|
      @the_lines << "      t.remove :#{column}"
    end
  end
  if options.rename.present?
    options.rename.each do |column|
      old_name, new_name = column.split(':')
      @the_lines << "      t.rename :#{old_name}, :#{new_name}"
    end
  end
  @the_lines << "    end"
end

#prepare_not_nullObject



50
51
52
53
54
55
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 50

def prepare_not_null
  return unless options.not_null.present?
  options.not_null.each do |column|
    @the_lines << "    change_column_null :#{table_name}, :#{column}, false"
  end
end

#prepare_set_defaultObject



57
58
59
60
61
62
63
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 57

def prepare_set_default
  return unless options.set_default.present?
  options.set_default.each do |column|
    column, preset = column.split(':')
    @the_lines << "    change_column_default :#{table_name}, :#{column}, #{preset}"
  end
end

#prepare_the_linesObject



21
22
23
# File 'lib/generators/scaffold_plus/migration/migration_generator.rb', line 21

def prepare_the_lines
  @the_lines = []
end