Class: RuboCop::Cop::Rails::HasManyOrHasOneDependent

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

Overview

This cop looks for ‘has_many` or `has_one` associations that don’t specify a ‘:dependent` option. It doesn’t register an offense if ‘:through` option was specified.

Examples:

# bad
class User < ActiveRecord::Base
  has_many :comments
  has_one :avatar
end

# good
class User < ActiveRecord::Base
  has_many :comments, dependent: :restrict_with_exception
  has_one :avatar, dependent: :destroy
  has_many :patients, through: :appointments
end

Constant Summary collapse

MSG =
'Specify a `:dependent` option.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb', line 53

def on_send(node)
  return if active_resource?(node.parent)

  unless association_without_options?(node)
    return if valid_options?(association_with_options?(node))
  end

  return if valid_options_in_with_options_block?(node)

  add_offense(node, location: :selector)
end