class RuboCop::Cop::Rails::FindEach

User.order(:foo).each
# good
@example AllowedPattern: [‘order’]
User.order(:foo).each
# good
@example AllowedMethods: [‘order’]
User.all.find_each
# good
User.all.each
# bad
@example
so the return values are different.
Also, ‘all.each` returns an `Array` instance and `all.find_each` returns nil,
This cop is unsafe if the receiver object is not an Active Record object.
@safety
Identifies usages of `all.each` and change them to use `all.find_each` instead.

def active_model_error?(node)

def active_model_error?(node)
  return false if node.nil?
  node.send_type? && node.method?(:errors)
end

def active_model_error_where?(node)

def active_model_error_where?(node)
  node.method?(:where) && active_model_error?(node.receiver)
end

def ignored?(node)

def ignored?(node)
  return true if active_model_error_where?(node.receiver)
  method_chain = node.each_node(:send).map(&:method_name)
  method_chain.any? { |method_name| allowed_method?(method_name) || matches_allowed_pattern?(method_name) }
end

def on_send(node)

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