Class: Anaconda::MigrationGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/anaconda/migration_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_migration_fileObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/anaconda/migration_generator.rb', line 10

def create_migration_file
  destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_anaconda_migration_for_#{file_name}_#{field_name}.rb".gsub(" ", "")
  migration_name = "AnacondaMigrationFor#{file_name.titlecase}#{field_name.titlecase}".gsub(" ", "")
  count = nil
  i = 1
  while !Dir.glob("db/migrate/*_anaconda_migration_for_#{file_name}_#{field_name}#{count}.rb".gsub(" ", "")).empty?
    i += 1
    count = "_#{i}"
    destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_anaconda_migration_for_#{file_name}_#{field_name}#{count}.rb".gsub(" ", "")
    migration_name = "AnacondaMigrationFor#{file_name.titlecase}#{field_name.titlecase}#{i}".gsub(" ", "")
  end
  create_file destination, "class \#{migration_name} < ActiveRecord::Migration\n  def change\nadd_column :\#{plural_name}, :\#{field_name}_filename, :string\nadd_column :\#{plural_name}, :\#{field_name}_file_path, :text\nadd_column :\#{plural_name}, :\#{field_name}_size, :integer\nadd_column :\#{plural_name}, :\#{field_name}_original_filename, :text\nadd_column :\#{plural_name}, :\#{field_name}_stored_privately, :boolean\nadd_column :\#{plural_name}, :\#{field_name}_type, :string\n  end\nend\n"
end