Sha256: 5f3890cf0713102fced85c71ba90daac6e66132df8db374f097e42398a6c43b7

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # 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
      class FindEach < Base
        include ActiveRecordHelper
        extend AutoCorrector

        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 preload
          references unscoped where
        ].freeze

        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

        def ignored?(node)
          return true if active_model_error_where?(node.receiver)

          method_chain = node.each_node(:send).map(&:method_name)

          (cop_config['IgnoredMethods'].map(&:to_sym) & method_chain).any?
        end

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

        def active_model_error?(node)
          node.send_type? && node.method?(:errors)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rails-2.12.3 lib/rubocop/cop/rails/find_each.rb