Files
sure/app/views/goals/_form_edit.html.erb
Guillem Arias f4b360bb96 feat(goals/edit): funding-accounts editor in the edit modal
Previously a user who linked the wrong account at creation had to
delete + recreate the goal. Now the edit modal carries the same
funding-accounts checkbox group as Step 1 of the stepper, pre-checked
with the goal's current links.

- GoalsController#edit loads @linkable_accounts + @currently_linked_account_ids.
- #update accepts account_ids; when supplied, runs the create / update
  inside a Goal.transaction and syncs linked accounts via
  sync_linked_accounts! (set-diff: destroy_all unselected goal_accounts,
  create the new ones). Validates at least one account before touching
  goal_accounts so the user gets a clean re-render.
- Removing an account preserves the goal's existing contributions —
  GoalContribution#account_must_be_linked_to_goal only fires on save,
  so historical rows stay valid.
- _form_edit partial accepts new locals; edit.html.erb threads them
  through.
- 3 new controller tests: identity-only patch leaves links intact;
  account_ids patch replaces the link set; empty account_ids
  re-renders with error.
2026-05-11 20:28:45 +02:00

74 lines
3.3 KiB
Plaintext

<%# locals: (goal:, linkable_accounts:, currently_linked_account_ids:) %>
<% if goal.errors.any? %>
<%= render "shared/form_errors", model: goal %>
<% end %>
<%= styled_form_with model: goal,
url: goal_path(goal),
method: :patch,
class: "space-y-3" do |f| %>
<%= f.text_field :name,
label: t("goals.form_stepper.step1.fields.name"),
required: true,
autofocus: true %>
<%= f.money_field :target_amount,
label: t("goals.form_stepper.step1.fields.target_amount"),
required: true %>
<%= f.date_field :target_date,
label: t("goals.form_stepper.step1.fields.target_date") %>
<div>
<div class="mb-2">
<span class="block text-sm font-medium text-primary"><%= t("goals.form_stepper.step1.fields.funding_accounts") %></span>
<p class="text-xs text-secondary mt-0.5"><%= t("goals.form_stepper.step1.fields.funding_accounts_hint") %></p>
</div>
<div class="bg-container-inset rounded-lg p-1">
<% 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_stepper.step1.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| %>
<label class="flex items-center gap-3 px-3 py-2.5 cursor-pointer <%= "border-t border-subdued" if idx > 0 %>">
<%= 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" %>
<%= render Goals::AvatarComponent.new(name: account.name, color: Goals::AvatarComponent.color_for(account.name), size: "md") %>
<div class="flex-1 min-w-0">
<p class="text-sm font-medium text-primary truncate"><%= account.name %></p>
<p class="text-xs text-secondary"><%= (account.subtype || subtype).titleize %></p>
</div>
<span class="text-sm text-primary tabular-nums"><%= Money.new(account.balance, account.currency).format %></span>
</label>
<% end %>
</div>
<% end %>
</div>
</div>
<div>
<span class="block text-sm text-secondary mb-2"><%= t("goals.form_stepper.step1.fields.color") %></span>
<div class="flex flex-wrap gap-2">
<% Goal::COLORS.each do |c| %>
<label class="relative">
<%= f.radio_button :color, c, class: "sr-only peer" %>
<div class="w-6 h-6 rounded-full cursor-pointer peer-checked:ring-2 peer-checked:ring-offset-2 peer-checked:ring-gray-500"
style="background-color: <%= c %>"></div>
</label>
<% end %>
</div>
</div>
<%= f.text_area :notes,
label: t("goals.form_stepper.step1.fields.notes"),
rows: 2 %>
<div class="flex justify-end pt-2">
<%= f.submit t("goals.edit.save") %>
</div>
<% end %>