Module: Pgbus::Web::JobContext
- Defined in:
- lib/pgbus/web/job_context.rb
Overview
Presents the Current attributes persisted in a job payload (Pgbus::CurrentAttributes::METADATA_KEY) for the dashboard's Context card: { "Current" => { "tenant" => "gid://app/Tenant/42", ... } }.
Pure presentation: ActiveJob argument encodings are unwrapped to display strings (a GlobalID shows its gid, a serialized Symbol/Time its value) — nothing is constantized or located. Callers pass the payload through PayloadFilter first (ApplicationHelper#pgbus_parse_message does), so sensitive values arrive already redacted.
Constant Summary collapse
- AJ_GLOBALID =
"_aj_globalid"- AJ_SERIALIZED =
"_aj_serialized"- AJ_META_PREFIX =
"_aj_"
Class Method Summary collapse
-
.display(value) ⇒ Object
String form of a value for the card.
- .from_payload(payload) ⇒ Object
- .present_attrs(attrs) ⇒ Object
- .unwrap(value) ⇒ Object
Class Method Details
.display(value) ⇒ Object
String form of a value for the card. Hashes/arrays that are ActiveJob encodings collapse to their payload; anything else renders as JSON (stable across Ruby versions — Hash#inspect changed in 3.4).
46 47 48 49 |
# File 'lib/pgbus/web/job_context.rb', line 46 def display(value) unwrapped = unwrap(value) unwrapped.is_a?(String) ? unwrapped : JSON.generate(unwrapped) end |
.from_payload(payload) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/pgbus/web/job_context.rb', line 23 def from_payload(payload) data = parse(payload) stored = data && data[Pgbus::CurrentAttributes::METADATA_KEY] return nil unless stored.is_a?(Hash) && stored.any? stored.to_h do |klass_name, attrs| [klass_name.to_s, present_attrs(attrs)] end end |
.present_attrs(attrs) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/pgbus/web/job_context.rb', line 33 def present_attrs(attrs) return {} unless attrs.is_a?(Hash) attrs.each_with_object({}) do |(name, value), out| next if name.to_s.start_with?(AJ_META_PREFIX) out[name.to_s] = display(value) end end |
.unwrap(value) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pgbus/web/job_context.rb', line 51 def unwrap(value) case value when Hash return value[AJ_GLOBALID] if value.key?(AJ_GLOBALID) return unwrap(value["value"]) if value.key?(AJ_SERIALIZED) && value.key?("value") value.each_with_object({}) do |(k, v), out| next if k.to_s.start_with?(AJ_META_PREFIX) out[k] = unwrap(v) end when Array value.map { |v| unwrap(v) } else value end end |