123456789_123456789_123456789_123456789_123456789_

Module: Rails::Generators::Migration

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
ActiveRecord::Generators::Base, ::ActiveRecord::Generators::Migration, ActiveRecord::Generators::MigrationGenerator, ActiveRecord::Generators::ModelGenerator
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
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 version method.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#migration_class_name (readonly)

[ GitHub ]

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

attr_reader :migration_number, :migration_file_name, :migration_class_name

#migration_file_name (readonly)

[ GitHub ]

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

attr_reader :migration_number, :migration_file_name, :migration_class_name

#migration_number (readonly)

[ GitHub ]

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

attr_reader :migration_number, :migration_file_name, :migration_class_name

Instance Method Details

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

[ GitHub ]

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

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 = {})

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.

migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
[ GitHub ]

  
# File 'railties/lib/rails/generators/migration.rb', line 56

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

  set_migration_assigns!(destination)
  context = instance_eval("binding")

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

  create_migration numbered_destination, nil, config do
    match = ERB.version.match(/\Aerb\.rb \[(?<version>[^ ]+) /)
    if match && match[:version] >= "2.2.0" # Ruby 2.6+
      ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(context)
    else
      ERB.new(::File.binread(source), nil, "-", "@output_buffer").result(context)
    end
  end
end

#set_migration_assigns!(destination)

[ GitHub ]

  
# File 'railties/lib/rails/generators/migration.rb', line 39

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