Class: RuboCop::Cop::Captive::Rails::MigrationMethods

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/captive/rails/migration_methods.rb

Overview

This cop ensures the use ‘change` method when is possible.

Il est possible d’utiliser la méthode ‘change` lors de l’utilisation de méthode de ActiveRecord::Migration tel que : ‘remove_column`, `add_column`, `add_index`, … La méthode gère automatiquement le UP et le DOWN. Il n’est donc pas nécéssaire définir le ‘def up` et `def down` seulement lors d’une migration de donnée

Examples:

# bad
def up
  remove_column :table, :column, :type
end

def down
  add_column :table, :column, :type
end

# good
def change
  remove_column :table, :column, :type
end

See Also:

Constant Summary collapse

MSG =
"Avoid using ActiveRecord::Migration methods in `up` and `down` methods. \
Use `change` instead."
BLACKLISTED_METHODS =
%i(
  add_column
  add_foreign_key
  add_index
  add_reference
  add_timestamps
  change_column
  change_table
  create_table
  create_join_table
  drop_table
  remove_column
  remove_foreign_key
  remove_index
  remove_reference
  remove_timestamps
  rename_column
  rename_index
  rename_table
).freeze

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object



59
60
61
62
63
64
# File 'lib/rubocop/cop/captive/rails/migration_methods.rb', line 59

def on_def(node)
  return unless %i(up down).include?(node.method_name)
  return unless migration_method?(node)

  add_offense(node, message: MSG)
end