Sha256: 73aa90f1a7b9b4d2f6d423a0e385d8327307a5c04a2f5976f894a0bdbc126a00

Contents?: true

Size: 926 Bytes

Versions: 9

Compression:

Stored size: 926 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

          ->(corrector) { corrector.replace(each_loc, 'find_each') }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/rails/find_each.rb
rubocop-0.35.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.34.2 lib/rubocop/cop/rails/find_each.rb
rubocop-0.34.1 lib/rubocop/cop/rails/find_each.rb
rubocop-0.34.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.33.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.32.1 lib/rubocop/cop/rails/find_each.rb
rubocop-0.32.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.31.0 lib/rubocop/cop/rails/find_each.rb