Module: WTF::QueryTracker

Defined in:
lib/wtf/query_tracker.rb

Defined Under Namespace

Classes: Trackable

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.trackablesObject (readonly)

Returns the value of attribute trackables.



6
7
8
# File 'lib/wtf/query_tracker.rb', line 6

def trackables
  @trackables
end

Class Method Details

.match(pattern, sql) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/wtf/query_tracker.rb', line 36

def match(pattern, sql)
  case pattern
  when Regexp
    pattern.match(sql)
  when String
    pattern == sql
  when Proc
    pattern.call(sql)
  end
end

.on_sql(sql) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/wtf/query_tracker.rb', line 28

def on_sql(sql)
  trackables.each do |it|
    if match(it.pattern, sql)
      WTF::Dumper.new(:sql, sql, *caller.take(it.options[:size] || 30), :line).call
    end
  end
end

.prepare_hookObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wtf/query_tracker.rb', line 16

def prepare_hook
  ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval %{
    module TrackingSQL
      def log(sql, *args, &block)
        WTF::QueryTracker.on_sql(sql)
        super(sql, *args, &block)
      end
    end
    prepend TrackingSQL
  }
end

.start_tracking(pattern, options = {}) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/wtf/query_tracker.rb', line 8

def start_tracking(pattern, options = {})
  if @trackables.nil?
    prepare_hook
    @trackables = []
  end
  @trackables << Trackable.new(pattern, options)
end