Files
sure/app/components/goals/card_component.rb
Guillem Arias 88032ce020 feat(goals): v2 architecture — drop ledger, derive balance, add pledge
Reshape the goals feature to live on top of linked-account balances.
A goal's balance is now the live balance of every depository account
linked to it — no parallel ledger, no "log a contribution" step.

The "Add contribution" affordance is replaced by a 7-day GoalPledge
(kind: transfer | manual_save). GoalPledge::Reconciler matches incoming
Transactions (via Account::ProviderImportAdapter) and Valuations (via
Account::ReconciliationManager) against open pledges within ±5 days,
±$0.50, or ±1% — single hook covers every provider (Plaid, SimpleFIN,
Lunchflow, Enable Banking, Brex, IBKR, Kraken, SnapTrade) plus manual
balance edits. A 15-minute Sidekiq cron sweeps expired pledges.

Goal model: balance derived from linked_accounts.sum(&:balance), new
pace (90-day net non-transfer inflow), months_of_runway,
last_matched_pledge_*, pledge_action_label_key (the "I just
transferred…" vs "I just saved…" verb switch).

UI:
- Index gets a 3-card KPI strip (Contributed last 30d / Needs this
  month / On track) plus a pending-pledges callout.
- Show page swaps the "Add contribution" CTA for the pledge modal,
  replaces the contribution list with a pending-pledge banner, and
  rebuilds the funding widget into per-account rows with a 12-bucket
  weekly sparkline and last-30 inflow.
- Projection chart adds a required-line (dashed light from
  today → target) and a translucent pending-pledge bump at today's X.

Schema (3 migrations):
1. goal_pledges table with PG enums (goal_pledge_kind, goal_pledge_status),
   open-by-expiry index, and unique-when-not-null matched_transaction_id.
2. Drop goal_contributions.
3. Partial unique index on
   transactions ((extra -> 'goal' ->> 'pledge_id')) built CONCURRENTLY
   so it doesn't block prod.

After pulling: run bin/rails db:migrate, then commit the schema.rb sync
separately (or let CI regenerate).

Deferred to v1.1: allocation columns, contention/archived banners,
"why is this behind?" diagnostic, reallocate flow, refresh-sync +
Plaid throttle, unallocated-cash chip, joint-account approval,
goal_activities log, polymorphic matched_entry_id/type for manual
pledge audit.
2026-05-14 16:07:14 +02:00

107 lines
3.1 KiB
Ruby

class Goals::CardComponent < ApplicationComponent
RING_SIZE = 64
RING_STROKE = 6
def initialize(goal:)
@goal = goal
end
attr_reader :goal
def progress_percent
goal.progress_percent
end
def ring_color
case goal.status
when :reached then "var(--color-green-600)"
when :behind then "var(--color-yellow-600)"
when :on_track then "var(--color-green-500)"
else "var(--color-gray-400)"
end
end
def linked_accounts
@linked_accounts ||= goal.linked_accounts.to_a
end
def linked_accounts_count_label
I18n.t("goals.goal_card.accounts", count: linked_accounts.size)
end
# Single screen-reader sentence for the card's title <a> aria-label.
# Without this, the whole-card link would inherit every nested text node
# as its accessible name (>15 strings on a typical card).
def aria_label
status_text = I18n.t("goals.status.#{goal.display_status}")
progress_text = I18n.t("goals.goal_card.aria_progress",
percent: progress_percent,
target: goal.target_amount_money.format)
[ goal.name, status_text, progress_text ].join(", ")
end
def secondary_line
if goal.completed?
I18n.t("goals.goal_card.completed")
elsif goal.target_date.nil?
I18n.t("goals.goal_card.no_target_date")
else
days = (goal.target_date - Date.current).to_i
if days >= 0
I18n.t("goals.goal_card.days_left_by", count: days, date: I18n.l(goal.target_date, format: :long))
else
I18n.t("goals.goal_card.past_due")
end
end
end
def ring_circumference
@ring_circumference ||= 2 * Math::PI * ring_radius
end
def ring_radius
@ring_radius ||= (RING_SIZE - RING_STROKE) / 2.0
end
def ring_offset
pct = [ [ progress_percent.to_i, 0 ].max, 100 ].min
ring_circumference * (1 - pct / 100.0)
end
def pace_line
return nil if goal.archived? || goal.paused? || goal.completed? || goal.status == :reached
avg = goal.pace_money.format
target = goal.monthly_target_amount ? Money.new(goal.monthly_target_amount, goal.currency).format : nil
if target
I18n.t("goals.goal_card.pace_with_target", avg: avg, target: target)
else
I18n.t("goals.goal_card.pace_no_target", avg: avg)
end
end
def footer_line
if goal.archived?
I18n.t("goals.goal_card.footer_archived")
elsif goal.paused?
I18n.t("goals.goal_card.footer_paused")
elsif goal.completed? || goal.status == :reached
I18n.t("goals.goal_card.footer_reached")
elsif goal.status == :behind && goal.monthly_target_amount
catch_up = Money.new(goal.monthly_target_amount, goal.currency).format
I18n.t("goals.goal_card.footer_catch_up", amount: catch_up)
elsif goal.status == :no_target_date
I18n.t("goals.goal_card.footer_no_deadline")
else
days = goal.last_matched_pledge_days_ago
if days.nil?
I18n.t("goals.goal_card.footer_no_pledges")
elsif days.zero?
I18n.t("goals.goal_card.footer_last_today")
else
I18n.t("goals.goal_card.footer_last_days", count: days)
end
end
end
end