Module: Athar::Dashboard::ActorLabels

Defined in:
lib/athar/dashboard/actor_labels.rb

Overview

Builds a human-readable label for an actor record. Real host apps may or may not override ‘to_s` on their actor models (Devise’s User typically doesn’t), so this falls back to common identifying attributes before using ‘to_s`, and to “Type#id” when nothing readable is available.

Constant Summary collapse

IDENTIFYING_ATTRIBUTES =
%i[email name username login handle].freeze

Class Method Summary collapse

Class Method Details

.humanize(record, type, id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/athar/dashboard/actor_labels.rb', line 13

def humanize(record, type, id)
  return "#{type}##{id}" unless record

  IDENTIFYING_ATTRIBUTES.each do |attribute|
    next unless record.respond_to?(attribute)

    value = record.public_send(attribute)
    return value.to_s if value.present?
  end

  string = record.to_s
  return string if string.is_a?(String) && !string.start_with?("#<")

  "#{type}##{id}"
end