Module: Rails::Generators::Migration

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Generators::Migration
Defined in:
railties/lib/rails/generators/migration.rb

Overview

Holds common methods for migrations. It assumes that migrations have the [0-9]*_name format and can be used by other frameworks (like Sequel) just by implementing the next_migration_number method.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included, prepend_features, prepended

Instance Attribute Details

#migration_class_nameObject (readonly)

Returns the value of attribute migration_class_name.



12
13
14
# File 'railties/lib/rails/generators/migration.rb', line 12

def migration_class_name
  @migration_class_name
end

#migration_file_nameObject (readonly)

Returns the value of attribute migration_file_name.



12
13
14
# File 'railties/lib/rails/generators/migration.rb', line 12

def migration_file_name
  @migration_file_name
end

#migration_numberObject (readonly)

Returns the value of attribute migration_number.



12
13
14
# File 'railties/lib/rails/generators/migration.rb', line 12

def migration_number
  @migration_number
end

Instance Method Details

#create_migration(destination, data, config = {}, &block) ⇒ Object



34
35
36
# File 'railties/lib/rails/generators/migration.rb', line 34

def create_migration(destination, data, config = {}, &block)
  action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config)
end

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

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

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

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


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'railties/lib/rails/generators/migration.rb', line 55

def migration_template(source, destination, config = {})
  source = File.expand_path(find_in_source_paths(source.to_s))

  set_migration_assigns!(destination)

  dir, base = File.split(destination)
  numbered_destination = File.join(dir, ["%migration_number%", base].join("_"))

  file = create_migration numbered_destination, nil, config do
    ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(binding)
  end
  Rails::Generators.add_generated_file(file)
end

#set_migration_assigns!(destination) ⇒ Object



38
39
40
41
42
43
44
45
# File 'railties/lib/rails/generators/migration.rb', line 38

def set_migration_assigns!(destination)
  destination = File.expand_path(destination, destination_root)

  migration_dir = File.dirname(destination)
  @migration_number     = self.class.next_migration_number(migration_dir)
  @migration_file_name  = File.basename(destination, ".rb")
  @migration_class_name = @migration_file_name.camelize
end