Module: Rails::Generators::Migration

Defined in:
lib/rails/generators/migration.rb

Overview

Holds common methods for migrations. It assumes that migrations has the [0-9]*_name format and can be used by another frameworks (like Sequel) just by implementing the next migration version method.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#migration_class_nameObject (readonly)

Returns the value of attribute migration_class_name.



8
9
10
# File 'lib/rails/generators/migration.rb', line 8

def migration_class_name
  @migration_class_name
end

#migration_file_nameObject (readonly)

Returns the value of attribute migration_file_name.



8
9
10
# File 'lib/rails/generators/migration.rb', line 8

def migration_file_name
  @migration_file_name
end

#migration_numberObject (readonly)

Returns the value of attribute migration_number.



8
9
10
# File 'lib/rails/generators/migration.rb', line 8

def migration_number
  @migration_number
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



10
11
12
# File 'lib/rails/generators/migration.rb', line 10

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#migration_template(source, destination = nil, config = {}) ⇒ Object

Creates a migration template at the given destination. The difference to the default template method is that the migration version is appended to the destination file name.

The migration version, migration file name, migration class name are available as instance variables in the template to be rendered.

Examples

migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails/generators/migration.rb', line 45

def migration_template(source, destination=nil, config={})
  destination = File.expand_path(destination || source, self.destination_root)

  migration_dir = File.dirname(destination)
  @migration_number     = self.class.next_migration_number(migration_dir)
  @migration_file_name  = File.basename(destination).sub(/\.rb$/, '')
  @migration_class_name = @migration_file_name.camelize

  destination = self.class.migration_exists?(migration_dir, @migration_file_name)

  if behavior == :invoke
    raise Error, "Another migration is already named #{@migration_file_name}: #{destination}" if destination
    destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb")
  end

  template(source, destination, config)
end