The personal finance app for everyone (by everyone)
Ruby
<%# locals: (holding:, context:, cancel_action:) %>
<%
# cost_basis stores the per-share cost with four decimals, so the form submits
# the per-share field; the total only helps to fill it in. The per-share field
# shows as many of those decimals as the stored value uses, at least two (also
# for zero-decimal currencies such as JPY), so saving unchanged keeps the basis.
# A user-set basis of 0 (e.g. shares received for free) is a real value and is
# shown as 0.00; an unknown basis leaves the fields empty (Holding#known_cost_basis).
# Both fields use step="any", so money_field leaves pastes unrounded: a
# per-share price below a cent (10 x 0.0049) gives the same basis as typing it.
# The per-share field is required, so an emptied editor is not submitted (and
# does not ask to overwrite the basis only to be rejected).
current_per_share = holding.known_cost_basis
per_share_precision = current_per_share ? current_per_share.round(4).to_s("F").split(".").last.sub(/0+\z/, "").length.clamp(2, 4) : 2
current_total = current_per_share && holding.qty.positive? ? (current_per_share * holding.qty).round(2) : nil
# Turbo submits the form, so the overwrite confirmation shows; "_top" leaves
# the holdings-list frame and the drawer for the redirect to the account.
form_data = { turbo_frame: "_top" }
if holding.avg_cost
form_data[:turbo_confirm] = CustomConfirm.new(
title: t("holdings.cost_basis_cell.overwrite_confirm_title"),
body: t("holdings.cost_basis_cell.overwrite_confirm_body", current: format_money(holding.avg_cost))
).to_data_attribute
end
%>
<%= styled_form_with model: holding,
url: holding_path(holding),
method: :patch,
namespace: dom_id(holding, "#{context}_cost_basis"),
class: "space-y-3",
data: form_data.merge(controller: "cost-basis-form", cost_basis_form_qty_value: holding.qty) do |f| %>
<%# Laid out like an invoice line: per share x quantity = total. Only the
per-share cost is submitted; the total field just helps to fill it in. %>
<%= f.money_field :per_share_cost_basis,
label: t("holdings.cost_basis_cell.per_share_label"),
name: f.field_name(:per_share_cost_basis),
hide_currency: true,
currency_value_override: holding.currency,
value: current_per_share,
precision: per_share_precision,
required: true,
placeholder: "0.00",
input_class: "privacy-sensitive privacy-sensitive-interactive",
step: "any",
min: 0,
amount_data: { action: "input->cost-basis-form#updateTotal", cost_basis_form_target: "perShare" } %>
<p class="flex items-center gap-2 px-3 text-sm">
<span class="text-secondary">×</span>
<span class="text-primary privacy-sensitive"><%= t("holdings.holding.shares", qty: format_quantity(holding.qty, exact: true)) %></span>
</p>
<%# The holding stores the per-share cost, so neither field is bound to it. %>
<%= f.money_field :total_cost_basis,
label: safe_join([ tag.span("=", class: "text-secondary"), t("holdings.cost_basis_cell.total_cost_basis_label") ], " "),
name: nil,
hide_currency: true,
currency_value_override: holding.currency,
value: current_total,
precision: 2,
placeholder: "0.00",
input_class: "privacy-sensitive privacy-sensitive-interactive",
step: "any",
min: 0,
amount_data: { action: "input->cost-basis-form#updatePerShare", cost_basis_form_target: "total" } %>
<div class="flex justify-end gap-2 pt-2">
<%= render DS::Button.new(text: t("holdings.cost_basis_cell.cancel"), variant: :secondary, data: { action: cancel_action }) %>
<%= render DS::Button.new(text: t("holdings.cost_basis_cell.save"), type: "submit") %>
</div>
<% end %>