Class: WhereIsWaldo::Presence

Inherits:
ApplicationRecord show all
Defined in:
app/models/where_is_waldo/presence.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_associations!Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/where_is_waldo/presence.rb', line 17

def configure_associations!
  subject_class = WhereIsWaldo.config.subject_class_constant
  return unless subject_class

  subject_col = WhereIsWaldo.config.subject_column
  # rubocop:disable Rails/InverseOf -- dynamic association, inverse not applicable
  belongs_to :subject,
             class_name: subject_class.name,
             foreign_key: subject_col,
             optional: true
  # rubocop:enable Rails/InverseOf
end

.session_columnObject

Dynamic column accessors



31
32
33
# File 'app/models/where_is_waldo/presence.rb', line 31

def session_column
  WhereIsWaldo.config.session_column
end

.subject_columnObject



35
36
37
# File 'app/models/where_is_waldo/presence.rb', line 35

def subject_column
  WhereIsWaldo.config.subject_column
end

.table_nameObject



12
13
14
15
# File 'app/models/where_is_waldo/presence.rb', line 12

def table_name
  tn = WhereIsWaldo.config.table_name
  tn.respond_to?(:call) ? tn.call : tn
end

Instance Method Details

#as_presence_hashObject

Convert to hash for API responses



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/where_is_waldo/presence.rb', line 58

def as_presence_hash
  config = WhereIsWaldo.config

  {
    session_id: self[config.session_column],
    subject_id: self[config.subject_column],
    connected_at: connected_at&.iso8601,
    last_heartbeat: last_heartbeat&.iso8601,
    tab_visible: tab_visible,
    subject_active: subject_active,
    last_activity: last_activity&.iso8601,
    metadata: ,
    subject: subject_data
  }
end

#online?(timeout: nil) ⇒ Boolean

Check if this presence is considered online

Returns:

  • (Boolean)


75
76
77
78
# File 'app/models/where_is_waldo/presence.rb', line 75

def online?(timeout: nil)
  threshold = (timeout || WhereIsWaldo.config.timeout).seconds.ago
  last_heartbeat && last_heartbeat > threshold
end