Class: MaquinaStream::BlocksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/maquina_stream/blocks_controller.rb

Overview

The blocks the client asked for, as morphing Turbo Stream actions.

Only ids the message actually has are served, and only the ones requested: ids comes from the client, so it is filtered against the document rather than trusted. A request for every block is a legitimate cold repair, so the count is not capped — but each id is matched, not interpolated.

Instance Method Summary collapse

Instance Method Details

#index ⇒ Object

GET /maquina_stream/:sid/blocks?ids[]=… → a Turbo Stream carrying one morphing replace per requested block.



13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/maquina_stream/blocks_controller.rb', line 13

def index
  requested = Array(params[:ids]).map(&:to_s).to_set
  blocks = document.blocks.select { |block| requested.include?(block.id) }

  # Rendered explicitly rather than via `render turbo_stream:`, which does
  # not count as a render when the list is empty and then looks for a
  # template that does not exist. Asking to repair nothing is a normal
  # answer to a manifest that already agreed.
  render plain: blocks.map { |block| morph(block) }.join, content_type: Mime[:turbo_stream]
end