Class: RuboCop::Cop::Carwow::AddColumnWithComment

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/carwow/add_column_with_comment.rb

Overview

Checks the migration to confirm that new columns have a comment.

Examples:

# bad
add_column :users, :name, :string

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

# good
add_column :users, :name, :string, comment: 'The name of the user'

# good
create_table :users do |t|
  t.string :name, comment: 'The name of the user'
  t.string :email, comment: 'The email of the user'
end

Constant Summary collapse

MSG =
"Missing 'comment' parameter for new column."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/carwow/add_column_with_comment.rb', line 40

def on_send(node)
  return unless add_column?(node) || inside_create_table_block?(node.parent.parent)

  # Timestamps and indexes do not need comments
  return if i[timestamps index].include?(node.method_name)

  # Extract arguments
  _receiver, _method_name, *args = *node

  # Check if there is a 'comment' argument
  comment_arg = find_comment_arg(args)

  return if comment_arg

  add_offense(node, location: :expression, message: MSG)
end