Class: Trendable::MigrationGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_migration_fileObject



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

def create_migration_file
  plural_name = file_name.pluralize
  model_name  = file_name

  destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_trendable_migration_for_#{model_name}.rb".gsub(" ", "")
  migration_name = "TrendableMigrationFor#{model_name.titlecase}".gsub(" ", "")
  count = nil
  i = 1
  while !Dir.glob("db/migrate/*_trendable_migration_for_#{model_name}#{count}.rb".gsub(" ", "")).empty?
    i += 1
    count = "_#{i}"
    destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_trendable_migration_for_#{model_name}#{count}.rb".gsub(" ", "")
    migration_name = "TrendableMigrationFor#{model_name.titlecase}#{i}".gsub(" ", "")
  end
  create_file destination, <<-FILE
class #{migration_name} < ActiveRecord::Migration
  def change
add_column :#{plural_name}, :trending_power, :integer, default: 1000
add_index  :#{plural_name}, :trending_power
  end
end
FILE
end