How close can a Ruby on Rails app come to ideal ALA?
The LiveView version of this question ended with six designs at 100 on the hand walk. Rails is a different kind of framework, and Ruby a different kind of language, so the question gets asked again: for each thing a Rails app does that Spray’s application wouldn’t, is it forced by Rails, forced by Ruby, or a habit? Two designs answer it, V01 and V02, and the second meets every rule the Ruby linter checks at --super-strict.
What Spray asks
The same as before: the application instantiates, configures and wires, holds all the product’s knowledge and none of its logic, “no normal programming language code such as assignments and if statements” (§3.5). For an OO language he adds what the Ruby checklist gathers: classes have a main interface for the layer above and ports for everything else; peers have no associations, “not even its interface. Not even a base class” (§2.1.3); wiring is explicit, never a container’s; configuration and wiring are “immutable after they are set” (§3.9); one thread per stateful instance. His own form is an object graph built once at start-up and then set running (§3.11.3).
What Rails hands the controller
A Rails app is called per request. It gets a controller action named by a route, params as strings, a session, a flash, and a response to render or redirect; then the request ends and everything it built is gone. Turbo hands it a second channel back to open pages, streams it broadcasts on. Active Job hands it a way to do work after the response.
Some of this is Spray’s model under other names. A controller action that pushes one input is a wire from an event to a port. A button_to is an event named by a URL. The template’s nesting is the “display inside” wiring. Other parts are foreign: a composition that can’t outlive a request, a session the framework owns, a form whose method the framework chooses, and an object model where every reference is mutable.
Rails’ pieces as abstractions
| Piece | What it is | What it may do |
|---|---|---|
| a controller action | application: a wire | decode params, push one input of a composition, render or redirect |
a screen class (Screens::Cart) | application | instantiate, configure, wire, hold the words; built per request |
| a page template | application: UI-layout wiring | place components, pass values in; no loop, comparison, or model call |
| a partial that takes projected rows | UI domain abstraction | render what it’s given, name the URLs it’s given |
a model (ApplicationRecord) | persistence for one feature, or the shared identity | reached as a configured store; owned by one feature |
| a validation | an intrinsic constraint | messages from the composition or I18n keys it owns |
| a Turbo Stream broadcast | a sink the composition wires | the composition owns the stream names; since 2026-10-21 a change made elsewhere travels as a fact instead (see “Changes made elsewhere”) |
| an Active Job | an asynchronous execution model | deliver a push to an input the composition named |
| a Stimulus controller | UI state only | never a product rule |
| a service object, a concern on a model, a DI container | not used | — |
The departures, one at a time
Forced by Rails, and valid
The composition is rebuilt per request. Spray wires once at start-up. Rails can’t, so each screen’s constructor runs per request, and every stateful instance keeps its state in a store it is configured with (the lines in cart_items, the undo offer in a removed_at column, the checkout step in its own table). The screen never looks inside what it stores. The dividend is that Puma’s threads never share an instance, which is Spray’s one-thread rule (§3.9) for free. The cost is that a job that finishes later has to rebuild the screen by name to deliver its result; PushLater does exactly that, and it is the one piece that reads as a trick.
Routing an outcome to a response. if screen.saved then redirect_to ... else render :form_again is a branch in the application. Its arms only redirect, render, or hand the page a value, which is R11’s own exemption for routing two outputs. A stricter reader would give Records two outputs and the controller two one-line actions; I left it, and the linter reads it as routing.
Decoding params. params[:id].to_i adapts the browser’s strings. Small, and at the edge. params.expect(...) does the rest.
The session as the identity’s home. The cart id lives in the session because Rails has nowhere else per browser. Reading it is the framework’s; the fallback for a flow that outlives its cart (session.fetch(:cart_id) { cart_id }) is the one place the guard moved into the connection rather than out of the code.
Forms, CSRF and HTTP methods. form_with model: picks the URL and the method from the record, which is the convention that removed a persisted? ternary from a page. It also sends _method=patch for a persisted record, which broke a POST-only validate route and a POST-only submit route until the browser tests found both. The live-validation fetch now drops _method and sends the page’s CSRF token, because Rails 8’s per-form tokens are bound to the form’s own action.
Framework base classes. ApplicationController, ApplicationRecord, ApplicationJob: subclassing a framework class to configure it is R9’s allowed form, and the linter exempts it. Subclassing your own class isn’t.
Forced by Ruby, and handled
No interfaces, no field types. Spray’s port is a field typed by a paradigm interface. Ruby has neither, so the Foundation declares ports with macros: output is a private instance variable the wiring fills, input a handler that stays private. It is the one vocabulary a Rails team hasn’t met, and it is ninety lines. The consequence a Rubyist meets first: an output port is the instance variable of its name, so a class can’t keep state under that name too.
Duck typing hides the knowledge, not the coupling. A class that sends a peer’s own messages to whatever it was given is written to that peer’s design whether or not anything declares it (§3.3.1). The rule became “send only a paradigm’s messages”, and the tell is respond_to?(:charge) on a collaborator. The linter flags that form; it can’t see a plain gateway.charge(...), which is the honest limit the checklist states for a dynamically typed language.
Mutable references. This is where Elixir had it for free. A value handed to two peers in Ruby can become a back channel; a getter can hand out a live collection for a peer to change. The variants put hashes built per call and frozen Money values on wires, keep configuration in constructor arguments that are never reassigned, and build the hardware command of the coffee maker with Data#with. The remaining live objects on wires are Active Record records that Records and the flows emit on form and created, which only the composition lands for the view. A clean R2 in Ruby is a design achievement; in Elixir it was the language.
One constant per file. Zeitwerk wants it, and so does Spray (“one abstraction per file”, §2.3.2). The one friction is that app/ala/ as an autoload root gives the layers their namespaces for free, and Rails.application makes Application an unusable layer name, so the compositions are Screens::.
Not forced: habits the second design removed
Each of these was a finding on V01 and a change in V02.
An association between features’ tables. belongs_to :product on every line table let the storefront’s lines read the catalogue’s columns, Spray’s shared entity (§6.17.2). V02 holds product ids and asks a products pull port, wired by the screen to one ProductIndex. One more query per page; the join was the coupling.
Broadcasts from model callbacks. after_commit { broadcast... } is a lower record deciding what happens next. Both designs wire broadcasts from screens; the linter flags a callback that reaches another unit as R1.
A helper as the agreed names. V01 kept the DOM ids live updates target in a helper both components and screens called, a peer edge from every component. V02 makes it Foundation::DomTargets, declared with helper, so both depend down.
Components rendering components. A cart line that renders a product line and a stock badge is a UI abstraction naming peers. V02 declares a views/elements/ tier of generic elements under views/components/, and no component renders a component.
Loops, comparisons and ternaries in pages. @s.rows.each, wishlist_ids.include?, form.persisted? ? ... : .... V02 renders collections, lets rows carry their URLs from a lambda the screen configures with the route helpers, and lets the cart line take the wishlist’s ids. The form’s URL comes from form_with model:.
One domain layer for everything. Totals calling pricing, a flow calling a stock check, looked like peers because the map said so. V02 declares a rules layer under the domain for the pure rules and projections. A map with one domain layer reports the same code as fifteen peer edges; the honesty is in what the layer holds: nothing in rules/ reads a store or a word.
Stateful abstractions holding what the page needs to decide. Records emits created and updated instead of saved so the screen wires two notices and two live updates with no branch; SubmitOrder emits its step after a failed submit so the controller’s failure arm only renders.
What the two designs show
| V01 | V02 | |
|---|---|---|
ala_lint default / --strict / --super-strict | 66 / 61 / 53 | 100 / 100 / 100 |
Rules met under --super-strict (of 10 checked) | 4 | 10 |
Scored findings at --super-strict | 45 | 0 |
| Functions with no scored finding | 182 / 204 | 208 / 208 |
| Tests (integration + browser) | 50 + 0 | 50 + 5 |
| Familiarity | 4 | 4 |
The two measures the linter reports and never scores stay where they are: the composition’s share of functions (42%: controllers and templates count, and a request-per-action framework has many) and the average unit size (18 lines: one-port classes are small). Spray’s 3 to 10% was measured on wiring code; a Rails app’s “application” includes every action and every page.
How far a Rails app actually gets
Further than I expected, and for a reason different from LiveView’s. LiveView’s departures were about the page: events by name, two mounts, a URL that changes. Rails’ departures are about the request ending and the language’s objects. The first is solved by the same move every time, a store configured into the instance that needs it, and the move was already Spray’s: persistence is “an abstraction of persistence”, not a port to an adapter (§6.19). The second is solved by discipline the linter can only partly check.
What holds V02’s score up is five declarations the tool can’t judge: the rules and elements layers, peer_ok on the paradigms layer, the session fallback, the routing arms, and cart_items shared by the storefront and the portal as the lines abstraction both build on. The design records each. None is a trick, and any could be argued the other way by a reader who draws the map differently.
What it costs
- A screen class per page and small controllers: more files than a scaffold.
wire_toandon, one new vocabulary; everything else is stock Rails.- Every list is a
render collection:, so a row that wants to differ says so in its data. - Live updates and the payment job need the screen to be rebuildable from its config, which is true today and a constraint a future screen could break silently.
- Mutability is policed by convention. The linter catches class variables,
Thread.current, exposed mutated collections and reassigned fields; it can’t see an aliased object handed to two peers.
What it costs at run time
Rails rebuilds the screen on every request, so the first question is what the composition itself costs: a few dozen allocations, O(parts + wires), before any query. It doesn’t register. The queries do, and ALA puts them in one visible place each:
- Pull ports batch, or they’re N+1.
CartLines,SavedItemsandWishlisteach ask theproductsport once with the list of ids and receive a hash, so the cart page costs one query per feature, not one per line. V01’sincludes(:product)had the same count through the association; removing it was about coupling, not cost. The hazard is general: a request/response port looks the same whether it is asked once or n times, and the drawing shows the port, not the cardinality. The composition has to see that the question is batched. - One input costs what it reaches. Removing a line runs
CartLines.remove, which emitsremovedonce; the wire lands it inUndo.capture, one update. Then the redirect re-renders the page, which is where a Rails app pays: a full screen build and all of its loads per change, hidden by Turbo’s morph. LiveView keeps the state in the process and patches one row; Rails reloads. That is the framework’s execution model, not the design’s, and it is the real difference between the two platforms’ cost shapes. - Jobs rebuild by name.
PushLaterconstructs the checkout screen again inside the job and pushescharge: the same O(parts) as a request, plus the loads the screen’s parts do. A job that rebuilt a screen with an expensive constructor would pay it twice; R11’s “no I/O in the composition” is also what keeps the rebuild cheap. - Broadcast fan-out is the stream’s grain. The catalogue stream carries every product fact to every open page, as in Elixir; here Turbo does the delivery and the DOM patch, so a page pays nothing in Ruby for a fact it doesn’t show. That changed in V05 to V07 with
ChangeFeed(below): each open page now pays a request for each fact it listens to.
The coffee maker shows the other shape: polled execution senses three abstractions every cycle (right for a controller), and Boiler#drive first emitted its heater command on every push of any input, so a cycle where state and pot both changed emitted it three times (harmless, since the last landing won). It now emits only when the output changes, as Not and Equals always did, and a test counts one command per change: the output-change guard is the connection’s guard, in Spray’s sense, written once in the instance that owns the output. The checklist’s “Kinds of connection” section now records these as the run-time properties a wiring makes checkable.
Changes made elsewhere
Added 2026-10-21. Rails’ own answer to showing a change made elsewhere is Turbo Streams from the model. broadcasts_to (or an after_update_commit that calls broadcast_replace_to) re-renders the record’s partial when it commits and pushes the HTML to every page that opened the stream with turbo_stream_from. It is very little code, and it is the coupling ALA forbids: the model decides which views hear about it and renders them, so a lower record knows every view (R1, R10). V01 and V02 moved the broadcasts out of the models into screens (LiveUpdate, wired like any sink). That fixed the direction but not the knowledge. The screen that made a change still rendered another page’s row and aimed it at a DOM id.
By V05 to V07 that had produced two bugs, found in all three. A staff stock edit at /products never reached an open cart, because nothing broadcast to the cart’s markup. And the portal’s catalogue row shared the /products row’s DOM id, so a staff edit replaced it with the staff row, Edit and Delete buttons included. A change-cost experiment run the same day, ten requirement changes made in sequence to a conventional Rails app and to V07, found the same thing from the other side: the steps that added live updates were ALA’s most expensive and held most of its confirmed defects, because a state-holding page had to build new machinery each time, while the conventional app reused broadcasts it already had.
ProgrammingParadigms::ChangeFeed makes outside change a paradigm, built once in the layer Spray keeps for paradigm interfaces (§2.2), with the observer inside the paradigm, where “the subscriber does not know the publisher” (§4.4.2). It is a Publish sink the changing screen wires, a Subscription part a page places with the topic and the kinds of fact it listens to, one channel, and one Stimulus controller:
# V07's catalogue: a staff save publishes the product's stock
@publish_stock = ProgrammingParadigms::ChangeFeed::Publish.new(topic: Store::CATALOG_TOPIC, kind: :stock, fields: ->(product) { { product_id: product.id, stock: product.stock } })
# V07's cart: a feed of that kind, and the arm that patches its badges
@feed = ProgrammingParadigms::ChangeFeed::Subscription.new(topic: Store::CATALOG_TOPIC, kinds: %i[stock], url: urls.live_cart_path)
in [ :feed, :stock ] then @patches.replace_all(@dom.stock_selector(payload[:product_id]), @badge_html.(payload))
What travels is a fact keyed by identity ({ product_id:, stock: }), never HTML. The topic is the one name both sides share, the way two sides agree on a global event name (§4.7.4), so it is declared once and handed to both compositions as configuration.
How it relates to the two Rails pieces it touches:
- Turbo Streams still patch the page, but only the page that is showing itself. In V05 and V07 the browser posts a fact back to the page’s own live URL, and that screen answers with its own Turbo Stream patches. V06 morph-refreshes the page, and the rebuilt screen reads current values through its tables. Either way each page renders its own markup. What goes is the part
broadcasts_todoes: render once in the model and send the HTML to every page. - Action Cable is the transport. In V06 and V07,
ChangeFeed::Channelis anActionCable::Channel::Basethat streams a topic signed the way Turbo signs stream names. V05 carries its fact as JSON in a custom<turbo-stream action="change_feed">on Turbo’s own signed stream channel. ChangeFeed replaces neither. It decides what goes over the wire (a fact, not a fragment) and who decides what the fact means (the page’s wiring, not the model).
The cost is real. Every fact is a request per open page (V05, V07) or a whole-page GET (V06), where broadcasts_to sent one push of HTML that the browser applied. In return no model and no screen knows another page’s markup, and a new kind of outside change is a publish sink and a wire. The V05, V06 and V07 posts each have how it is wired there. The LiveView variants have the same paradigm over Phoenix.PubSub, and the LiveView post explains why it isn’t LiveView either.
What hasn’t been tried
Spray’s Features layer in Rails (V02’s screens are under 90 lines, so none has outgrown one abstraction); ViewComponent as the UI abstraction instead of partials; a contract check between templates’ data-action names and Stimulus controllers; a multi-page composition where two screens share a part; Turbo Stream responses instead of morph refresh for a shopper’s own changes; a real payment gateway with a webhook; and authentication, where before_action is the obvious place for a rule R11 says shouldn’t be there.
The code is in ala_variants_ruby.