Class: RuboCop::Cop::Rails::DestroyAllBang

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/destroy_all_bang.rb

Overview

Cop to enforce the use of ‘each(&:destroy!)` over `destroy_all`. docs.rubocop.org/rubocop-rails/cops_rails.html#railssavebang github.com/rails/rails/pull/37782 discuss.rubyonrails.org/t/proposal-add-destroy-all-method-to-activerecord-relation/80959

# bad
User.destroy_all

# good
User.each(&:destroy!)

Constant Summary collapse

MSG =
'Use `each(&:destroy!)` instead of `destroy_all`.'
RESTRICT_ON_SEND =
i[destroy_all].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



21
22
23
24
25
26
# File 'lib/rubocop/cop/rails/destroy_all_bang.rb', line 21

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