V03 in Rails, a pure core and streams: values that pull through ports, and changes that answer with patches

October 10, 2026 · ALA · V03 · Ruby · Rails · Hotwire · Turbo Streams · checklist

V02 scored 100 at every tier and left two things the Elixir side had and the Rails side didn’t. V50 and V51 keep the cart’s state as a value that does no I/O, so a feature’s tests run with no database. And a LiveView page sends the browser only what changed; V02 redirected after every click and let Turbo morph a full render. V03 is V02 with both, as measurable changes rather than a new shape. The layer map, the pull port for product data and the per-request screens are V02’s.

The state abstractions are values

V02’s CartLines, SavedItems and Wishlist each owned an Active Record model and did their own reads and writes. That is Spray’s own form for a domain abstraction with a store behind it, and it meant nothing below the composition ran without Postgres. V03 gives them the shape V51’s state has: pulled through a port on load, changed in memory, every change sent out for a store the composition wires.

class CartLines
  include Foundation::Ports
  output :items, ProgrammingParadigms::RequestResponse   # the stored lines, asked for
  output :products, ProgrammingParadigms::RequestResponse
  output :changed, ProgrammingParadigms::DataFlow         # { op:, ... } for whoever stores
  output :adding, ProgrammingParadigms::DataFlow
  output :rows, ProgrammingParadigms::DataFlow
  output :row_added, ProgrammingParadigms::DataFlow
  output :row_changed, ProgrammingParadigms::DataFlow
  output :row_removed, ProgrammingParadigms::DataFlow
  output :contents, ProgrammingParadigms::DataFlow
  # ...

  input(:load, ProgrammingParadigms::Event) do
    @held = ask(:items)
    @catalog = ask(:products, @held.map { _1[:product_id] }.uniq)
    emit(:rows, @held.map { row(_1) })
    contents
  end

  input(:bump, ProgrammingParadigms::DataFlow) do |change|
    item = find(change[:item_id])
    item[:quantity] = [ item[:quantity] + change[:delta], 1 ].max
    emit(:changed, op: :quantity, id: item[:id], quantity: item[:quantity])
    emit(:row_changed, row(item))
    contents
  end
end

The store is its own abstraction, a data source-and-sink (§5.2.2) with lookup, apply and add ports, and the screen wires the two:

@lines.wire_to(@items, from: :items, to: :lookup)      # the lines, pulled (request/response, §4.6)
@lines.wire_to(@items, from: :changed, to: :apply)     # every change, written
@lines.wire_to(@items, from: :adding, to: :add)        # a new line asked for ...
@items.wire_to(@lines, from: :added, to: :arrived)     # ... and handed back with its id

A value’s tests wire fakes to its ports and run with no database: an Answerer on items and products, a Recorder on changed and contents. Three such tests cover a bump, a removal and a save for later, and assert what was announced for the store rather than what a table holds.

What stayed store-backed, and why: Undo (its held removal must outlive the request, and removed_at is where it lives), PricingChoices (two columns read and written in place), and the flows. The pure core is the three lists a shopper edits, which are the three that change on every click.

A change answers with patches

V02’s controllers redirected after every change and Turbo morphed the page: a full render per click, hidden from the reader. The requirements asked for streams with no full re-render, and V01’s design doc had recorded the departure. V03 closes it. The values emit deltas (row_added, row_changed, row_removed, and rows_changed for a whole pane) beside the full rows a GET renders, and the screen wires each delta, the summary, the undo offer, the tab counts and the selectors to a Patch sink:

line_html = ->(row) { Partial.render("components/cart_line", row:, wishlist_ids: wishlist_ids.(), t: t[:cart], labels:) }
@lines.wire_to(patch(:append, dom.items_pane_id, line_html), from: :row_added)
@lines.wire_to(patch(:replace, ->(row) { dom.cart_line_id(row[:id]) }, line_html), from: :row_changed)
@lines.wire_to(patch(:remove, ->(row) { dom.cart_line_id(row[:id]) }), from: :row_removed)
@totals.wire_to(patch(:update, dom.summary_id, ->(summary) { Partial.render("components/cart_summary", summary:, t: t[:cart]) }), from: :summary)

ProgrammingParadigms::Patches is the response: a buffer the sinks add to, where the last patch of a target wins (a summary re-emitted by every load and change is sent once), rendered through the controller’s turbo_stream builder. The controller concern answers a Turbo request with the patches and a plain request with V02’s redirect, so V02’s fifty integration tests pass unchanged and four new ones assert the stream. The targets are Foundation::DomTargets names, which the page renders and the screen patches: the same agreed-names module as V02, with eleven names instead of three.

This is the “display inside” wiring Spray draws, written as sinks. Two things it forced out of the domain: Undo announces pending after every change, not only on load, and PricingChoices announces a shipping or promo choice as it is made. V02 could rely on the reload to show those; a page that is patched can’t.

The cart, drawn from the built graph

flowchart LR
  screen[[Screens::Cart]]
  lines[lines: CartLines]
  undo[undo: Undo]
  saved[saved: SavedItems]
  wishlist[wishlist: Wishlist]
  pricing[pricing: PricingChoices]
  totals[totals: CartTotals]
  products[products: ProductIndex]
  items[items: CartItemsStore]
  saved_store[saved_store: SavedItemsStore]
  wishlist_store[wishlist_store: WishlistStore]
  lines -- items → lookup --> items
  lines -- products → lookup --> products
  lines -- changed → apply --> items
  lines -- adding → add --> items
  lines -- rows --> screen
  lines -- contents → contents --> totals
  lines -- removed → capture --> undo
  lines -- saved → stash --> saved
  lines -- line --> screen
  lines -- row_added → push --> patch_1([Patch])
  lines -- row_changed → push --> patch_2([Patch])
  lines -- row_removed → push --> patch_3([Patch])
  undo -- pending --> screen
  undo -- pending → push --> patch_4([Patch])
  undo -- restored --> screen
  saved -- items → lookup --> saved_store
  saved -- products → lookup --> products
  saved -- changed → apply --> saved_store
  saved -- rows_changed → push --> patch_5([Patch])
  saved -- moved → receive --> lines
  wishlist -- items → lookup --> wishlist_store
  wishlist -- products → lookup --> products
  wishlist -- changed → apply --> wishlist_store
  wishlist -- rows_changed → push --> patch_6([Patch])
  wishlist -- taken → receive --> lines
  pricing -- discount → discount --> totals
  pricing -- shipping_method → shipping_method --> totals
  totals -- summary --> screen
  totals -- summary → push --> patch_7([Patch])
  totals -- summary → push --> patch_8([Patch])
  items -- added → arrived --> lines

The stores are at the right, wired like any other part. Each Patch is a sink the screen made inline, drawn by what it is. Some edges to the screen are left out here for room; the generated chart in the repo has every wire.

What it scores

V02V03
ala_lint default / --strict / --super-strict100 / 100 / 100100 / 100 / 100
Rules met under --super-strict (of 10 checked)1010
Functions with no scored finding208 / 208238 / 238
Integration tests5057
Browser tests (Cuprite)55
Cart screen, lines90149
Non-blank lines in app/1,8772,131
Familiarity44

Two findings came up during the build and were real. The saved and wishlist panes each rendered their own line partial, a component rendering a peer (ALA R1); the fix is V02’s own, a generic pane element that takes the item partial’s name from the page. And "flash" appeared in DomTargets and in the flash element (ALA R5); the element now renders flash_id. One linter change was needed, a precision fix rather than an exemption: a no-argument read of a name on a constant or a local, passed into a call, is configuration being passed and not one abstraction’s result handed to another, so the R11 handled-data check no longer counts it.

Later (2026-10-19)

A clone check joined the linter (R7, reported at the default tier and scored from --strict). It finds three pieces of logic written twice here: the checkout’s and the portal’s step flow, the product-row projection, and the two totals summaries, the duplicates later extracted in V05 to V07. They are left as they are in this archived variant, so it scores 100 / 99 / 99. The same day CartLines#find was written def find(id) = … or raise …, and Ruby attaches the or raise to the method definition, so a line it didn’t hold came back nil instead of raising; it is bracketed now, with a test. 58 integration tests and 5 browser tests.

V03 was archived the same day. Its pure core and its Turbo Stream patches are carried by V05, V06 and V07, which also have the fixes for five defects found that day (an undo over Turbo that didn’t put the row back, gift wrap kept on removal, no “Item restored” in the portal, a stale checkout-button total, and a payment outcome that could be lost during the Pay redirect). V03 has all five and keeps them, as archived variants are left as they were.

What it costs at run time

V03 is the Rails design where the domain’s tests say what was announced rather than what a table holds, and where a click costs the page what it changed. Both are things the Elixir variants had first, and both fit Rails’ own tools: Turbo Streams were waiting for an abstraction whose outputs are already deltas.

The code is in ala_variants_ruby/v03-pure-core-streams.

← all posts