Class: Napa::CLI::Migration

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/napa/cli/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#join_tablesObject (readonly)

Returns the value of attribute join_tables.



14
15
16
# File 'lib/napa/cli/migration.rb', line 14

def join_tables
  @join_tables
end

#migration_actionObject (readonly)

Returns the value of attribute migration_action.



14
15
16
# File 'lib/napa/cli/migration.rb', line 14

def migration_action
  @migration_action
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



14
15
16
# File 'lib/napa/cli/migration.rb', line 14

def table_name
  @table_name
end

Instance Method Details

#migrationObject



54
55
56
57
58
59
# File 'lib/napa/cli/migration.rb', line 54

def migration
  self.class.source_root "#{File.dirname(__FILE__)}/templates/"
  say 'Generating migration...'
  template @migration_template, "#{output_directory}/#{migration_filename}.rb"
  say 'Done!', :green
end

#migration_filenameObject



20
21
22
# File 'lib/napa/cli/migration.rb', line 20

def migration_filename
  "#{version}_#{migration_name.underscore}"
end

#output_directoryObject



24
25
26
# File 'lib/napa/cli/migration.rb', line 24

def output_directory
  './db/migrate'
end

#parse_attributes!Object



28
29
30
31
32
# File 'lib/napa/cli/migration.rb', line 28

def parse_attributes!
  self.attributes = (attributes || []).map do |attr|
    GeneratedAttribute.parse(attr)
  end
end

#set_local_assigns!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/napa/cli/migration.rb', line 34

def set_local_assigns!
  @migration_template = "migration/migration.rb.tt"
  filename = migration_name.underscore
  case filename
  when /^(add|remove)_.*_(?:to|from)_(.*)/
    @migration_action = $1
    @table_name       = $2.pluralize
  when /join_table/
    if attributes.length == 2
      @migration_action = 'join'
      @join_tables      = attributes.map(&:plural_name)

      set_index_names
    end
  when /^create_(.+)/
    @table_name = $1.pluralize
    @migration_template = "model/db/migrate/migration.rb.tt"
  end
end

#versionObject



16
17
18
# File 'lib/napa/cli/migration.rb', line 16

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