Class: RuboCop::Cop::Migration::AlterAfterCreateTable

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/migration/alter_after_create_table.rb

Overview

Prefer defining table changes inside the create_table block instead of altering the table immediately after creating it.

Examples:

# bad
create_table :users do |t|
  t.string :email
end

add_index :users, :email, unique: true

# good
create_table :users do |t|
  t.string :email
  t.index :email, unique: true
end

Constant Summary collapse

MSG =
'Prefer defining new table changes inside the create_table block.'
ALTER_METHODS =
(
  Rails::BulkChangeTable::COMBINABLE_ALTER_METHODS +
  Rails::BulkChangeTable::MYSQL_COMBINABLE_ALTER_METHODS +
  Rails::BulkChangeTable::POSTGRESQL_COMBINABLE_ALTER_METHODS +
  Rails::BulkChangeTable::POSTGRESQL_COMBINABLE_ALTER_METHODS_SINCE_6_1
).uniq.freeze
RESTRICT_ON_SEND =
ALTER_METHODS.freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rubocop/cop/migration/alter_after_create_table.rb', line 37

def on_block(node)
  return unless node.send_node&.command?(:create_table)
  return unless (alter_node = alter_node_after_create_table(node))
  return unless same_table?(node.send_node, alter_node)

  add_alter_offense(node, alter_node)
end