Module: Bulletin::Warning

Defined in:
lib/bulletin/warning.rb

Overview

Converts a Bullet notification object into a plain, JSON-safe Hash that can survive serialization across an ActiveJob boundary (the thread-local collector is gone by the time a job runs, so we extract eagerly).

Notes from Bullet 8.x internals:

- `notification.path` is always nil (detectors never pass it), so request
  context comes from the Rack env instead, not from here.
- `title`/`body` are public and together form Bullet's own fix advice.
- the call stack lives in @callers (N+1 and unused-eager-loading only;
  counter-cache notifications have none) and is reachable only via an
  ivar because the subclasses mark `call_stack_messages` protected.

Class Method Summary collapse

Class Method Details

.advice_for(notification) ⇒ Object



33
34
35
# File 'lib/bulletin/warning.rb', line 33

def advice_for(notification)
  [safe(notification, :title), safe(notification, :body)].compact.join("\n").strip
end

.backtrace_for(notification) ⇒ Object



37
38
39
# File 'lib/bulletin/warning.rb', line 37

def backtrace_for(notification)
  Array(notification.instance_variable_get(:@callers)).map(&:to_s)
end

.from(notification) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/bulletin/warning.rb', line 18

def from(notification)
  {
    "kind"         => kind_for(notification),
    "base_class"   => notification.base_class.to_s,
    "associations" => Array(notification.associations).map(&:to_s),
    "advice"       => advice_for(notification),
    "backtrace"    => backtrace_for(notification)
  }
end

.kind_for(notification) ⇒ Object



28
29
30
31
# File 'lib/bulletin/warning.rb', line 28

def kind_for(notification)
  # Bullet::Notification::NPlusOneQuery => "n_plus_one_query"
  ActiveSupport::Inflector.underscore(notification.class.name.split("::").last)
end

.safe(notification, method) ⇒ Object



41
42
43
44
45
# File 'lib/bulletin/warning.rb', line 41

def safe(notification, method)
  notification.public_send(method)
rescue StandardError
  nil
end