mirror of
https://github.com/we-promise/sure.git
synced 2026-05-30 15:59:02 +00:00
Brings the savings goals UI closer to the Claude Design reference shared
by the user. Changes:
- Sidebar nav label: "Savings goals" → "Savings".
- Status pill copy: "Behind" → "Behind pace" (matches Pill component
from GoalsCommon.jsx).
- Empty state rewritten with a large target icon, "No goals yet"
heading, and the descriptive body copy from the design.
Goal detail page (matches GoalDetail.jsx):
- New "← All goals" back link above the header.
- 2-column hero: ring card on the left (320px column), Projection card
on the right.
- Projection card uses a new D3 Stimulus controller
(`savings-goal-projection-chart`) that draws:
· saved area + line from goal creation → today (solid, primary)
· dashed projection segment from today → target date (yellow when
behind, green when on track)
· horizontal dashed target line with label
· today marker (vertical dashed line + dot)
Data shape comes from `SavingsGoal#projection_payload`.
- Card subtitle generates a contextual sentence ("At $X/mo you'll fall
short. Bump to $Y/mo to hit it on time." / "At your current pace
you'll reach this goal around Month YYYY." / "Goal reached. Nice
work.") with a strong tag highlighting the actionable figure.
- Stat row now shows Linked balance (sum across linked accounts) +
"N accounts" sub-caption instead of duplicate "Target date" stat.
New goal modal (matches the design images 2 + 3):
- DS::Dialog custom header: DS::FilledIcon target glyph + title + step
subtitle ("Step 1 of 2 · Goal details" / "Step 2 of 2 · Review &
start") that updates as the user advances.
- Connected stepper at top of body: numbered circles connected by a
bar, step-1 circle flips to ✓ when complete.
- Step 1 heading "What are you saving for?" + supporting copy.
- Name field paired with a target glyph affordance on its left.
- Target amount + Target date in a 2-col grid.
- Funding accounts list now grouped by account subtype with uppercase
section headers (CHECKING / SAVINGS / HSA / CD / MONEY MARKET /
OTHER), each row showing avatar + name + subtype + balance.
- Step 2 heading "Looks good?" + Review card (goal target + funding
accounts summary + suggested monthly = target/months_remaining), and
a disclosure for the optional initial contribution.
- Footer: "Cancel" left text-button (closes modal) / "Back" left text
when on step 2; "Continue →" or "Create goal →" right arrow button.
Demo generator: Depository accounts now set `subtype` ("checking" /
"savings") on the accountable so they group correctly in the modal.
Tests: all green, 35 runs in the savings suite, 92 assertions.
229 lines
7.1 KiB
Ruby
229 lines
7.1 KiB
Ruby
class SavingsGoal < ApplicationRecord
|
|
include AASM, Monetizable
|
|
|
|
COLORS = Category::COLORS
|
|
|
|
belongs_to :family
|
|
has_many :savings_goal_accounts, dependent: :destroy
|
|
has_many :linked_accounts, through: :savings_goal_accounts, source: :account
|
|
has_many :savings_contributions, dependent: :destroy
|
|
|
|
validates :name, presence: true, length: { maximum: 255 }
|
|
validates :target_amount, presence: true, numericality: { greater_than: 0 }
|
|
validates :currency, presence: true
|
|
validate :must_have_at_least_one_linked_account
|
|
validate :linked_accounts_must_be_depository
|
|
validate :linked_accounts_must_match_goal_currency
|
|
validate :linked_accounts_must_belong_to_family
|
|
validate :currency_locked_once_contributions_exist
|
|
|
|
monetize :target_amount
|
|
|
|
scope :alphabetically, -> { order(Arel.sql("LOWER(name) ASC")) }
|
|
scope :active_first, lambda {
|
|
order(Arel.sql("CASE state WHEN 'active' THEN 0 WHEN 'paused' THEN 1 WHEN 'completed' THEN 2 ELSE 3 END"))
|
|
}
|
|
scope :with_current_balance, lambda {
|
|
left_outer_joins(:savings_contributions)
|
|
.group(Arel.sql("savings_goals.id"))
|
|
.select(Arel.sql("savings_goals.*, COALESCE(SUM(savings_contributions.amount), 0) AS current_balance_total"))
|
|
}
|
|
|
|
# 63-bit Postgres advisory-lock key per family. Used by future auto-fund flows
|
|
# and any future per-family serialization of goal contributions.
|
|
def self.advisory_lock_key_for(family_id)
|
|
Digest::SHA1.hexdigest("savings_goals:family:#{family_id}").to_i(16) % (2**63)
|
|
end
|
|
|
|
aasm column: :state do
|
|
state :active, initial: true
|
|
state :paused
|
|
state :completed
|
|
state :archived
|
|
|
|
event :pause do
|
|
transitions from: :active, to: :paused
|
|
end
|
|
|
|
event :resume do
|
|
transitions from: :paused, to: :active
|
|
end
|
|
|
|
event :complete do
|
|
transitions from: [ :active, :paused ], to: :completed
|
|
end
|
|
|
|
event :archive do
|
|
transitions from: [ :active, :paused, :completed ], to: :archived
|
|
end
|
|
|
|
event :unarchive do
|
|
transitions from: :archived, to: :active
|
|
end
|
|
end
|
|
|
|
def current_balance
|
|
@current_balance ||= if attributes.key?("current_balance_total")
|
|
attributes["current_balance_total"] || 0
|
|
else
|
|
savings_contributions.sum(:amount)
|
|
end
|
|
end
|
|
|
|
def current_balance_money
|
|
@current_balance_money ||= Money.new(current_balance, currency)
|
|
end
|
|
|
|
def remaining_amount
|
|
@remaining_amount ||= [ target_amount - current_balance, 0 ].max
|
|
end
|
|
|
|
def remaining_amount_money
|
|
@remaining_amount_money ||= Money.new(remaining_amount, currency)
|
|
end
|
|
|
|
def progress_percent
|
|
return @progress_percent if defined?(@progress_percent)
|
|
|
|
@progress_percent = if completed?
|
|
100
|
|
elsif target_amount.to_d.zero?
|
|
0
|
|
else
|
|
[ ((current_balance.to_d / target_amount.to_d) * 100).round, 100 ].min
|
|
end
|
|
end
|
|
|
|
def months_remaining
|
|
return nil unless target_date
|
|
|
|
months = (target_date.year - Date.current.year) * 12 + (target_date.month - Date.current.month)
|
|
[ months, 0 ].max
|
|
end
|
|
|
|
def monthly_target_amount
|
|
return @monthly_target_amount if defined?(@monthly_target_amount)
|
|
|
|
@monthly_target_amount = if target_date.nil?
|
|
nil
|
|
elsif months_remaining.zero?
|
|
remaining_amount
|
|
else
|
|
(remaining_amount.to_d / months_remaining).ceil(2)
|
|
end
|
|
end
|
|
|
|
# Segment array consumed by the shared `donut-chart` Stimulus controller
|
|
# (see app/javascript/controllers/donut_chart_controller.js). Same shape
|
|
# as Budget#to_donut_segments_json: filled portion in goal color, unused
|
|
# remainder as the system "unallocated" fill.
|
|
def to_donut_segments_json
|
|
filled = current_balance.to_d
|
|
rem = remaining_amount.to_d
|
|
|
|
if filled.zero? && rem.zero?
|
|
return [ { color: "var(--budget-unallocated-fill)", amount: 1, id: "unused" } ]
|
|
end
|
|
|
|
segments = []
|
|
segments << { color: color.presence || "var(--color-blue-500)", amount: filled, id: "saved" } if filled.positive?
|
|
segments << { color: "var(--budget-unallocated-fill)", amount: rem, id: "unused" } if rem.positive?
|
|
segments
|
|
end
|
|
|
|
# Cumulative contributions series for the projection chart, sorted by
|
|
# date ascending. Consumed by the
|
|
# `savings-goal-projection-chart` Stimulus controller.
|
|
def projection_payload
|
|
sorted = savings_contributions.order(contributed_at: :asc).to_a
|
|
running = 0
|
|
saved_series = sorted.map do |c|
|
|
running += c.amount.to_d
|
|
{ date: c.contributed_at.to_s, value: running.to_f }
|
|
end
|
|
|
|
{
|
|
saved_series: saved_series,
|
|
start_date: created_at.to_date.to_s,
|
|
today: Date.current.to_s,
|
|
target_date: target_date&.to_s,
|
|
target_amount: target_amount.to_f,
|
|
current_amount: current_balance.to_f,
|
|
avg_monthly: average_monthly_contribution.to_f,
|
|
required_monthly: monthly_target_amount.to_f,
|
|
currency: currency,
|
|
status: status.to_s
|
|
}
|
|
end
|
|
|
|
# :reached → progress_percent >= 100
|
|
# :on_track → has target_date and current pace >= required monthly pace
|
|
# :behind → has target_date and current pace < required monthly pace
|
|
# :no_target_date → progress < 100 and target_date is nil
|
|
def status
|
|
return :reached if progress_percent >= 100
|
|
return :no_target_date if target_date.nil?
|
|
return :on_track if monthly_target_amount.to_d <= average_monthly_contribution.to_d
|
|
|
|
:behind
|
|
end
|
|
|
|
def average_monthly_contribution
|
|
return 0 if savings_contributions.empty?
|
|
|
|
first_at = savings_contributions.minimum(:contributed_at)
|
|
return current_balance if first_at.blank?
|
|
|
|
months = ((Date.current.year - first_at.year) * 12 + (Date.current.month - first_at.month)) + 1
|
|
months = 1 if months < 1
|
|
(current_balance.to_d / months).round(2)
|
|
end
|
|
|
|
private
|
|
def must_have_at_least_one_linked_account
|
|
return unless savings_goal_accounts.reject(&:marked_for_destruction?).empty?
|
|
|
|
errors.add(:base, :at_least_one_linked_account_required)
|
|
end
|
|
|
|
def linked_accounts_must_be_depository
|
|
offending = savings_goal_accounts.reject(&:marked_for_destruction?).reject do |sga|
|
|
sga.account&.depository?
|
|
end
|
|
return if offending.empty?
|
|
|
|
errors.add(:linked_accounts, :must_be_depository)
|
|
end
|
|
|
|
def linked_accounts_must_match_goal_currency
|
|
return if currency.blank?
|
|
|
|
mismatched = savings_goal_accounts.reject(&:marked_for_destruction?).reject do |sga|
|
|
sga.account.nil? || sga.account.currency == currency
|
|
end
|
|
return if mismatched.empty?
|
|
|
|
errors.add(:linked_accounts, :currency_mismatch)
|
|
end
|
|
|
|
def linked_accounts_must_belong_to_family
|
|
return if family.nil?
|
|
|
|
foreign = savings_goal_accounts.reject(&:marked_for_destruction?).reject do |sga|
|
|
sga.account.nil? || sga.account.family_id == family_id
|
|
end
|
|
return if foreign.empty?
|
|
|
|
errors.add(:linked_accounts, :must_belong_to_family)
|
|
end
|
|
|
|
# Once a goal has contributions, changing currency would orphan amounts
|
|
# in the old currency. Lock it.
|
|
def currency_locked_once_contributions_exist
|
|
return unless persisted? && currency_changed?
|
|
return unless savings_contributions.exists?
|
|
|
|
errors.add(:currency, :locked_after_contributions)
|
|
end
|
|
end
|