summaryrefslogtreecommitdiff
path: root/rubocop/cop/performance/ar_count_each.rb
blob: 2fe8e549872a0066f096a13803f340fdfa174110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      class ARCountEach < RuboCop::Cop::Cop
        def message(ivar)
          "If #{ivar} is AR relation, avoid `#{ivar}.count ...; #{ivar}.each... `, this will trigger two queries. " \
          "Use `#{ivar}.load.size ...; #{ivar}.each... ` instead. If #{ivar} is an array, try to use #{ivar}.size."
        end

        def_node_matcher :count_match, <<~PATTERN
          (send (ivar $_) :count)
        PATTERN

        def_node_matcher :each_match, <<~PATTERN
          (send (ivar $_) :each)
        PATTERN

        def file_name(node)
          node.location.expression.source_buffer.name
        end

        def in_haml_file?(node)
          file_name(node).end_with?('.haml.rb')
        end

        def on_send(node)
          return unless in_haml_file?(node)

          ivar_count = count_match(node)
          return unless ivar_count

          node.each_ancestor(:begin) do |begin_node|
            begin_node.each_descendant do |n|
              ivar_each = each_match(n)

              add_offense(node, location: :expression, message: message(ivar_count)) if ivar_each == ivar_count
            end
          end
        end
      end
    end
  end
end