Class: SourceMonitor::TurboStreams::StreamResponder

Inherits:
Object
  • Object
show all
Includes:
ActionView::RecordIdentifier
Defined in:
lib/source_monitor/turbo_streams/stream_responder.rb

Defined Under Namespace

Classes: Operation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamResponder

Returns a new instance of StreamResponder.



12
13
14
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 12

def initialize
  @operations = []
end

Instance Attribute Details

#operationsObject (readonly)

Returns the value of attribute operations.



10
11
12
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 10

def operations
  @operations
end

Instance Method Details

#append(target, partial:, locals: {}) ⇒ Object



21
22
23
24
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 21

def append(target, partial:, locals: {})
  operations << Operation.new(action: :append, target:, partial:, locals:)
  self
end

#redirect(url, action: "advance") ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 56

def redirect(url, action: "advance")
  operations << Operation.new(
    action: :redirect,
    target: "source_monitor_redirects",
    partial: nil,
    locals: { url:, turbo_action: action }
  )
  self
end

#remove(target) ⇒ Object



34
35
36
37
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 34

def remove(target)
  operations << Operation.new(action: :remove, target:, partial: nil, locals: nil)
  self
end

#remove_row(record) ⇒ Object



39
40
41
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 39

def remove_row(record)
  remove(dom_id(record, :row))
end

#render(view_context) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 66

def render(view_context)
  operations.map do |operation|
    if operation.action == :redirect
      # Custom redirect action - manually build the turbo-stream tag with URL attribute
      url = operation.locals[:url]
      turbo_action = operation.locals[:turbo_action]
      view_context.tag.send(:"turbo-stream",
        action: "redirect",
        target: operation.target,
        url: url,
        "visit-action": turbo_action
      )
    elsif operation.partial
      view_context.turbo_stream.public_send(
        operation.action,
        operation.target,
        partial: operation.partial,
        locals: operation.locals || {}
      )
    else
      view_context.turbo_stream.public_send(
        operation.action,
        operation.target
      )
    end
  end
end

#replace(target, partial:, locals: {}) ⇒ Object



16
17
18
19
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 16

def replace(target, partial:, locals: {})
  operations << Operation.new(action: :replace, target:, partial:, locals:)
  self
end

#replace_details(record, partial:, locals: {}) ⇒ Object



26
27
28
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 26

def replace_details(record, partial:, locals: {})
  replace(dom_id(record, :details), partial:, locals:)
end

#replace_row(record, partial:, locals: {}) ⇒ Object



30
31
32
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 30

def replace_row(record, partial:, locals: {})
  replace(dom_id(record, :row), partial:, locals:)
end

#toast(message:, level: :info, title: nil, delay_ms: 5000) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/source_monitor/turbo_streams/stream_responder.rb', line 43

def toast(message:, level: :info, title: nil, delay_ms: 5000)
  append(
    "source_monitor_notifications",
    partial: "source_monitor/shared/toast",
    locals: {
      message:,
      level: level || :info,
      title:,
      delay_ms: delay_ms || 5000
    }
  )
end