Sha256: 1af19996132df3c70e92bd22cc6a48eda83b8a15616c1e8417889409a9454f47

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8
# 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
      class FindEach < Cop
        MSG = 'Use `find_each` instead of `each`.'.freeze

        SCOPE_METHODS = [:all, :where, :not].freeze

        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)
          return if method_chain(node).any? { |m| ignored_by_find_each?(m) }

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

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

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

        private

        def method_chain(node)
          if (node.send_type? || node.block_type?) && !node.receiver.nil?
            if node.parent
              method_chain(node.parent) << node.method_name
            else
              [node.method_name]
            end
          else
            []
          end
        end

        def ignored_by_find_each?(relation_method)
          # Active Record's #find_each ignores various extra parameters
          [:order, :limit, :select].include?(relation_method)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/rails/find_each.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/rails/find_each.rb
rubocop-0.43.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.42.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.41.2 lib/rubocop/cop/rails/find_each.rb
rubocop-0.41.1 lib/rubocop/cop/rails/find_each.rb
rubocop-0.41.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.40.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.39.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.38.0 lib/rubocop/cop/rails/find_each.rb
rubocop-0.37.2 lib/rubocop/cop/rails/find_each.rb
rubocop-0.37.1 lib/rubocop/cop/rails/find_each.rb