Skip to content

Commit

Permalink
use each
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Sep 21, 2023
1 parent 9636d34 commit 6945782
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/rubocop/cop/rails/destroy_all_bang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RuboCop
module Cop
module Rails
# Cop to enforce the use of `find_each(&:destroy!)` over `destroy_all`.
# Cop to enforce the use of `each(&:destroy!)` over `destroy_all`.
# https://docs.rubocop.org/rubocop-rails/cops_rails.html#railssavebang
# https://github.com/rails/rails/pull/37782
# https://discuss.rubyonrails.org/t/proposal-add-destroy-all-method-to-activerecord-relation/80959
Expand All @@ -12,16 +12,16 @@ module Rails
# User.destroy_all
#
# # good
# User.find_each(&:destroy!)
# User.each(&:destroy!)
class DestroyAllBang < Base
extend AutoCorrector
MSG = 'Use `find_each(&:destroy!)` instead of `destroy_all`.'
MSG = 'Use `each(&:destroy!)` instead of `destroy_all`.'
RESTRICT_ON_SEND = %i[destroy_all].freeze

def on_send(node)
destroy_all_range = node.loc.selector
add_offense(destroy_all_range) do |corrector|
corrector.replace(node.loc.selector, 'find_each(&:destroy!)')
corrector.replace(node.loc.selector, 'each(&:destroy!)')
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/rubocop/cop/rails/destroy_all_bang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@
it 'registers an offense when using `destroy_all`' do
expect_offense(<<~RUBY)
User.destroy_all
^^^^^^^^^^^ Use `find_each(&:destroy!)` instead of `destroy_all`.
^^^^^^^^^^^ Use `each(&:destroy!)` instead of `destroy_all`.
RUBY

expect_correction(<<~RUBY)
User.find_each(&:destroy!)
User.each(&:destroy!)
RUBY

expect_offense(<<~RUBY)
User.where(desactivated: true).destroy_all
^^^^^^^^^^^ Use `find_each(&:destroy!)` instead of `destroy_all`.
^^^^^^^^^^^ Use `each(&:destroy!)` instead of `destroy_all`.
RUBY

expect_correction(<<~RUBY)
User.where(desactivated: true).find_each(&:destroy!)
User.where(desactivated: true).each(&:destroy!)
RUBY

expect_offense(<<~RUBY)
User
.where(desactivated: true)
.destroy_all
^^^^^^^^^^^ Use `find_each(&:destroy!)` instead of `destroy_all`.
^^^^^^^^^^^ Use `each(&:destroy!)` instead of `destroy_all`.
RUBY

expect_correction(<<~RUBY)
User
.where(desactivated: true)
.find_each(&:destroy!)
.each(&:destroy!)
RUBY
end

it 'does not register an offense when using `#each(&:destroy!)`' do
expect_no_offenses(<<~RUBY)
User.find_each(&:destroy!)
User.each(&:destroy!)
RUBY

expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 6945782

Please sign in to comment.