class Gris::Generators::MigrationGenerator

Attributes

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

Public Instance Methods

migration() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 53
def migration
  self.class.source_root "#{File.dirname(__FILE__)}/templates/#{@migration_template}"
  say 'Generating migration...'
  directory '.', output_directory
  say 'Done!', :green
end
migration_filename() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 19
def migration_filename
  "#{version}_#{migration_name.underscore}"
end
output_directory() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 23
def output_directory
  './db/migrate'
end
parse_attributes!() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 27
def parse_attributes!
  self.attributes = (attributes || []).map do |attr|
    GeneratedAttribute.parse(attr)
  end
end
set_local_assigns!() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 33
def set_local_assigns!
  @migration_template = 'migration'
  filename = migration_name.underscore
  case filename
  when /^(add|remove)_.*_(?:to|from)_(.*)/
    @migration_action = Regexp.last_match[1]
    @table_name       = Regexp.last_match[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 = Regexp.last_match[1].pluralize
    @migration_template = 'create_table_migration'
  end
end
version() click to toggle source
# File lib/gris/generators/migration_generator.rb, line 15
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/gris/generators/migration_generator.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/gris/generators/migration_generator.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/gris/generators/migration_generator.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