Files
sure/app/components/savings/goal_card_component.rb
Guillem Arias 3e05ea8670 feat(savings_goals): goal card pace + status-driven footer
Each card now answers "what's my next move" without clicking into the
detail page. Under the amount/target row, a pace line shows actual avg
contributions vs the monthly target. The footer (previously "$X left")
switches by status:

- behind  → "Save $Y/mo to catch up"
- on_track → "Last contribution Nd ago" (or "today" / "No contributions yet")
- reached / completed → "Goal reached"
- no_target_date → "No deadline set"
- paused → "Paused"

Add SavingsGoal#last_contribution_at and #last_contribution_days_ago.
Both these methods and average_monthly_contribution now respect a loaded
:savings_contributions association so the index page doesn't N+1.
Controller eager-loads :savings_contributions + :linked_accounts.
2026-05-11 14:20:40 +02:00

94 lines
2.6 KiB
Ruby

class Savings::GoalCardComponent < 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(--text-primary)"
else "var(--text-subdued)"
end
end
def linked_accounts
@linked_accounts ||= goal.linked_accounts.to_a
end
def linked_accounts_count_label
I18n.t("savings_goals.goal_card.accounts", count: linked_accounts.size)
end
def secondary_line
if goal.completed?
I18n.t("savings_goals.goal_card.completed")
elsif goal.target_date.nil?
I18n.t("savings_goals.goal_card.no_target_date")
else
days = (goal.target_date - Date.current).to_i
if days >= 0
I18n.t("savings_goals.goal_card.days_left_by", count: days, date: I18n.l(goal.target_date, format: :long))
else
I18n.t("savings_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.paused? || goal.completed? || goal.status == :reached
avg = Money.new(goal.average_monthly_contribution, goal.currency).format
target = goal.monthly_target_amount ? Money.new(goal.monthly_target_amount, goal.currency).format : nil
if target
I18n.t("savings_goals.goal_card.pace_with_target", avg: avg, target: target)
else
I18n.t("savings_goals.goal_card.pace_no_target", avg: avg)
end
end
def footer_line
if goal.paused?
I18n.t("savings_goals.goal_card.footer_paused")
elsif goal.completed? || goal.status == :reached
I18n.t("savings_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("savings_goals.goal_card.footer_catch_up", amount: catch_up)
elsif goal.status == :no_target_date
I18n.t("savings_goals.goal_card.footer_no_deadline")
else
days = goal.last_contribution_days_ago
if days.nil?
I18n.t("savings_goals.goal_card.footer_no_contributions")
elsif days.zero?
I18n.t("savings_goals.goal_card.footer_last_today")
else
I18n.t("savings_goals.goal_card.footer_last_days", count: days)
end
end
end
end