Files
sure/app/helpers/goals_helper.rb
T
buzzromainandClaude Opus 5 7176b4b521 fix(goals): stop the first goal being told about earmarks it has none of (#3216)
* fix(goals): stop the first goal being told about earmarks it has none of

Linking an account and leaving the amount blank shows "This account will
fund whatever is left after the other earmarks". On a first goal there are
no other earmarks — so the sentence points at something absent, and it is
where a user meets the word for the first time.

The row already carries `data-earmarked-by-others`, and it is zero in that
case. With the account to itself the hint now says so plainly, and
"earmark" appears only where earmarks actually exist — which lets the
context do the explaining instead of the vocabulary needing it.

Pinned server-side rather than in JS. The branch is a ternary; the failure
that matters is the form not handing over the second string, which does not
raise — the value reads as undefined and the line renders empty. There is
also a test that the two hints stay different, since identical copy would
leave the branch doing nothing and the first-time reader back where they
started.

Nothing runs `test/javascript` — no npm script, no CI step — so a test
there would have guarded nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016GTNba5qE5NwzaHzbp27ye

* fix(goals): tell an account claimed in full from one claimed by nobody

Review on #3216. The new "this account is yours alone" hint keyed off
`earmarked_by_other_goals`, which sums `allocated_amount` — and a
whole-account link carries nil, so it contributes zero. An account another
goal already claims in full therefore looked exactly like an unclaimed one.

The form promised the whole balance, and `whole_account_link_must_be_exclusive`
refused the blank allocation on submit. That is worse than the sentence
this PR set out to fix: it does not merely describe something absent, it
describes something the save then contradicts.

The row carries both readings now, because neither can be derived from the
other. `whole_account_claimed_by_other_goals?` asks the question the sum
cannot answer, and the two share the same row filter so the goal being
edited is excluded from both.

The two tests added here also move above the first `private`, same as on
`define_method`, which is public regardless — but they read as a mistake
sitting among the helpers.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016GTNba5qE5NwzaHzbp27ye

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 07:52:00 +02:00

56 lines
2.5 KiB
Ruby

module GoalsHelper
# Completing a goal releases the money it was holding, so both places that
# offer it — the header menu and the celebration panel — ask first, and ask
# the same question. Built here rather than twice inline: the two wordings
# drifting apart is how one of them ends up describing the wrong consequence.
def goal_complete_confirm(goal)
body = if goal.progress_percent < 100
t("goals.show.confirm_complete_body_short",
progress: goal.progress_percent,
saved: goal.current_balance_money.format(precision: 0),
target: goal.target_amount_money.format(precision: 0))
else
t("goals.show.confirm_complete_body")
end
CustomConfirm.new(
title: t("goals.show.confirm_complete_title"),
body: body,
btn_text: t("goals.show.confirm_complete_cta")
)
end
# Fixed earmarks on `account` held by goals OTHER than the one being
# edited, read from a pooled map loaded once per render
# (Goal.pooled_allocations_for) rather than per account — the form lists
# every fundable account, so a per-account query is a guaranteed N+1.
#
# Excluding the current goal is what makes the warning trustworthy:
# `Account#goal_earmarked_total` counts every goal, so reopening a goal
# that earmarks 5,000 on a 6,000 account would leave 1,000 of apparent
# headroom, and re-entering the same 5,000 would trip a message although
# nothing changed.
#
# Whole-balance links carry a nil allocated_amount and contribute zero:
# they reserve no fixed slice. That is the right reading here — what they
# do claim is guarded separately, at write time, by GoalAccount.
def earmarked_by_other_goals(account, pooled:, current_goal: nil)
other_goal_rows(account, pooled, current_goal).sum { |row| row[:allocated_amount].to_d }
end
# A whole-account link carries a nil allocation, so it contributes zero to
# the sum above — "nothing else claims this account" and "another goal
# claims all of it" look identical from that figure alone. They are not:
# the second refuses a blank allocation at the door
# (`GoalAccount#whole_account_link_must_be_exclusive`).
def whole_account_claimed_by_other_goals?(account, pooled:, current_goal: nil)
other_goal_rows(account, pooled, current_goal).any? { |row| row[:allocated_amount].nil? }
end
private
def other_goal_rows(account, pooled, current_goal)
(pooled[account.id] || [])
.reject { |row| current_goal&.persisted? && row[:goal_id] == current_goal.id }
end
end