class RuboCop::Cop::Rails::FindEach

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

@example

# bad
User.all.each

# good
User.all.find_each

@example IgnoredMethods: ['order']

# good
User.order(:foo).each

Constants

MSG
RESTRICT_ON_SEND
SCOPE_METHODS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 31
def on_send(node)
  return unless node.receiver&.send_type?
  return unless SCOPE_METHODS.include?(node.receiver.method_name)
  return if node.receiver.receiver.nil? && !inherit_active_record_base?(node)
  return if ignored?(node)

  range = node.loc.selector
  add_offense(range) do |corrector|
    corrector.replace(range, 'find_each')
  end
end

Private Instance Methods

ignored?(node) click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 45
def ignored?(node)
  method_chain = node.each_node(:send).map(&:method_name)
  (cop_config['IgnoredMethods'].map(&:to_sym) & method_chain).any?
end