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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
AllowedMethods, AllowedPattern, ActiveRecordHelper
Defined in:
lib/rubocop/cop/rails/find_each.rb

Overview

Identifies usages of ‘all.each` and change them to use `all.find_each` instead.

Examples:

# bad
User.all.each

# good
User.all.find_each

AllowedMethods: [‘order’]

# good
User.order(:foo).each

AllowedPattern: [‘order’]

# good
User.order(:foo).each

Constant Summary collapse

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

Constants included from ActiveRecordHelper

ActiveRecordHelper::WHERE_METHODS

Instance Method Summary collapse

Methods included from ActiveRecordHelper

#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name

Instance Method Details

#on_send(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/rails/find_each.rb', line 41

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