Class: Gitlab::Styles::Rubocop::Cop::ActiveRecordDependent

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
ModelHelpers
Defined in:
lib/gitlab/styles/rubocop/cop/active_record_dependent.rb

Overview

Cop that prevents the use of ‘dependent: …` in ActiveRecord models.

Constant Summary collapse

MSG =
'Do not use `dependent: to remove associated data, ' \
'use foreign keys with cascading deletes instead'.freeze
METHOD_NAMES =
[:has_many, :has_one, :belongs_to].freeze

Instance Method Summary collapse

Methods included from ModelHelpers

#in_model?

Instance Method Details

#on_send(node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/styles/rubocop/cop/active_record_dependent.rb', line 16

def on_send(node)
  return unless in_model?(node)
  return unless METHOD_NAMES.include?(node.children[1])

  node.children.last.each_node(:pair) do |pair|
    key_name = pair.children[0].children[0]

    add_offense(pair, location: :expression) if key_name == :dependent
  end
end