summaryrefslogtreecommitdiff
path: root/rubocop/cop/destroy_all.rb
blob: 38b6cb40f91fc1e7f68e67cf670089e5721ab2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

module RuboCop
  module Cop
    # Cop that blacklists the use of `destroy_all`.
    class DestroyAll < RuboCop::Cop::Cop
      MSG = 'Use `delete_all` instead of `destroy_all`. ' \
        '`destroy_all` will load the rows into memory, then execute a ' \
        '`DELETE` for every individual row.'

      def_node_matcher :destroy_all?, <<~PATTERN
        (send {send ivar lvar const} :destroy_all ...)
      PATTERN

      def on_send(node)
        return unless destroy_all?(node)

        add_offense(node, location: :expression)
      end
    end
  end
end