Sha256: 327ff4e2db68445ddad200b6ab81767fb6e300440c160889063a687adf76d02b

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

# frozen_string_literal: true

module Rubocop
  module Cop
    # Cop that prevents the use of `dependent: ...` in ActiveRecord models.
    # @example
    #   # bad
    #   belongs_to :foo, dependent: :destroy
    #
    #   # good
    #   belongs_to :foo # With database foreign key with cascading deletes
    class ActiveRecordDependent < RuboCop::Cop::Base
      MSG = 'Do not use `dependent:` to remove associated data, ' \
        'use foreign keys with cascading deletes instead.'

      RESTRICT_ON_SEND = %i[has_many has_one belongs_to].to_set.freeze
      ALLOWED_OPTIONS = %i[restrict_with_error].freeze

      # @!method dependent_use(node)
      def_node_matcher :dependent_use, <<~PATTERN
        (send _ %RESTRICT_ON_SEND ... (hash <$(pair (sym :dependent) (sym $_)) ...>))
      PATTERN

      def on_send(node)
        dependent_use(node) do |pair, value|
          add_offense(pair) unless ALLOWED_OPTIONS.include?(value)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/active_record_dependent.rb
gitlab-styles-13.0.0 lib/rubocop/cop/active_record_dependent.rb