Skip to content

Commit

Permalink
Added Rails/DestroyAllBang
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Sep 19, 2023
1 parent 268fb76 commit cdf2a57
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# main

* Added cop `Rails/DestroyAllBang` ([#X](https://github.com/petalmd/rubocop-petal/pull/X))
* Fix `Migration/ChangeTableReferences` offense location (Fix [#61](https://github.com/petalmd/rubocop-petal/issues/61)) ([#62](https://github.com/petalmd/rubocop-petal/pull/62))
* Added cop `Sidekiq/NoEarlyNilReturn` ([#58](https://github.com/petalmd/rubocop-petal/pull/58))
* Added cop `Rails/EnumStartingValue` ([#57](https://github.com/petalmd/rubocop-petal/pull/57))
Expand Down
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Grape/UnnecessaryNamespace:
Include:
- app/api/**/*

Rails/DestroyAllBang:
Description: 'Prevent using `destroy_all` in favor of `each(&:destroy!)` to go along Rails/SaveBang cop.'
StyleGuide: https://rails.rubystyle.guide#save-bang
Enabled: true

Rails/EnumPrefix:
Description: 'Set prefix options when using enums.'
Enabled: true
Expand Down
30 changes: 30 additions & 0 deletions lib/rubocop/cop/rails/destroy_all_bang.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Cop to enforce the use of `find_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
#
# # bad
# User.destroy_all
#
# # good
# User.find_each(&:destroy!)
class DestroyAllBang < Base
extend AutoCorrector
MSG = 'Use `find_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!)')
end
end
end
end
end
end
46 changes: 46 additions & 0 deletions spec/rubocop/cop/rails/destroy_all_bang_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::DestroyAllBang, :config do
it 'registers an offense when using `destroy_all`' do
expect_offense(<<~RUBY)
User.destroy_all
^^^^^^^^^^^ Use `find_each(&:destroy!)` instead of `destroy_all`.
RUBY

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

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

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

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

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

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

expect_no_offenses(<<~RUBY)
User.each(&:destroy!)
RUBY
end
end

0 comments on commit cdf2a57

Please sign in to comment.