Class: RuboCop::Cop::Migration::SchemaStatementsMethods

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

Overview

Use SchemaStatements methods already defined in a migration class. Using already defined method is sorter and will be log during migration.

# bad
ActiveRecord::Base.connection.table_exists? 'users'

# bad
ActiveRecord::Base.connection.column_exists? 'users', 'first_name'

# bad
ApplicationRecord.connection.execute('SELECT COUNT(*) FROM `users`')

# good
table_exists? 'users'

# good
column_exists? 'users', 'first_name'

# good
execute('SELECT COUNT(*) FROM `users`')

Constant Summary collapse

MSG =
'Use already defined methods. Remove `ActiveRecord::Base.connection`.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless use_connection(node)

  add_offense(node.children[0]) do |corrector|
    corrector.replace(node, node.source.gsub("#{node.children[0].source}.", ''))
  end
end