Skip to content

Module Poly::Stack

  • Extended by: ActiveSupport::Concern
  • Includes: Poly::Role
  • Defined in: lib/poly/stack.rb

Card-side concern. Turns a polymorphic, role-discriminated model (the “card” model, e.g. Status) into a stack where the most-recently created card per (resource, role) is the current “prime” — the top of the stack. The contract is immutable payload, mutable linkage/index metadata: prior rows are never deleted, but their is_prime and superseded_by_id columns are mutated in place when a new card supersedes them — this is not a literally append-only/immutable-row stack.

class Status < ApplicationRecord
belongs_to :resource, polymorphic: true
include Poly::Joins
include Poly::Stack
poly_stack :resource
end

Poly::Stack builds on Poly::Role (the resource_role discriminator) and adds only is_prime (the enforced golden-child marker) and superseded_by_id (an unconstrained audit edge). It is deliberately payload agnostic: it does not define the payload column, the actor, or the reason — those belong to the host model. The parent-side accessor macro (has_stack) is the consumer’s to write, the way Midas writes has_coin on Poly::Role — see the README.

Public Class Methods

poly_stack

poly_stack(assoc_name, max_length: 64)

Declares the stack. assoc_name is the polymorphic belongs_to whose *_role column discriminates independent stacks on the same table. Applies Poly::Role, adds a prime scope, and installs the callbacks that promote each new card to prime.

  • @param assoc_name [Symbol] the polymorphic association to stack under
  • @param max_length [Integer] maximum role length
  • @return [void]

prime

prime()

Scope: the current prime (top of stack) card per resource and role.

  • @return [ActiveRecord::Relation]