mirror of
https://github.com/we-promise/sure.git
synced 2026-08-12 11:10:20 +00:00
The show page repeated the same data multiple times across surfaces
that should each say one thing once. Per-screen counts before this
commit:
- Account % distribution: 4 places (distribution bar + dot-legend
strip + 5-bar weight pill + % column)
- Current balance: 3 places (ring, funding heading total, ring
"of $X" subline)
- Target amount: 3 places (header, ring subline, catch-up body)
- Target date: 3 places (header, catch-up body, chart axis)
- Pace: 2 places (catch-up body, projection subtitle)
- ".00" cents: every monetary string
This pass:
- Funding widget drops the dot-legend strip (color/name/% triplet
redundant with the distribution bar's color + the per-row avatar
color) and the 5-bar weight pill (rendered as "1-of-5 sliver" for
low-weight accounts — read as a glitch; the % number next to it
covered the same fact). Row grid shrinks from 5 to 4 columns.
- Funding section heading drops `· $187,031` — the ring card
already carries the total balance.
- Catch-up alert reframes:
Title was "Save $26,621/mo to stay on track" (the *full* required
rate, with the misleading "stay on track" while the pill says
"Behind"). Now "Save $20,002/mo more to catch up" using
`catch_up_delta_money` — the user's actual delta over current
pace.
Body collapsed from two with-date / no-date variants to a single
"Current pace $X/mo · required $Y/mo to hit your target." Drops
the target date duplication since the header already says it.
Pledge CTA pre-fills with the *delta*, not the full required —
so accepting it once funds the gap instead of stacking the full
required rate on top of existing pace.
Secondary link "Or adjust your target" → "Adjust target instead"
(less defeatist framing).
- Projection chart subtitle "At $X/mo you'll miss your target date."
drops the pace duplication (catch-up above already states pace).
New: "Falling short at current pace." Diagnostic only.
- All money on the show page uses `format(precision: 0)`. The ".00"
cents added no information at goal-tracking scale.
- Header `Record pledge` demotes to `outline` variant when status is
`:behind` — the catch-up alert below owns the primary action.
One primary action per surface.
Also adjacent fixes:
- Funding widget keys avatar / distribution color off `account.id`,
not `account.name`. Renaming an account no longer recolors it
retroactively; two accounts with name-hash collisions no longer
share a color (Ruby idiom audit finding).
- `Goals::StatusPillComponent`: add `:completed` variant with
`circle-check-big` icon. `Goal#display_status` now returns
`:completed` when `goal.completed?` so a manually-completed
goal (e.g. user stopped at 80%) reads "Completed" rather than
falling through to `:on_track`/`:behind` and lying on the index.
Locale: drop `body_with_date` (folded into `body`),
`projection.behind` no longer carries interpolation args (caller
doesn't pass them either), `projection.no_pace` plain-language
rewrite ("inflow" → "deposits"), add `status.completed: "Completed"`.
349 lines
11 KiB
Ruby
349 lines
11 KiB
Ruby
class Goal < ApplicationRecord
|
|
include AASM, Monetizable
|
|
|
|
COLORS = Category::COLORS
|
|
ICONS = Category.icon_codes
|
|
|
|
validates :icon, inclusion: { in: ICONS, allow_nil: true }
|
|
|
|
belongs_to :family
|
|
has_many :goal_accounts, dependent: :destroy
|
|
has_many :linked_accounts, through: :goal_accounts, source: :account
|
|
has_many :goal_pledges, dependent: :destroy
|
|
has_many :open_pledges,
|
|
-> { where(status: "open").where("expires_at >= ?", Time.current) },
|
|
class_name: "GoalPledge"
|
|
|
|
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_linked
|
|
|
|
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"))
|
|
}
|
|
|
|
def self.advisory_lock_key_for(family_id)
|
|
Digest::SHA1.hexdigest("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
|
|
|
|
# Balance is the live balance of every linked depository account.
|
|
# v1: single linked account in practice. v1.1+: minus other goals' allocations
|
|
# via the upcoming GoalBacking query.
|
|
def current_balance
|
|
@current_balance ||= linked_accounts.sum { |a| a.balance.to_d }
|
|
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
|
|
|
|
# 90-day rolling monthly pace: net inflow into linked accounts divided by
|
|
# three months. Transfers between linked accounts net to zero — both sides
|
|
# land inside this account set. Transfers from outside (e.g. checking →
|
|
# linked savings) net positive, which is the behaviour we want: the user
|
|
# taps "I just transferred…", the transfer arrives, balance goes up,
|
|
# pace goes up, status flips off "behind". Excludes user-flagged-excluded
|
|
# entries. Entry amount sign convention in Sure: inflow is negative.
|
|
def pace
|
|
return @pace if defined?(@pace)
|
|
|
|
@pace = if linked_accounts.empty?
|
|
0
|
|
else
|
|
account_ids = linked_accounts.map(&:id)
|
|
net = Entry
|
|
.joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")
|
|
.where(account_id: account_ids, date: 90.days.ago.to_date..Date.current)
|
|
.where(excluded: false)
|
|
.sum(:amount)
|
|
(-net.to_d / 3).round(2)
|
|
end
|
|
end
|
|
|
|
def pace_money
|
|
@pace_money ||= Money.new(pace, currency)
|
|
end
|
|
|
|
# Months of cash on hand at current pace (open-ended goals).
|
|
def months_of_runway
|
|
return nil if target_date.present?
|
|
return nil if pace.zero? || pace.negative?
|
|
|
|
(current_balance.to_d / pace.to_d).round(1)
|
|
end
|
|
|
|
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
|
|
|
|
# 90-day balance trajectory of linked accounts. Used by the projection chart
|
|
# to render the saved-to-date line. Returns an empty series when the linked
|
|
# account lacks ≥30 days of history.
|
|
def projection_payload
|
|
series_values = balance_series_values
|
|
saved_series = series_values.map { |v| { date: v.date.to_s, value: v.value.amount.to_f } }
|
|
|
|
earliest = series_values.first&.date || created_at.to_date
|
|
|
|
{
|
|
saved_series: saved_series,
|
|
start_date: earliest.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: pace.to_f,
|
|
required_monthly: monthly_target_amount.to_f,
|
|
currency: currency,
|
|
status: status.to_s,
|
|
pending_pledge_amount: open_pledges.sum(:amount).to_f
|
|
}
|
|
end
|
|
|
|
def display_status
|
|
return @display_status if defined?(@display_status)
|
|
|
|
@display_status = if archived?
|
|
:archived
|
|
elsif paused?
|
|
:paused
|
|
elsif completed?
|
|
:completed
|
|
else
|
|
status
|
|
end
|
|
end
|
|
|
|
# :reached → progress_percent >= 100
|
|
# :on_track → has target_date and pace >= required monthly
|
|
# :behind → has target_date and pace < required monthly
|
|
# :no_target_date → open-ended
|
|
def status
|
|
return @status if defined?(@status)
|
|
|
|
@status = if progress_percent >= 100
|
|
:reached
|
|
elsif target_date.nil?
|
|
:no_target_date
|
|
elsif monthly_target_amount.to_d <= pace.to_d
|
|
:on_track
|
|
else
|
|
:behind
|
|
end
|
|
end
|
|
|
|
# Date of the most-recently-matched pledge's underlying entry. Used by the
|
|
# show header to display "Last saved N days ago". Anchoring on the entry's
|
|
# date keeps the readout stable under sync re-runs (which would bump
|
|
# pledge#updated_at). Returns nil if no pledge has resolved yet.
|
|
def last_matched_pledge_at
|
|
return @last_matched_pledge_at if defined?(@last_matched_pledge_at)
|
|
|
|
@last_matched_pledge_at = Entry
|
|
.where(entryable_type: "Transaction")
|
|
.joins("INNER JOIN goal_pledges ON goal_pledges.matched_transaction_id = entries.entryable_id")
|
|
.where(goal_pledges: { goal_id: id, status: "matched" })
|
|
.maximum(:date)
|
|
end
|
|
|
|
def last_matched_pledge_days_ago
|
|
last = last_matched_pledge_at
|
|
return nil if last.nil?
|
|
|
|
(Date.current - last).to_i
|
|
end
|
|
|
|
# True when any linked account is wired to a live sync provider (Plaid,
|
|
# SimpleFIN, or any AccountProvider — Brex, Enable Banking, IBKR, Kraken,
|
|
# SnapTrade, Lunchflow). Drives the pledge-create copy: connected accounts
|
|
# get the "I just transferred…" path; manual-only accounts get "I just
|
|
# saved…" so users aren't told to wait for a sync that won't happen.
|
|
def any_connected_account?
|
|
linked_accounts.any? { |a| !a.manual? }
|
|
end
|
|
|
|
# "I just transferred" for bank-connected accounts, "I just saved" for manual-only.
|
|
def pledge_action_label_key
|
|
any_connected_account? ? "goals.show.pledge_just_transferred" : "goals.show.pledge_just_saved"
|
|
end
|
|
|
|
# Single source of truth for the projection-chart subtitle / chart-aria
|
|
# description. Used to live inline in show.html.erb as a 17-line if/elsif
|
|
# chain. Returns an `html_safe` string when it picks the `_html` variant.
|
|
def projection_summary
|
|
return @projection_summary if defined?(@projection_summary)
|
|
|
|
@projection_summary =
|
|
if completed? || progress_percent >= 100
|
|
I18n.t("goals.show.projection.reached")
|
|
elsif target_date.nil?
|
|
I18n.t("goals.show.projection.no_target_date")
|
|
elsif monthly_target_amount && pace.to_d < monthly_target_amount.to_d
|
|
I18n.t("goals.show.projection.behind")
|
|
elsif pace.positive?
|
|
months = (remaining_amount.to_d / pace.to_d).ceil
|
|
I18n.t(
|
|
"goals.show.projection.on_track_html",
|
|
date: (Date.current >> months.to_i).strftime("%b %Y")
|
|
)
|
|
else
|
|
I18n.t("goals.show.projection.no_pace")
|
|
end
|
|
end
|
|
|
|
# Monthly extra needed beyond the current pace to hit the target on time.
|
|
# Clamps at zero — never asks the user to "make up" a deficit they're
|
|
# already ahead of.
|
|
def catch_up_delta_money
|
|
return Money.new(0, currency) if monthly_target_amount.nil?
|
|
|
|
delta = [ monthly_target_amount.to_d - pace.to_d, 0 ].max
|
|
Money.new(delta, currency)
|
|
end
|
|
|
|
private
|
|
def balance_series_values
|
|
return [] if linked_accounts.empty?
|
|
|
|
Balance::ChartSeriesBuilder.new(
|
|
account_ids: linked_accounts.map(&:id),
|
|
currency: currency,
|
|
period: Period.last_90_days
|
|
).balance_series.values
|
|
rescue StandardError => e
|
|
# Degrade gracefully (chart drops to target-line-only) but surface
|
|
# the failure — silent fallbacks here masked real Builder bugs.
|
|
Rails.logger.error("Goal##{id} balance series failed: #{e.class}: #{e.message}")
|
|
Sentry.capture_exception(e) if defined?(Sentry)
|
|
[]
|
|
end
|
|
|
|
def must_have_at_least_one_linked_account
|
|
return unless goal_accounts.reject(&:marked_for_destruction?).empty?
|
|
|
|
errors.add(:base, :at_least_one_linked_account_required)
|
|
end
|
|
|
|
def linked_accounts_must_be_depository
|
|
offending = 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 = 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 = 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
|
|
|
|
def currency_locked_once_linked
|
|
return unless persisted? && currency_changed?
|
|
return unless goal_accounts.where.not(id: nil).exists?
|
|
|
|
errors.add(:currency, :locked_after_linked)
|
|
end
|
|
end
|