Store a card's credit limit and show how much of it is in use

JA
2d ago
+554-1

Part of #4356
Fork pre-flight: jaysbeekay/sure#1020 (same commits)

Stacked on up/card-daily-rate.

Verified on the stack's top commit 6f85ab855 (up/card-next-statement, with current main merged in at the bottom and each slice merged up the chain): full unit suite 19760 runs, 0 failures, 8 errors, 9 skips (the 8 are ReleaseNotesWorkflowTest, no node in the test container); the card system test credit_card_limit_chart_test 8 runs, 0 failures.

Summary

Cards get a credit_limit column beside available_credit, editable on the card form. The Overview tab shows the limit and how much of it the balance uses ("Using 8.9% of limit"). As decided on the issue (2026-11-20), a user-set limit drives the percentage; available_credit is not read for it, and Enable Banking's balance derivation is unchanged.

What changed

  • Migration 20261128120000_add_credit_limit_to_credit_cards: credit_cards.credit_limit decimal(10,2), nullable (same precision as available_credit).
  • CreditCard validates credit_limit as at least 0 and below CREDIT_LIMIT_CEILING (10**8, the column's range), nil allowed. A negative or oversized limit from the form gets the usual 422 instead of saving, or a 500 from a numeric overflow.
  • CreditCard#credit_limit_money, #utilization_percent: owed / limit × 100, rounded to one decimal; a balance over the limit shows its true share (110%); an overpaid card shows 0; nil for a nil, zero or negative limit.
  • Card form: "Credit limit" beside "Available credit"; CreditCardsController permits it. A form save locks it (Account#lock_saved_attributes! → accountable), so a provider writing through enrich_attribute (the Redbark slice, #5441) keeps a user's value.
  • Overview tab: a "Credit Limit" card above the statement cards, shown whether or not the statement cycle is set.
  • Family::DataImporter::CREDIT_CARD_DETAIL_ATTRIBUTES carries credit_limit (the exporter serialises all accountable columns). A limit the card would refuse (below 0 or 10**8 and over) is dropped to nil, so it does not roll back the whole import.
  • Folded in: available_credit, apr and annual_fee are decimal(10,2) too and had no bound, so a value of ±108 from the form or an import hit PG::NumericValueOutOfRange. They now validate strictly between −CREDIT_LIMIT_CEILING and CREDIT_LIMIT_CEILING (none required ≥ 0 before, and none does now), and Family::DataImporter drops a value outside that range on either side instead of failing the import. Tests: "available credit, APR and annual fee must fit their columns" (±99,999,999.99 valid, ±108 invalid) and "imports a credit card without an available credit, APR or annual fee its column cannot hold". Both failed first (a value at the bound was valid; the import raised PG::NumericValueOutOfRange).
  • Card form, under the "Balance is available credit" toggles: a note that Credit limit does not change the debt. Enable Banking's note says the debt still comes from the bank's limit or the Available credit field. Lunch Flow has its own note naming only the Available credit field, because LunchflowAccount::Processor reads nothing else.
  • Assistant::Function::GetAccounts#credit_card_terms reports credit_limit; its available_credit note says credit_limit is the limit.
  • en.yml: form label/placeholder, the two toggle notes, Overview title and "Using %{percent} of limit". Each key is also added to the locales that translate its neighbours: the form label and placeholder in 19, the toggle notes in 15, and the Overview title and usage line in 8 (de, es, fr, id, nb, nl, pt-BR, zh-CN). nb.yml keeps its CRLF endings. These are machine translations and want a native speaker's check.

Tests (red on the base, green here)

Red run on the base: 8 errors ActiveModel::UnknownAttributeError: unknown attribute 'credit_limit' for CreditCard and 1 failure (no form input).

test/models/credit_card_test.rb

  • 533.10 of 6,000 → 8.9 (truncation would give 8.8), credit_limit_money $6,000
  • 4,800 → 80; 6,600 → 110 (not capped)
  • overpaid −100 → 0
  • nil / 0 / −50 limit → nil, with the fixture's available_credit 5,000 present and not used (−50 is now assigned without saving, since it no longer saves)
  • "a credit limit cannot be negative": −0.01 invalid; 0 and nil valid. Before the fix: Expected true to be nil or false
  • "a credit limit must fit its column": 99,999,999.99 valid, 100,000,000 invalid. Before: Expected true to be nil or false

test/controllers/credit_cards_controller_test.rb: edit form renders credit_limit; PATCH saves 6,000 and locks it.

  • "a credit limit below zero or too large for the column is rejected": −50 and 100,000,000 each get 422 and the limit stays 6,000. Before: PG::NumericValueOutOfRange (a 500).
  • "the balance is available credit toggles say the credit limit field does not change the debt": each toggle shows its own note and not the other's, and the Lunch Flow note does not mention the bank. Before: the note was absent.
    test/controllers/accounts_controller_test.rb: Overview shows "Credit Limit", "$6,000.00", "Using 8.9% of limit" on a card with no statement cycle; no limit → no card. The $6,000.00 is matched inside the Credit Limit card, not anywhere on the page.
  • "credit card overview shows the credit limit beside a configured statement cycle": close day 25, due day 5; the limit card and the statement card both render.
    test/models/assistant/function/get_accounts_test.rb: terms[:credit_limit] 6,000; absent when unset.
    test/models/family/data_importer_test.rb: the limit round-trips through full export/import; "imports a credit card without a credit limit it would refuse" (−50 and 100,000,000 dropped, 99,999,999.99 kept, the card's other details still imported). Before: ActiveRecord::RecordInvalid, so the whole import rolled back.
    test/i18n_test.rb "card credit limit keys exist beside their siblings": each new key is present wherever its neighbour is translated. Before: de: credit_cards.form.credit_limit is missing.

Mutations (each alone, targeted files re-run)

#MutationCaught by
M1truncate instead of round8.9% test
M2no clamp for an overpaid cardoverpaid test
M3negative limit acceptedno-positive-limit test
M4cap at 100%boundaries test (110)
M5credit_limit not permittedform save test
M6dropped from importerround trip
M7dropped from GetAccountsGetAccounts test
M8Overview card shown without a limitno-limit Overview test
M9credit_limit_money reads available_credit3 tests
M10Overview card hidden when a statement cycle is setthe configured-cycle Overview test
M11greater_than_or_equal_to: 0 droppedcannot-be-negative test, controller rejection test
M12less_than: CREDIT_LIMIT_CEILING dropped, or the ceiling raised to 10**9fits-its-column test, controller rejection test, importer test
M13importer keeps an out-of-range limit, or its bounds moved (−100, 10**9, inclusive range)importer test
M14Lunch Flow shows the shared Enable Banking note, or the Enable Banking block shows the Lunch Flow notetoggle notes test
M15the Lunch Flow note mentions the banktoggle notes test
M16a translated key dropped from de.ymlI18n sibling-keys test

Verification

  • Full unit suite on af9634c94: 19502 runs, 0 failures, 8 errors, 9 skips; the 8 errors are ReleaseNotesWorkflowTest (no node in the container). After merging up/card-daily-interest's latest head (481dff72f), the targeted card files (credit_card, credit_cards_controller, accounts_controller, data_importer, i18n and the rest) ran 501 runs, 0 failures, 0 errors.
  • System accounts_test with the screenshot run: 14 runs, 0 failures.
  • bin/rubocop: no offenses. erb_lint: no errors. bin/brakeman: 0 warnings.

Screenshots

Taken with the system-test runner (seeded card, balance $5,100, limit $6,000). Before = base up/card-daily-interest.

ViewBeforeAfter
Overview (credit limit card) — light, desktopbeforeafter
Overview (credit limit card) — light, phonebeforeafter
Overview (credit limit card) — dark, desktopbeforeafter
Overview (credit limit card) — dark, phonebeforeafter
Edit form (credit limit field) — light, desktopbeforeafter
Edit form (credit limit field) — light, phonebeforeafter
Edit form (credit limit field) — dark, desktopbeforeafter
Edit form (credit limit field) — dark, phonebeforeafter

After the review fixes (af9634c94): the edit form, with the notes under the "Balance is available credit" toggles. The before column for these is the "after" form above.

ViewAfter
Edit form, toggle notes — light, desktopafter
Edit form, toggle notes — light, phoneafter
Edit form, toggle notes — dark, desktopafter
Edit form, toggle notes — dark, phoneafter

Migration notes

Adds one nullable column; existing rows get NULL (no limit, nothing shown). No backfill.

Blast radius

  • utilization_percent / credit_limit_money are new; their only callers are the Overview partial (and the chart slice above this one).
  • CREDIT_CARD_DETAIL_ATTRIBUTES is read by the importer for every card; the new key is nil-safe for older exports (no key → not set).
  • GetAccounts terms gain a key only when the limit is set (compact).
  • Writers of credit_limit: the card form (AccountableResource#update), Family::DataImporter (new cards only) and, from up/redbark-card-limit up, RedbarkAccount::Processor through enrich_attribute.
  • Providers: SimpleFIN, Lunch Flow and Enable Banking cards keep their limit in available_credit when the card is in available-credit mode (Brex writes its remaining credit there, not a limit). Their usage stays blank until Credit limit is filled in, and the two fields are not reconciled. Filling Credit limit from Enable Banking is a follow-up (filling the limit from Enable Banking), under #4356. The Lunch Flow toggle's older description, which predates this PR and still mentions a limit reported by the bank, is folded into a follow-up (filling the limit from Enable Banking) as well.
  • Overlap: open upstream PR we-promise/sure#5271 (annual fees) edits the same form, Overview, importer, locale and schema; textual conflicts only.

Summary by cubic

Adds a storeable credit limit to credit cards so the Overview tab can show how much of the limit the balance uses. The limit is nullable, editable on the card form, carried through export/import, and reported by the assistant's GetAccounts terms.

The Overview shows the limit and "Using X% of limit", rounded to one decimal; over-limit balances show their true share, overpaid cards show 0%, and nothing appears without a positive limit. available_credit, apr, and annual_fee now validate within their decimal column range, so values of ±10**8 no longer raise PG::NumericValueOutOfRange. A credit limit must be between 0 and below 100,000,000; the form returns 422 instead of a 500. The "balance is available credit" toggle notes now tell Lunch Flow and Enable Banking users to enter the card's total limit in the Available credit field when the provider reports none, since Credit limit never changes the debt.

  • The importer drops any card detail the card would refuse rather than failing the whole import: non-numeric, non-finite, or hex amounts, values that cast or validate out of range, statement days, and unknown interest bases.
  • New labels and toggle notes are translated wherever their neighboring keys are translated.

Summary by CodeRabbit

  • New Features
    • Add an optional credit limit to credit card accounts. The overview displays the limit and percentage used when available, even without a statement cycle.
    • Include credit limits in account details and data exports.
    • Add credit-card form guidance on how credit limits relate to displayed usage and debt calculations, with labels and guidance available in multiple languages.
  • Bug Fixes
    • Reject invalid credit limits and out-of-range card details during entry and import.