← Back to Pull requests

Add gift cards as a preview account type #4294

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 18, 2026 03:57 UTC
Author

Problem

Sure has no good place for store and gift cards. People park them under "Other assets", where a card's value can't go down as it is spent, there is nowhere to keep the code or PIN, and nothing reminds them before a card expires.

What this adds

A GiftCard accountable, behind preview features.

  • Tracked as cash. The balance is what's left on the card and spending is recorded as transactions, like a depository. Gift cards are grouped under assets in the sidebar and balance sheet. They are deliberately left out of the spendable-cash lists (budget cash accounts, fundable goal accounts, cash insights, paycheck planner), since the money can only be spent at one store; a comment on Account#balance_type records this.

  • Overview tab:

  • - remaining amount, initial amount and amount spent
    - format (physical or digital)
    - expiry date, with "expires in N days" or "expired" badges
    - a "Card details" disclosure with copy buttons for the code and PIN, and an open link for the redeem URL. The PIN is masked (••••) until "Show" is clicked.
  • Form:

  • - "Received on", which is used as the opening date
    - initial amount; leave it empty for an unspent card
    - format, expiry, card number or code, PIN and redeem link
  • Credentials. The card number, PIN and redeem link can spend the card, so they are:

  • - encrypted at rest when Active Record encryption is ready, including keys derived from SECRET_KEY_BASE (same Encryptable policy as other secrets). Without encryption keys, the form shows a warning that they will be stored unencrypted.
    - covered by security:backfill_encryption (BACKFILL_MANIFEST, version 14)
    - filtered from request logs
    - excluded from as_json, which covers the family data export
    - not restored by the family data import
    - shown only to the account owner and full_control shares. read_write (annotate-only) and read_only shares see the balance and expiry, not the credentials.
    - PIN kept out of the account page. The overview shows it masked; "Show" loads it into a Turbo frame from GET /gift_cards/:id/credentials, with the same permission check and Cache-Control: no-store. The card number stays visible, since it is what tells cards apart and is needed to use them.
  • Partly spent cards. The initial amount is stored as face_value, together with the currency the card was issued in. "Spent" is face_value − balance. A balance edit rewrites a manual account's opening valuation, so the initial amount is stored separately rather than read from that valuation.

  • Opening date. The ledger opens at the end of the day before "Received on". A valuation sets that day's closing balance, so spending on the day the card was received still lowers the balance.
  • Changes to shared code

  • AccountableResource:

  • - Account type on create and update: create builds the controller's own type instead of the submitted accountable_type, and update ignores a submitted type. Without this, any account form could be used to create, or switch to, a gated type such as GiftCard. PropertiesController gets the same treatment. Shared tests in AccountableResourceInterfaceTest cover every account controller.
    - A rejected create form now keeps the submitted opening date instead of resetting it.
  • Accountable::PREVIEW_TYPES and Accountable.types_for(user): the account picker and the import account-type mapping use these to hide preview types from users without preview features. The appearance settings use Accountable.group_types_for(user), which also includes the type of any account the user can access, so turning preview features off never hides an existing account's group. Saving the form keeps the saved selection of groups it didn't show.

  • docs/llm-guides/architecture.md: the account type inventory now includes GiftCard and preview types.
  • Screenshots

    | Overview | New gift card |
    |---|---|
    | !Overview | !Form |

    | Account picker (preview pill) | Sidebar |
    |---|---|
    | !Picker | !Sidebar |

    Migration notes

    One new migration, CreateGiftCards (20261016120000). It creates the gift_cards table:

  • face_value and face_value_currency

  • expires_on and card_format, with a check constraint for physical/digital

  • encrypted text columns for card_number, pin and redeem_url

  • subtype and locked_attributes
  • The migration is additive. GiftCard is added to ActiveRecordEncryptionConfig::BACKFILL_MANIFEST and CURRENT_BACKFILL_VERSION moves from 13 to 14, so installs that completed an earlier backfill re-run it before the plaintext fallback is turned off.

    Limitations and decisions

  • Preview gates creation only. It hides the account picker entry and the gift card form, account type mappings in CSV imports, and the appearance setting. Existing gift-card accounts stay visible and editable after a user opts out, and their balances stay in financial totals. Provider link flows and the NDJSON import (SureImport) accept any Accountable::TYPES value, so a hand-crafted request can still create a gift card in the user's own family. I kept that out of scope to keep the PR focused; it only affects visibility, not authorization.

  • Spending after a currency change. After an account's currency changes, "spent" shows as unknown and the initial amount keeps its original currency. Converting the face value can't match the balance sync, which converts each entry at its own date.

  • Balance edits after a currency change (existing behaviour). Account::CurrentBalanceManager applies a balance delta in the account's currency to an opening valuation still in the old currency. This affects every manual account and is not addressed here.

  • History start. Because of the end-of-previous-day anchor, the balance history starts one day before "Received on".

  • Expiry is informational. Nothing happens automatically when a card expires.

  • Currency label. The initial-amount field's currency symbol doesn't follow a change in the balance currency selector while the form is open. The field uses step: "any", so the amount is not rounded to the precision of the currency it was rendered with. Instead, the model rounds it on save to the precision of the card's own currency.
  • Validation

  • bin/rails test (after merging main on 2026-11-12): 15,834 runs. The gift card and encryption backfill failures from the merge are fixed. What remains fails the same way on main locally: SimplefinEntry::ProcessorTest, SimplefinItem::ImporterRotatedIdsTest, Settings::HostingsControllerTest and Eval::Langfuse::ExperimentRunnerTest.

  • test/system/accounts_test.rb, which includes the PIN show/hide flow, passes.

  • With temporary encryption keys, the gift card encryption tests pass. Without keys they are skipped.

  • bin/rubocop, erb_lint, npm run lint (Biome) and bin/brakeman are clean. i18n-tasks check-normalized is clean for the new locale files.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Added support for physical and digital gift card accounts, including balances, expiry details, and redemption information.
    Gift card overviews show remaining, initial, and spent amounts, plus expiry status; gift cards also include overview and activity tabs.
    Users with preview features enabled can create gift cards, and credential visibility follows sharing permissions.
    Gift cards can be imported and categorized alongside other account types.
    Demo data now includes gift cards and related spending.
    Bug Fixes
    Account forms can no longer override the account type assigned by the app.
    * Account-group selections not shown in appearance settings are preserved when settings are updated.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: nielseulink Review duration: 62777m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4294"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59% (MEDIUM) · Higher = larger blast radius
    59%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 34.8% · Safe to merge
    34.8%