Class: RuboCop::Cop::Rails::FindEach

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/rails/find_each.rb

Overview

This cop is used to identify usages of ‘all.each` and change them to use `all.find_each` instead.

Examples:

# bad
User.all.each

# good
User.all.find_each

Constant Summary collapse

MSG =
'Use `find_each` instead of `each`.'
SCOPE_METHODS =
%i[
  all eager_load includes joins left_joins left_outer_joins not preload
  references unscoped where
].freeze
IGNORED_METHODS =
%i[order limit select].freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



34
35
36
# File 'lib/rubocop/cop/rails/find_each.rb', line 34

def autocorrect(node)
  ->(corrector) { corrector.replace(node.loc.selector, 'find_each') }
end

#on_send(node) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/rails/find_each.rb', line 24

def on_send(node)
  return unless node.receiver&.send_type? &&
                node.method?(:each)

  return unless SCOPE_METHODS.include?(node.receiver.method_name)
  return if method_chain(node).any? { |m| ignored_by_find_each?(m) }

  add_offense(node, location: :selector)
end