Sha256: e46c20557af0603f03969b22d7fdd46be415ee9884b5352d399f9e1c45691ba8

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

# encoding: utf-8

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
      class FindEach < Cop
        MSG = 'Use `find_each` instead of `each`.'

        SCOPE_METHODS = [:all, :where]

        def on_send(node)
          receiver, second_method, _selector = *node
          return unless second_method == :each
          return if receiver.nil?

          _model, first_method = *receiver
          return unless SCOPE_METHODS.include?(first_method)

          add_offense(node, node.loc.selector, MSG)
        end

        def autocorrect(node)
          each_loc = node.loc.selector

          @corrections << lambda do |corrector|
            corrector.replace(each_loc, 'find_each')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/rails/find_each.rb
rubocop-0.30.0 lib/rubocop/cop/rails/find_each.rb