class Napa::CLI::Migration

Attributes

join_tables[R]
migration_action[R]
table_name[R]

Public Instance Methods

migration() click to toggle source
# 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_filename() click to toggle source
# File lib/napa/cli/migration.rb, line 20
def migration_filename
  "#{version}_#{migration_name.underscore}"
end
output_directory() click to toggle source
# File lib/napa/cli/migration.rb, line 24
def output_directory
  './db/migrate'
end
parse_attributes!() click to toggle source
# 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!() click to toggle source
# 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
version() click to toggle source
# File lib/napa/cli/migration.rb, line 16
def version
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Private Instance Methods

attributes_with_index() click to toggle source
# File lib/napa/cli/migration.rb, line 62
def attributes_with_index
  attributes.select { |a| !a.reference? && a.has_index? }
end
index_name_for(attribute) click to toggle source
# File lib/napa/cli/migration.rb, line 72
def index_name_for(attribute)
  if attribute.foreign_key?
    attribute.name
  else
    attribute.name.singularize.foreign_key
  end.to_sym
end
set_index_names() click to toggle source
# File lib/napa/cli/migration.rb, line 66
def set_index_names
  attributes.each_with_index do |attr, i|
    attr.index_name = [attr, attributes[i - 1]].map{ |a| index_name_for(a) }
  end
end