From 69457822a5c4c01bcd4e3f9fc169af6326cea175 Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Thu, 21 Sep 2023 08:44:34 -0400 Subject: [PATCH] use each --- lib/rubocop/cop/rails/destroy_all_bang.rb | 8 ++++---- spec/rubocop/cop/rails/destroy_all_bang_spec.rb | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/rubocop/cop/rails/destroy_all_bang.rb b/lib/rubocop/cop/rails/destroy_all_bang.rb index c4f8d5c..2a53c61 100644 --- a/lib/rubocop/cop/rails/destroy_all_bang.rb +++ b/lib/rubocop/cop/rails/destroy_all_bang.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/rails/destroy_all_bang_spec.rb b/spec/rubocop/cop/rails/destroy_all_bang_spec.rb index 20f1f60..01940e4 100644 --- a/spec/rubocop/cop/rails/destroy_all_bang_spec.rb +++ b/spec/rubocop/cop/rails/destroy_all_bang_spec.rb @@ -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)