class QueryLimit::Analyzer

Constants

MATCHER

Attributes

stack[R]

Public Class Methods

new(stack) click to toggle source
# File lib/query_limit/analyzer.rb, line 5
def initialize(stack)
  @stack = stack
end

Public Instance Methods

analyze_np1() click to toggle source
# File lib/query_limit/analyzer.rb, line 11
def analyze_np1
  grp = stack.group_by { |entry| entry.sql.match(MATCHER)&.captures&.at(1) }
  diff = grp.values.find { |entries| entries.size > 1 }

  tell_the_story(diff.first.sql, diff.first.stacktrace) if diff
end

Private Instance Methods

tell_the_story(query, stacktrace) click to toggle source
# File lib/query_limit/analyzer.rb, line 20
def tell_the_story(query, stacktrace)
  puts
  puts 'Possible N+1 query has been detected'
  puts
  puts "#{query}"
  puts
  puts stacktrace.reject { |s| s.include? 'gem' }.map { |s| "    #{s}"}.join("\n")
  puts
end