Class: Popularable::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/popularable/install_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
31
32
33
34
35
36
37
38
# File 'lib/generators/popularable/install_generator.rb', line 8

def create_migration_file
  destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_popularable_installation_migration.rb".gsub(" ", "")
  migration_name = "PopularableInstallationMigration".gsub(" ", "")
  count = nil
  i = 1
  while !Dir.glob("db/migrate/*_popularable_installation_migration.rb".gsub(" ", "")).empty?
    i += 1
    count = "_#{i}"
    destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_popularable_installation_migration.rb".gsub(" ", "")
    migration_name = "PopularableInstallationMigration#{i}".gsub(" ", "")
  end
  create_file destination, <<-FILE
class #{migration_name} < ActiveRecord::Migration
  def change
create_table :popularable_popularity_events do |t|
  t.string  :popularable_type
  t.integer :popularable_id

  t.date :popularity_event_date
  t.integer :popularity, default: 0

  t.timestamps null: false
end

add_index :popularable_popularity_events, [:popularable_type, :popularable_id], name: "index_popularable_popularity_events_type_id"
add_index :popularable_popularity_events, [:popularity_event_date, :popularable_type, :popularable_id, :popularity], name: "index_popularable_popularity_events_type_id_popularity"
add_index :popularable_popularity_events, :popularity
  end
end
FILE
end