Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Rails/DestroyAllBang #65

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`(Fix [#42](https://github.com/petalmd/rubocop-petal/issues/42)). ([#65](https://github.com/petalmd/rubocop-petal/pull/65))
* Added cop `Chewy/FieldType`. ([#64](https://github.com/petalmd/rubocop-petal/pull/64))
* 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))
Expand Down
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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 `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.each(&:destroy!)
class DestroyAllBang < Base
extend AutoCorrector
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, 'each(&:destroy!)')
end
end
end
end
end
end
42 changes: 42 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,42 @@
# 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 `each(&:destroy!)` instead of `destroy_all`.
RUBY

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

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

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

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

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

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