The personal finance app for everyone (by everyone)
Ruby
<%# locals: (account:, url:) %>
<%= render "accounts/form", account: account, url: url do |form| %>
<%= form.fields_for :accountable do |loan_form| %>
<%= loan_form.hidden_field :rate_schedule_snapshot %>
<%# What kind of loan it is sits with the account's own fields, straight
after the balance, above the divider and the loan's terms. %>
<div>
<%= loan_form.select :subtype,
Loan.subtype_options_for_select,
{ label: true, prompt: t("loans.form.subtype_prompt"), include_blank: t("loans.form.none") } %>
</div>
<%= render "shared/ruler", classes: "my-4" %>
<div class="space-y-2"
data-controller="loan-rate-changes"
data-loan-rate-changes-fixed-type-value="<%= Loan::FIXED_RATE_TYPE %>">
<div class="flex items-center gap-2">
<%= loan_form.money_field :initial_balance,
label: t("loans.form.initial_balance"),
default_currency: Current.family.currency,
required: true %>
</div>
<div class="flex items-center gap-2">
<%= loan_form.number_field :interest_rate,
label: t("loans.form.interest_rate"),
placeholder: t("loans.form.interest_rate_placeholder"),
min: 0,
step: 0.005 %>
<%# A provider can write a rate type the form never offers ("arm"); it
stays in the list so saving the form keeps it rather than silently
replacing it with the first option. %>
<%= loan_form.select :rate_type,
loan_rate_type_options(account.loan),
{ label: t("loans.form.rate_type") },
{ data: { loan_rate_changes_target: "rateType",
action: "change->loan-rate-changes#toggle" } } %>
</div>
<div class="flex items-center gap-2">
<%# What the borrower put in up front. Not part of the amortisation --
the loan amortises what was lent -- but it is what makes the
leverage figure on the overview readable. %>
<%= loan_form.money_field :down_payment,
label: t("loans.form.down_payment"),
default_currency: Current.family.currency %>
</div>
<div class="flex items-center gap-2">
<%= loan_form.date_field :start_date,
label: t("loans.form.start_date"),
max: Date.current %>
</div>
<p class="text-xs text-secondary"><%= t("loans.form.start_date_hint") %></p>
<div class="flex items-center gap-2">
<%# Bounded here rather than validated on Loan: PlaidAccount::Liabilities::StudentLoanProcessor
writes this column from provider dates, and a term the simulator will not walk is
answered tolerantly there (no schedule, as for an unknown rate type). %>
<%= loan_form.number_field :term_months,
label: t("loans.form.term_months"),
placeholder: t("loans.form.term_months_placeholder"),
min: 1,
max: Loan::Simulator::MAX_PERIODS %>
</div>
<div class="flex items-center gap-2">
<%# How the lender measures a period against a year. The default charges
a flat twelfth, which is what every existing loan was built on, so
leaving this alone changes nothing about the schedule. %>
<%= loan_form.select :day_count_convention,
loan_day_count_convention_options,
{ label: t("loans.form.day_count_convention") } %>
</div>
<p class="text-xs text-secondary"><%= t("loans.form.day_count_convention_hint") %></p>
<div class="flex items-center gap-2">
<%# The premium many lenders quote beside the instalment. Charged
alongside it rather than inside it, so it moves the loan's total
cost without moving its repayment. %>
<%= loan_form.number_field :insurance_rate,
label: t("loans.form.insurance_rate"),
placeholder: t("loans.form.insurance_rate_placeholder"),
min: 0,
step: 0.005 %>
<%= loan_form.select :insurance_rate_type,
loan_insurance_rate_type_options,
{ label: t("loans.form.insurance_rate_type"),
include_blank: t("loans.form.none") } %>
</div>
<p class="text-xs text-secondary"><%= t("loans.form.insurance_rate_hint") %></p>
<div class="flex items-center justify-between gap-4">
<div class="space-y-1">
<p class="text-sm" id="principal_only_transfers_label"><%= t("loans.form.principal_only_transfers") %></p>
<p class="text-secondary text-sm"><%= t("loans.form.principal_only_transfers_hint") %></p>
</div>
<%= loan_form.toggle :principal_only_transfers,
"aria-labelledby": "principal_only_transfers_label" %>
</div>
<%# Rate changes only mean something for a loan whose rate can move. The
section is rendered for every rate type, retained rows included, and
for fixed it is hidden AND its inputs disabled -- here, server-side,
not only by the Stimulus controller, so a save without JavaScript
behaves the same. A disabled input is not submitted, so a fixed loan
sends no rate_changes key and its recorded changes are retained
rather than cleared. Switching the select to variable enables and
reveals the retained rows, and the save resubmits them. The rows sit
in a disclosure that opens closed, as Additional details does. %>
<% rate_changes_disabled = !account.loan.variable_rate_type? %>
<div data-loan-rate-changes-target="section" <%= "hidden" if rate_changes_disabled %>>
<%= render DS::Disclosure.new(variant: :inline, body_class: "space-y-2 mt-2 pl-4 border-l border-primary") do |disclosure| %>
<% disclosure.with_summary_content do %>
<span class="text-sm text-secondary hover:text-primary flex items-center gap-1 py-2">
<%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 transition-transform" %>
<%= t("loans.form.rate_changes") %>
</span>
<% end %>
<p class="text-xs text-secondary"><%= t("loans.form.rate_changes_hint") %></p>
<%# Submitted whenever the section is enabled, so removing the last row
still sends the key and the writer is called to clear the schedule.
Without it, "remove them all" would send nothing and silently leave
them persisted. Disabled with the section: on a fixed loan this
input alone would otherwise submit and clear the retained rows. %>
<input type="hidden" name="account[accountable_attributes][rate_changes][]" value=""
<%= "disabled" if rate_changes_disabled %>>
<div data-loan-rate-changes-target="rows" class="space-y-2">
<% account.loan.rate_change_rows.each do |row| %>
<%= render "loans/rate_change_row",
effective_date: row[:effective_date], rate: row[:rate],
disabled: rate_changes_disabled %>
<% end %>
</div>
<p data-loan-rate-changes-target="empty" class="text-xs text-subdued"
<%= "hidden" if account.loan.rate_change_rows.any? %>>
<%= t("loans.form.rate_changes_empty") %>
</p>
<template data-loan-rate-changes-target="template">
<%= render "loans/rate_change_row", effective_date: nil, rate: nil %>
</template>
<%= render DS::Button.new(text: t("loans.form.rate_change_add"), icon: "plus", variant: "ghost", type: "button",
disabled: rate_changes_disabled, data: { action: "loan-rate-changes#add" }) %>
<% end %>
</div>
</div>
<% end %>
<% end %>