The personal finance app for everyone (by everyone)
Ruby
<%# locals: (goal:, linkable_accounts:, currently_linked_account_ids: [], pooled_allocations: {}) %>
<%# goal-kind lives on this wrapper, not on the kind selector: its
dateField target is a sibling of the radios, and a controller only sees
targets inside its own element. Scoped to the selector, dateFieldTargets
came back empty and picking "Reserve to maintain" never hid the date. %>
<div data-controller="goal-form goal-kind"
data-goal-form-currency-value="<%= goal.currency.presence || Current.family.primary_currency_code %>"
data-goal-form-require-account-value="<%= !goal.persisted? %>"
data-goal-form-suggested-with-date-value="<%= t("goals.form.suggested_with_date") %>"
data-goal-form-suggested-no-date-value="<%= t("goals.form.suggested_no_date") %>"
data-goal-kind-amount-label-value="<%= t("goals.form.fields.target_amount") %>"
data-goal-kind-balance-label-value="<%= t("goals.form.fields.target_balance") %>"
data-goal-kind-derivable-value="<%= goal.months_target_derivable? %>">
<%= styled_form_with model: goal,
url: goal.persisted? ? goal_path(goal) : goals_path,
method: goal.persisted? ? :patch : :post,
class: "space-y-5",
data: { action: "submit->goal-form#validateOnSubmit" } do |f| %>
<div class="flex justify-center">
<%= render "color_picker", form: f, colors: Goal::COLORS, icons: Goal::ICONS %>
</div>
<%# Kind first: it changes what the rest of the form means. A reserve has
no deadline — target_date is hidden rather than disabled so it cannot
be submitted with a value that would then drive a pace the goal does
not have. %>
<div class="grid grid-cols-2 gap-2">
<% Goal::KINDS.each do |kind| %>
<%= render "goals/radio_card", form: f, attribute: :kind, value: kind,
label: t("goals.form.kinds.#{kind}.label"),
hint: t("goals.form.kinds.#{kind}.hint"),
radio_options: { data: { goal_kind_target: "radio", action: "change->goal-kind#refresh" } } %>
<% end %>
</div>
<div>
<%= f.text_field :name,
placeholder: t("goals.form.fields.name_placeholder"),
autofocus: true,
required: true,
label: t("goals.form.fields.name"),
data: { goal_form_target: "nameInput", action: "input->goal-form#nameChanged" } %>
<p class="<%= "hidden" unless goal.errors[:name].any? %> mt-1.5 text-xs text-destructive" data-goal-form-target="nameError"><%= t("goals.form.errors.name_required") %></p>
</div>
<%# Only meaningful for a reserve, and only in months mode — the Stimulus
controller shows it accordingly. The amount field stays visible either
way: in months mode it shows what the job last computed, which is the
figure the user is actually saving against — read-only there, because
in that mode it is derived rather than chosen. %>
<div data-goal-kind-target="modeField" class="hidden">
<%= f.select :target_mode,
Goal::TARGET_MODES.map { |mode| [ t("goals.form.target_modes.#{mode}"), mode ] },
{ label: t("goals.form.fields.target_mode") },
data: { goal_kind_target: "modeSelect", action: "change->goal-kind#refresh" } %>
</div>
<div data-goal-kind-target="monthsField" class="hidden">
<%= f.number_field :target_months,
label: t("goals.form.fields.target_months"),
min: 1,
step: 1,
placeholder: "6" %>
<p class="mt-1 text-xs text-secondary"><%= t("goals.form.fields.target_months_hint") %></p>
</div>
<%# Flex, not a two-column grid. A reserve hides the date, and a hidden grid
cell still holds its column, which left the amount at half the width of
the months field above it. Flex children share whatever is visible. %>
<div class="flex gap-3 items-start">
<div data-goal-kind-target="amountField" class="flex-1 min-w-0">
<%= f.money_field :target_amount,
label: t("goals.form.fields.target_amount"),
hide_currency: true,
required: true,
amount_data: { goal_form_target: "amountInput", action: "input->goal-form#suggestedChanged" } %>
<p class="<%= "hidden" unless goal.errors[:target_amount].any? %> mt-1.5 text-xs text-destructive" data-goal-form-target="amountError"><%= t("goals.form.errors.amount_required") %></p>
</div>
<%# In months mode the balance is worked out, not typed. A read-only
input still reads as something to fill in, sitting beside the months
that are the actual question — so it is shown as what it is. Only
when there is spending to work it out from, though: with none, the
controller leaves the field above in place and this stays hidden. %>
<div data-goal-kind-target="amountDerived" class="hidden flex-1 min-w-0">
<p class="form-field__label"><%= t("goals.form.fields.target_balance") %></p>
<p class="mt-1 text-lg font-medium text-primary tabular-nums privacy-sensitive">
<% if goal.target_amount.to_d.positive? %>
<%= goal.target_amount_money.format(precision: 0) %>
<% else %>
<span class="text-subdued text-sm font-normal"><%= t("goals.form.fields.target_balance_pending") %></span>
<% end %>
</p>
<p class="mt-1 text-xs text-subdued"><%= t("goals.form.fields.target_balance_hint") %></p>
</div>
<div data-goal-kind-target="dateField" class="flex-1 min-w-0">
<%= f.date_field :target_date,
label: t("goals.form.fields.target_date"),
data: { goal_form_target: "dateInput", action: "input->goal-form#suggestedChanged" } %>
</div>
</div>
<p class="text-xs text-secondary italic tabular-nums privacy-sensitive hidden" data-goal-form-target="suggested"></p>
<div>
<div class="mb-2">
<span class="block text-sm font-medium text-primary"><%= t("goals.form.fields.funding_accounts") %></span>
<p class="text-xs text-secondary mt-0.5"><%= t("goals.form.fields.funding_accounts_hint") %></p>
<p class="text-xs text-subdued mt-0.5"><%= t("goals.form.fields.earmark_hint") %></p>
<% goal_currency = goal.currency.presence || Current.family.primary_currency_code %>
<% if (linkable_accounts.map(&:currency) | [ goal_currency ]).size > 1 %>
<p class="text-xs text-secondary mt-0.5"><%= t("goals.form.fields.currency_conversion_hint", currency: goal_currency) %></p>
<% end %>
</div>
<%# Its own controller, not goal-form: that one is already at 10 targets
against the 7 the project guidelines suggest, and this needs none of
its state. Three targets here. %>
<div class="bg-container-inset rounded-lg p-1"
data-controller="goal-earmark"
data-goal-earmark-currency-value="<%= goal_currency %>"
<%# The APPLICATION locale, not the browser's. `Intl.NumberFormat`
with `undefined` lets the browser pick, so a French user on an
English-locale browser read separators and symbol placement that
matched nothing else on the page. The amounts themselves cannot
be formatted server-side — they change with every keystroke — so
the server decides the locale and the client applies it. %>
data-goal-earmark-locale-value="<%= I18n.locale %>"
data-goal-earmark-whole-balance-value="<%= t("goals.form.earmark.whole_balance") %>"
data-goal-earmark-whole-balance-alone-value="<%= t("goals.form.earmark.whole_balance_alone") %>"
data-goal-earmark-prorata-value="<%= t("goals.form.earmark.prorata") %>"
data-goal-earmark-headroom-value="<%= t("goals.form.earmark.headroom") %>"
data-goal-earmark-converted-value="<%= t("goals.form.earmark.converted") %>">
<% linked_allocation_by_account = goal.goal_accounts.index_by(&:account_id) %>
<% grouped = linkable_accounts.group_by { |a| a.subtype.to_s.presence || "other" } %>
<% grouped.each_with_index do |(subtype, accts), group_idx| %>
<div class="px-3 py-2 text-[11px] font-medium uppercase tracking-wide text-secondary"><%= t("goals.form.subtypes.#{subtype}", default: subtype.titleize) %></div>
<div class="bg-container rounded-md <%= "mb-1" if group_idx < grouped.size - 1 %>">
<% accts.each_with_index do |account, idx| %>
<% linked_ga = linked_allocation_by_account[account.id] %>
<div class="px-3 py-2.5 hover:bg-surface-hover <%= "border-t border-subdued" if idx > 0 %>"
data-balance="<%= account.balance.to_d %>"
data-currency="<%= account.currency %>"
data-rate="<%= earmark_conversion_rate(goal, account, goal_currency) %>"
data-earmarked-by-others="<%= earmarked_by_other_goals(account, pooled: pooled_allocations, current_goal: goal) %>"
data-whole-account-claimed="<%= whole_account_claimed_by_other_goals?(account, pooled: pooled_allocations, current_goal: goal) %>">
<div class="flex items-center gap-3">
<label class="flex items-center gap-3 flex-1 min-w-0 cursor-pointer">
<%= check_box_tag "goal[account_ids][]",
account.id,
currently_linked_account_ids.include?(account.id.to_s),
id: "goal_account_ids_#{account.id}",
class: "checkbox checkbox--light shrink-0",
data: {
goal_form_target: "linkedAccountCheckbox",
goal_earmark_target: "checkbox",
action: "change->goal-form#linkedAccountChanged change->goal-earmark#refresh"
} %>
<%= render Goals::AvatarComponent.new(name: account.name, color: Goals::AvatarComponent.color_for(account.name), size: "md") %>
<div class="min-w-0">
<p class="text-sm font-medium text-primary truncate"><%= account.name %></p>
<% row_subtype = account.subtype.to_s.presence || subtype %>
<p class="text-xs text-secondary tabular-nums privacy-sensitive"><%= t("goals.form.subtypes.#{row_subtype}", default: row_subtype.titleize) %> · <%= Money.new(account.balance, account.currency).format %></p>
</div>
</label>
<%= f.fields_for :allocations do |af| %>
<%= af.money_field account.id,
label: t("goals.form.fields.earmark_for", account: account.name),
label_class: "sr-only",
placeholder: t("goals.form.fields.whole_balance"),
value: linked_ga&.allocated_amount,
currency_value_override: account.currency,
hide_currency: true,
min: 0,
container_class: "shrink-0 w-48",
input_class: "tabular-nums privacy-sensitive privacy-sensitive-interactive",
amount_data: { goal_earmark_target: "allocationInput", action: "input->goal-earmark#refresh" } %>
<% end %>
</div>
<%# Never text-destructive: none of this is an error. A goal
in progress legitimately claims more than its account
holds — that is what saving toward something looks like. %>
<p class="mt-1 text-xs text-secondary privacy-sensitive hidden" data-goal-earmark-target="warning"></p>
</div>
<% end %>
</div>
<% end %>
</div>
<p class="<%= "hidden" unless goal.errors[:base].any? %> mt-1.5 text-xs text-destructive" data-goal-form-target="accountsError"><%= t("goals.form.errors.accounts_required") %></p>
<%# Server-side only: GoalAccount refuses a second whole-balance link on an
account another goal already claims in full. There is no client-side
equivalent to mirror, so this renders straight from the record rather
than through a goal-form target (the controller is already at 10). %>
<% goal.errors.where(:"goal_accounts.base").each do |error| %>
<p class="mt-1.5 text-xs text-destructive"><%= error.message %></p>
<% end %>
</div>
<%# What progress counts. Only differs for an account that moves with the
market, but a plain-cash goal reads the same either way, so the choice
is always offered rather than revealed by the accounts ticked above.
A completed goal's basis is frozen with its snapshot, so there the
choice is shown disabled; disabled radios are not submitted, so an
ordinary edit still saves. Checked against the stored basis, so a
refused switch re-renders showing the basis that was kept; an unknown
basis falls back to it too, rather than leaving both radios blank. %>
<% basis_locked = goal.progress_basis_locked? %>
<% shown_basis = (basis_locked || Goal::PROGRESS_BASES.exclude?(goal.progress_basis)) ? goal.progress_basis_in_database : goal.progress_basis %>
<fieldset>
<legend class="block text-sm font-medium text-primary"><%= t("goals.form.fields.progress_basis") %></legend>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-2 mt-1.5">
<% Goal::PROGRESS_BASES.each do |basis| %>
<%= render "goals/radio_card", form: f, attribute: :progress_basis, value: basis,
label: t("goals.form.progress_bases.#{basis}.label"),
hint: t("goals.form.progress_bases.#{basis}.hint"),
radio_options: { checked: basis == shown_basis },
disabled: basis_locked %>
<% end %>
</div>
<% if basis_locked %>
<p class="mt-1.5 text-xs text-secondary"><%= t("goals.form.progress_basis_locked") %></p>
<% end %>
<% goal.errors.messages_for(:progress_basis).each do |message| %>
<p class="mt-1.5 text-xs text-destructive"><%= message %></p>
<% end %>
</fieldset>
<%= f.text_area :notes,
label: t("goals.form.fields.notes"),
rows: 2,
placeholder: t("goals.form.fields.notes_placeholder") %>
<div class="flex justify-end pt-2">
<%= f.submit goal.persisted? ? t("goals.form.save") : t("goals.form.create"),
data: { goal_form_target: "submitButton" } %>
</div>
<% end %>
</div>