Sha256: fb1dee77a561a68ca80dabf51371b11afce56cd9de338403b0a55ec9d025ecef

Contents?: true

Size: 835 Bytes

Versions: 3

Compression:

Stored size: 835 Bytes

Contents

# frozen_string_literal: true

module Rubocop
  module Cop
    # Cop that prevents the use of `dependent: ...` in ActiveRecord models.
    class ActiveRecordDependent < RuboCop::Cop::Base
      MSG = 'Do not use `dependent:` to remove associated data, ' \
            'use foreign keys with cascading deletes instead.'

      METHOD_NAMES = [:has_many, :has_one, :belongs_to].freeze
      ALLOWED_OPTIONS = [:restrict_with_error].freeze

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

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

          break if ALLOWED_OPTIONS.include?(option_name)

          add_offense(pair) if key_name == :dependent
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitlab-styles-11.0.0 lib/rubocop/cop/active_record_dependent.rb
gitlab-styles-10.1.0 lib/rubocop/cop/active_record_dependent.rb
gitlab-styles-10.0.0 lib/rubocop/cop/active_record_dependent.rb