Commit 96ce2da7 authored by Yorick Peterse's avatar Yorick Peterse

Blacklist the use of "destroy_all"

This method usually has really bad performance implications, as it loads
rows into memory and deletes them one by one.
parent 353360ef
# 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
......@@ -27,3 +27,4 @@ require_relative 'cop/project_path_helper'
require_relative 'cop/rspec/env_assignment'
require_relative 'cop/rspec/factories_in_migration_specs'
require_relative 'cop/sidekiq_options_queue'
require_relative 'cop/destroy_all'
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/destroy_all'
describe RuboCop::Cop::DestroyAll do
include CopHelper
subject(:cop) { described_class.new }
it 'flags the use of destroy_all with a send receiver' do
inspect_source('foo.destroy_all')
expect(cop.offenses.size).to eq(1)
end
it 'flags the use of destroy_all with a constant receiver' do
inspect_source('User.destroy_all')
expect(cop.offenses.size).to eq(1)
end
it 'flags the use of destroy_all when passing arguments' do
inspect_source('User.destroy_all([])')
expect(cop.offenses.size).to eq(1)
end
it 'flags the use of destroy_all with a local variable receiver' do
inspect_source(<<~RUBY)
users = User.all
users.destroy_all
RUBY
expect(cop.offenses.size).to eq(1)
end
it 'does not flag the use of delete_all' do
inspect_source('foo.delete_all')
expect(cop.offenses).to be_empty
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment