Class: UniversalTrackManager::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/universal_track_manager/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object



22
23
24
# File 'lib/generators/universal_track_manager/install_generator.rb', line 22

def self.next_migration_number(path)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Instance Method Details

#create_universal_track_manager_initializerObject



69
70
71
72
73
74
75
# File 'lib/generators/universal_track_manager/install_generator.rb', line 69

def create_universal_track_manager_initializer
  column_config = "config.campaign_columns = '#{options.param_list}'"
  copy_file "universal_track_manager.rb", "#{self.class.source_root}/universal_track_manager.rb-staged"
  gsub_file "#{self.class.source_root}/universal_track_manager.rb-staged", "#GENERATOR INSERTS CAMPAIGN COLUMN CONFIG HERE", column_config
  copy_file 'universal_track_manager.rb-staged', 'config/initializers/universal_track_manager.rb'
  remove_file "#{self.class.source_root}/universal_track_manager.rb-staged"
end

#create_universal_track_manager_migrationObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/universal_track_manager/install_generator.rb', line 27

def create_universal_track_manager_migration
  # guard against pre-0.7.3 sytnax
  if options['param_list']
    puts "Oops (FATAL): param_list is removed; use 'add' to augment the default list of fields or use 'only' to replace it"
    exit
  end

  # guard against using both 'add' and 'only'
  if options['add'] && options['only']
    puts "Oops (FATAL): You specified both 'add' and 'only'; use 'add' to augment the default list of fields OR use 'only' to replace it"
    exit
  end

  @default_params = %w{utm_source utm_medium utm_campaign utm_content utm_term}

  if options['add']
    options['add'].split(",").each  do |p|
      if !@default_params.include?(p)
        @default_params << p
      end
    end
  end


  if options['only']
    @default_params = []
    options['only'].split(",").each  do |p|
      @default_params << p
    end
  end

  column_defs = ""
  @default_params.each  do |p|
    column_defs += "          t.string :#{p}, limit:256\n"
  end
  copy_file "create_universal_track_manager_tables.rb", "#{self.class.source_root}/create_universal_track_manager_tables.rb-staged"
  gsub_file "#{self.class.source_root}/create_universal_track_manager_tables.rb-staged", "#GENERATOR INSERTS CAMPAIGN COLUMNS HERE", column_defs
  migration_template "create_universal_track_manager_tables.rb-staged",  "db/migrate/create_universal_track_manager_tables.rb"

  remove_file "#{self.class.source_root}/create_universal_track_manager_tables.rb-staged"
end

#migration_versionObject



77
78
79
# File 'lib/generators/universal_track_manager/install_generator.rb', line 77

def migration_version
  "[#{ActiveRecord::Migration.current_version}]" if ActiveRecord::Migration.respond_to?(:current_version)
end