feat(savings): add savings goals

Adds a standalone Savings goals feature: a piggy-bank style tracker that
lets a family set a target, link one or more Depository accounts as
funding sources, and log manual contributions over time. Supersedes #1569
(closed) — same intent, redesigned per reviewer + Discord feedback.

What this adds:

- New `/savings_goals` sidebar entry (piggy-bank icon) with index, show,
  state-filtered tabs (all/active/paused/completed/archived), and a
  2-step modal stepper for creation (Identity → Review).
- Multi-account funding via a `SavingsGoalAccount` join: a goal requires
  ≥1 linked Depository account (checking/savings/HSA/CD/money-market),
  and all linked accounts must share the goal's currency.
- Tracker balance model: goal balance = SUM(contributions.amount). No
  auto-flow from account balances. Contributions are pure logical
  records and don't move money between accounts.
- Manual contributions modal scoped to the goal's linked accounts.
  Initial contributions seeded at creation can't be deleted; manual
  ones can.
- AASM lifecycle: active / paused / completed / archived.
  Hard-delete only after archive.
- Status pills (On track / Behind / Reached / No date) derived from
  pace vs target_date.
- AI Assistant tool `create_savings_goal` lets the sidebar chat create
  a goal end-to-end from a natural-language prompt; soft errors carry
  the available-accounts list back to the LLM (mirrors the existing
  `import_bank_statement` pattern).
- Family-scoped throughout (`Current.family`-only access, account
  family-scoping enforced both in controllers and the AI tool).
- Demo data seed wires up 4 sample goals across the Depository accounts.

Intentionally out of scope (separate PRs / v1.1):

- Auto-fund from budget surplus + Sidekiq cron + budget-show card.
- Dashboard "Savings goals" widget.
- "Behind pace" projection chart on the detail page.
- `evaluate_savings_goal_feasibility` LLM tool (level-setting before
  create_savings_goal).
- Spend-less goals inside Budgets.
- Family-member-private goals (deferred investigation).
This commit is contained in:
Guillem Arias
2026-05-11 11:20:37 +02:00
parent 36960fe058
commit 77660d2ee4
49 changed files with 2419 additions and 5 deletions

View File

@@ -0,0 +1,58 @@
require "test_helper"
class SavingsContributionsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:family_admin)
@goal = savings_goals(:vacation_italy)
@depository = accounts(:depository)
ensure_tailwind_build
end
test "new renders the modal form" do
get new_savings_goal_contribution_url(@goal)
assert_response :success
end
test "create saves a manual contribution" do
assert_difference -> { @goal.savings_contributions.count } => 1 do
post savings_goal_contributions_url(@goal), params: {
savings_contribution: {
amount: "100",
contributed_at: Date.current.iso8601,
notes: ""
},
savings_contribution_account_id: @depository.id
}.merge(savings_contribution: { account_id: @depository.id, amount: "100", contributed_at: Date.current.iso8601 })
end
assert_redirected_to savings_goal_path(@goal)
contribution = @goal.savings_contributions.order(created_at: :desc).first
assert_equal "manual", contribution.source
assert_equal @depository, contribution.account
end
test "create rejects contribution from non-linked account" do
unlinked = Account.create!(family: @goal.family, accountable: Depository.new, name: "Unlinked", currency: "USD", balance: 100)
assert_no_difference "@goal.savings_contributions.count" do
post savings_goal_contributions_url(@goal), params: {
savings_contribution: { amount: "10", contributed_at: Date.current.iso8601, account_id: unlinked.id }
}
end
assert_response :unprocessable_entity
end
test "destroy manual contribution removes it" do
manual = savings_contributions(:vacation_italy_manual)
assert_difference "SavingsContribution.count", -1 do
delete savings_goal_contribution_url(@goal, manual)
end
end
test "destroy initial contribution is blocked" do
initial = savings_contributions(:vacation_italy_initial)
assert_no_difference "SavingsContribution.count" do
delete savings_goal_contribution_url(@goal, initial)
end
assert_redirected_to savings_goal_path(@goal)
end
end

View File

@@ -0,0 +1,146 @@
require "test_helper"
class SavingsGoalsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:family_admin)
@goal = savings_goals(:vacation_italy)
@depository = accounts(:depository)
@connected = accounts(:connected)
ensure_tailwind_build
end
test "index renders with active filter by default" do
get savings_goals_url
assert_response :success
assert_match(/Savings goals/i, response.body)
end
test "index honors state filter" do
get savings_goals_url(state: "paused")
assert_response :success
end
test "show renders the goal" do
get savings_goal_url(@goal)
assert_response :success
assert_match(@goal.name, response.body)
end
test "new renders the modal form" do
get new_savings_goal_url
assert_response :success
end
test "create persists a goal with linked accounts" do
assert_difference -> { SavingsGoal.count } => 1,
-> { SavingsGoalAccount.count } => 2 do
post savings_goals_url, params: {
savings_goal: {
name: "New goal",
target_amount: "1000",
target_date: 3.months.from_now.to_date.iso8601,
color: "#4da568",
account_ids: [ @depository.id, @connected.id ]
}
}
end
goal = SavingsGoal.order(created_at: :desc).first
assert_redirected_to savings_goal_path(goal)
end
test "create with initial contribution writes the contribution" do
assert_difference -> { SavingsContribution.count } => 1 do
post savings_goals_url, params: {
savings_goal: {
name: "Goal with initial",
target_amount: "1000",
color: "#4da568",
account_ids: [ @depository.id ],
initial_contribution_amount: "50",
initial_contribution_account_id: @depository.id
}
}
end
contribution = SavingsContribution.order(created_at: :desc).first
assert_equal "initial", contribution.source
assert_equal 50, contribution.amount.to_i
end
test "create rejects missing account_ids" do
assert_no_difference "SavingsGoal.count" do
post savings_goals_url, params: {
savings_goal: {
name: "Bad goal",
target_amount: "1000",
color: "#4da568"
}
}
end
assert_response :unprocessable_entity
end
test "create rejects foreign accounts" do
other_family = Family.create!(name: "Other", currency: "USD", locale: "en", country: "US", timezone: "UTC")
foreign = Account.create!(family: other_family, accountable: Depository.new, name: "Foreign", currency: "USD", balance: 100)
assert_no_difference "SavingsGoal.count" do
post savings_goals_url, params: {
savings_goal: {
name: "Foreign goal",
target_amount: "1000",
color: "#4da568",
account_ids: [ foreign.id ]
}
}
end
assert_response :unprocessable_entity
end
test "update modifies identity fields" do
patch savings_goal_url(@goal), params: { savings_goal: { name: "Renamed" } }
assert_redirected_to savings_goal_path(@goal)
assert_equal "Renamed", @goal.reload.name
end
test "pause/resume/complete/archive/unarchive flow" do
fresh = savings_goals(:emergency_fund)
patch pause_savings_goal_url(fresh)
assert fresh.reload.paused?
patch resume_savings_goal_url(fresh)
assert fresh.reload.active?
patch complete_savings_goal_url(fresh)
assert fresh.reload.completed?
patch archive_savings_goal_url(fresh)
assert fresh.reload.archived?
patch unarchive_savings_goal_url(fresh)
assert fresh.reload.active?
end
test "destroy on non-archived is rejected" do
assert_no_difference "SavingsGoal.count" do
delete savings_goal_url(@goal)
end
assert_redirected_to savings_goal_path(@goal)
end
test "destroy on archived deletes" do
@goal.archive!
assert_difference "SavingsGoal.count", -1 do
delete savings_goal_url(@goal)
end
assert_redirected_to savings_goals_path
end
test "another family's goal returns 404" do
other_family = Family.create!(name: "Other", currency: "USD", locale: "en", country: "US", timezone: "UTC")
other_account = Account.create!(family: other_family, accountable: Depository.new, name: "Foreign", currency: "USD", balance: 100)
other_goal = other_family.savings_goals.new(name: "Foreign goal", target_amount: 100, currency: "USD")
other_goal.savings_goal_accounts.build(account: other_account)
other_goal.save!
get savings_goal_url(other_goal)
assert_response :not_found
end
end