Class: RuboCop::Cop::Migration::ForeignKeyOption

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/migration/foreign_key_option.rb

Overview

Specify the foreign key option to create the constraint.

# bad
add_reference(:products, :user)
add_reference(:products, :user, foreign_key: false)
add_belongs_to(:products, :user)
t.references(:user)

# good
add_reference(:products, :user, foreign_key: true)
add_reference(:products, :user, foreign_key: { to_table: users })
add_belongs_to(:products, :user, foreign_key: true)
t.references(:user, foreign_key: true)

Constant Summary collapse

MSG =
'Add `foreign_key: true` or `foreign_key: { to_table: :some_table }`'
ADDING_REFERENCES_METHODS =
Set.new(%i[add_reference add_belongs_to references belongs_to]).freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless adding_reference?(node)
  return if polymorphic_reference?(node)

  add_offense(node) unless foreign_key_option?(node)
end