123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Generators::MigrationGenerator

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveRecord::Generators::Base
Defined in: activerecord/lib/rails/generators/active_record/migration/migration_generator.rb

Class Attribute Summary

Class Method Summary

Base - Inherited

.base_root

Set the current directory as base for the inherited generators.

::Rails::Generators::NamedBase - Inherited

.check_class_collision

Add a class collisions name to be checked on class initialization.

.new

::Rails::Generators::Base - Inherited

.base_root

Returns the base root for a common set of generators.

.default_source_root

Returns the default source root for a given generator.

.desc

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

.hide!

Convenience method to hide this generator from the available ones when running rails generator command.

.hook_for

Invoke a generator based on the value supplied by the user to the given option named “name”.

.namespace

Convenience method to get the namespace from the class name.

.remove_hook_for

Remove a previously added hook.

.source_root

Returns the source root for this generator using default_source_root as default.

.add_shebang_option!

Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.

.banner

Use Rails default banner.

.base_name

Sets the base_name taking into account the current class namespace.

.default_aliases_for_option

Returns default aliases for the option name given doing a lookup in Rails::Generators.aliases.

.default_for_option

Returns default for the option name given doing a lookup in config.

.default_generator_root,
.default_value_for_option

Returns the default value for the option name given doing a lookup in Rails::Generators.options.

.generator_name

Removes the namespaces and get the generator name.

.usage_path,
.class_option
.inherited

Cache source root and add lib/generators/base/generator/templates to source paths.

.hooks

Keep hooks configuration that are used on prepare_for_invocation.

.prepare_for_invocation

Prepare class invocation to search on ::Rails namespace if a previous added hook is being used.

Instance Attribute Summary

Instance Method Summary

Migration - Included

::Rails::Generators::Migration - Included

#create_migration,
#migration_template

Creates a migration template at the given destination.

#set_migration_assigns!

::Rails::Generators::NamedBase - Inherited

#application_name

Tries to retrieve the application name or simply return application.

#assign_names!, #attributes_names, #class_name, #class_path, #edit_helper, #file_path, #fixture_file_name, #human_name, #i18n_scope, #index_helper, #model_resource_name, #namespaced_class_path, #new_helper,
#parse_attributes!

Convert attributes array into GeneratedAttribute objects.

#plural_file_name, #plural_name, #plural_route_name, #plural_table_name, #redirect_resource_name, #regular_class_path, #route_url, #show_helper,
#singular_name

FIXME: We are avoiding to use alias because a bug on thor that make this method public and add it to the task list.

#singular_route_name, #singular_table_name, #table_name, #url_helper_prefix

::Rails::Generators::Base - Inherited

#class_collisions

Check whether the given class names are already taken by user application or Ruby on ::Rails.

#extract_last_module

Takes in an array of nested modules and extracts the last module.

#indent,
#module_namespacing

Wrap block with namespace of current application if namespace exists and is not skipped.

#namespace, #namespace_dirs, #namespaced_path, #wrap_with_namespace

::Rails::Generators::Actions - Included

#add_source

Add the given source to Gemfile

#application
#environment

Adds configuration code to a Rails runtime environment.

#gem

Adds a gem declaration to the Gemfile for the specified gem.

#gem_group

Wraps gem entries inside a group.

#generate

Runs another generator.

#git

Runs one or more git commands.

#github,
#initializer

Creates an initializer file in config/initializers/.

#lib

Creates a file in lib/.

#rails_command

Runs the specified Rails command.

#rake

Runs the specified Rake task.

#rakefile

Creates a Rake tasks file in lib/tasks/.

#readme

Reads the given file at the source root and prints it in the console.

#route

Make an entry in Rails routing file config/routes.rb.

#vendor

Creates a file in vendor/.

#append_file_with_newline

Append string to a file with a newline if necessary.

#execute_command

Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided.

#indentation

Indent the Gemfile to the depth of @indentation.

#log

Define log for backwards compatibility.

#match_file,
#optimize_indentation

Returns optimized string with indentation.

#quote

Always returns value in double quotes.

#rebase_indentation
#route_namespace_pattern,
#with_indentation

Manage Gemfile indentation for a DSL action block.

#initialize

Constructor Details

This class inherits a constructor from Rails::Generators::NamedBase

Instance Attribute Details

#join_tables (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 21

attr_reader :migration_action, :join_tables

#migration_action (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 21

attr_reader :migration_action, :join_tables

Instance Method Details

#attributes_with_index (private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 59

def attributes_with_index
  attributes.select { |a| !a.reference? && a.has_index? }
end

#create_migration_file

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 14

def create_migration_file
  set_local_assigns!
  validate_file_name!
  migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
end

#index_name_for(attribute) (private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 51

def index_name_for(attribute)
  if attribute.foreign_key?
    attribute.name
  else
    attribute.name.singularize.foreign_key
  end.to_sym
end

#normalize_table_name(_table_name) (private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 71

def normalize_table_name(_table_name)
  pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
end

#set_index_names (private)

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 45

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

#set_local_assigns! (private)

Sets the default migration template that is being used for the generation of the migration. Depending on command line arguments, the migration template and the table name instance variables are set up.

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 26

def set_local_assigns!
  @migration_template = "migration.rb"
  case file_name
  when /^(add)_.*_to_(.*)/, /^(remove)_.*?_from_(.*)/
    @migration_action = $1
    @table_name       = normalize_table_name($2)
  when /join_table/
    if attributes.length == 2
      @migration_action = "join"
      @join_tables      = pluralize_table_names? ? attributes.map(&:plural_name) : attributes.map(&:singular_name)

      set_index_names
    end
  when /^create_(.+)/
    @table_name = normalize_table_name($1)
    @migration_template = "create_table_migration.rb"
  end
end

#validate_file_name! (private)

A migration file name can only contain underscores (_), lowercase characters, and numbers 0-9. Any other file name will raise an ::ActiveRecord::IllegalMigrationNameError.

[ GitHub ]

  
# File 'activerecord/lib/rails/generators/active_record/migration/migration_generator.rb', line 65

def validate_file_name!
  unless /^[_a-z0-9]+$/.match?(file_name)
    raise IllegalMigrationNameError.new(file_name)
  end
end