Class: ActiveRecord::Cause::LogSubscriber

Inherits:
LogSubscriber
  • Object
show all
Defined in:
lib/activerecord/cause.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
["SCHEMA", "EXPLAIN"]

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/activerecord/cause.rb', line 21

def sql(event)
  return if ActiveRecord::Cause.match_paths.empty?
  return unless logger.debug?

  payload = event.payload

  return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

  loc = caller_locations.find do |l|
    ActiveRecord::Cause.match_paths.any? do |re|
      re.match(l.absolute_path)
    end
  end

  return unless loc

  name  = "#{payload[:name]} (ActiveRecord::Cause)"
  sql   = payload[:sql]
  binds = nil

  if respond_to?(:render_bind)
    unless (payload[:binds] || []).empty?
      binds = "  " + payload[:binds].map { |col,v|
        render_bind(col, v)
      }.inspect
    end
  end

  if odd?
    name = color(name, CYAN, true)
    sql  = color(sql, nil, true)
  else
    name = color(name, MAGENTA, true)
  end
  cause = color(loc.to_s, nil, true)

  output =
    if ActiveRecord::Cause.log_with_sql
      "  #{name}  #{sql}#{binds} caused by #{cause}"
    else
      "  #{name}  caused by #{cause}"
    end

  debug(output)
end