The personal finance app for everyone (by everyone)
Ruby
Make the before-payday strip readable at a month of bills (#4369)
jaysbeekayb42a944c1149
diff --git a/app/helpers/bills_helper.rb b/app/helpers/bills_helper.rbindex e097c79b3f..3472938a9d 100644--- a/app/helpers/bills_helper.rb+++ b/app/helpers/bills_helper.rb@@ -1,4 +1,8 @@module BillsHelper+ # Bills the covered bridge strip lists before folding the rest behind a+ # disclosure. A month-long window holds dozens; five keeps the strip a strip.+ PAYCHECK_BRIDGE_VISIBLE_ITEMS = 5+# A bill's next due date reads "Oct 5" when it falls this year and gains the# year otherwise, so an every-2-years bill never shows "Sep 1" for a date# that is years out. Same rule as Period#label. Payment dates are historical@@ -164,6 +168,25 @@ module BillsHelperendend+ # The bridge's bar: what is due before payday against the cash on hand.+ # Empty when the balance is unknown, since the bar would state a figure the+ # planner deliberately does not claim.+ def paycheck_bridge_segments(period)+ return [] if period.cash_on_hand.nil?++ normalize_segments([+ [ :due, period.obligation_total ],+ [ :left, period.cash_after_obligations ]+ ], period.cash_on_hand)+ end++ def paycheck_bridge_aria(period, currency)+ t("bills.paycheck.bridge_allocation_aria",+ cash: format_money(Money.new(period.cash_on_hand, currency)),+ due: format_money(Money.new(period.obligation_total, currency)),+ left: format_money(Money.new(period.cash_after_obligations, currency)))+ end+# The bar in words, in the same terms the card states in text.def paycheck_allocation_aria(period, currency)if period.short?diff --git a/app/views/bills/_paycheck_bridge.html.erb b/app/views/bills/_paycheck_bridge.html.erbindex 3440186db3..077b3b763e 100755--- a/app/views/bills/_paycheck_bridge.html.erb+++ b/app/views/bills/_paycheck_bridge.html.erb@@ -7,35 +7,66 @@earned a banner, so a bill due in five days appeared nowhere on the planthat is supposed to answer what is due before payday.- Deliberately quiet. Nothing is wrong here, and the window still has to be- visible. %>-<div class="rounded-xl bg-surface-inset px-4 py-3 space-y-1">+ A month between paydays holds dozens of bills, so each one is a dated+ ledger row like the period cards below, the first few show and the rest+ fold away. Only rendered once the balance is known (see+ BillsHelper#paycheck_plan_sections), so the cash figures always exist. %>++<%+ items = period.items_due+ shown = items.first(BillsHelper::PAYCHECK_BRIDGE_VISIBLE_ITEMS)+ folded = items.drop(BillsHelper::PAYCHECK_BRIDGE_VISIBLE_ITEMS)+ segments = paycheck_bridge_segments(period)+ segment_class = { due: "bg-inverse", left: "bg-success" }+%>++<section class="bg-container rounded-xl shadow-border-xs px-4 sm:px-5 py-4 space-y-3" data-paycheck-bridge><div class="flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1"><p class="text-sm text-primary tabular-nums privacy-sensitive"><span class="font-medium"><%= t("bills.paycheck.bridge_label") %></span><%= t("bills.paycheck.bridge_amount", amount: format_money(Money.new(period.obligation_total, currency))) %></p>- <%= link_to t("bills.paycheck.review_plan"), bills_path,- class: "text-xs text-secondary hover:text-primary shrink-0" %>+ <p class="text-xs text-secondary tabular-nums privacy-sensitive shrink-0">+ <%= t("bills.paycheck.bridge_left", amount: format_money(Money.new(period.cash_after_obligations, currency))) %>+ </p></div>- <p class="text-xs text-secondary">- <%= t("bills.paycheck.bridge_split",- date: l(period.ends_on + 1, format: :short),- cash: format_money(Money.new(period.cash_on_hand, currency))) %>- </p>-- <div class="pt-1 space-y-0.5">- <% period.items_due.each do |item| %>- <div class="flex items-baseline justify-between gap-3 text-xs">- <%= link_to item.occurrence.recurring_transaction.display_name,- bill_path(item.occurrence.recurring_transaction, occurrence: item.occurrence.id),- data: { turbo_frame: "drawer", turbo_prefetch: false },- class: "text-secondary hover:text-primary truncate" %>- <span class="text-secondary tabular-nums privacy-sensitive shrink-0">- <%= format_money(Money.new(item.share, currency)) %>- </span>+ <div class="space-y-2">+ <p class="text-xs text-secondary">+ <%= t("bills.paycheck.bridge_split",+ date: l(period.ends_on + 1, format: :short),+ cash: format_money(Money.new(period.cash_on_hand, currency))) %>+ </p>++ <% if segments.any? %>+ <div class="flex h-1.5 rounded-full overflow-hidden bg-surface-inset"+ role="img"+ aria-label="<%= paycheck_bridge_aria(period, currency) %>">+ <% segments.each do |key, percent| %>+ <div class="<%= segment_class[key] %>" style="width: <%= percent %>%"></div>+ <% end %></div><% end %></div>-</div>++ <div>+ <% shown.each do |item| %>+ <%= render "bills/paycheck_item", item: item, currency: currency, reserved: false %>+ <% end %>++ <% if folded.any? %>+ <%= render DS::Disclosure.new(variant: :inline, body_class: nil) do |disclosure| %>+ <% disclosure.with_summary_content do %>+ <span class="flex items-center gap-1 pt-1 text-xs text-secondary hover:text-primary">+ <%= t("bills.paycheck.bridge_show_more", count: folded.size) %>+ <%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 motion-safe:transition-transform motion-safe:duration-150" %>+ </span>+ <% end %>++ <% folded.each do |item| %>+ <%= render "bills/paycheck_item", item: item, currency: currency, reserved: false %>+ <% end %>+ <% end %>+ <% end %>+ </div>+</section>diff --git a/app/views/bills/_paycheck_item.html.erb b/app/views/bills/_paycheck_item.html.erbindex dfb391f676..2b52b46548 100644--- a/app/views/bills/_paycheck_item.html.erb+++ b/app/views/bills/_paycheck_item.html.erb@@ -1,13 +1,14 @@<%# One bill as a ledger line: due date in a fixed column, name, amount. Areserved line also names the obligation it is a slice of, since the amount- on the right is a share rather than the whole bill. %>+ on the right is a share rather than the whole bill. An occurrence past its+ grace says so, on the occurrence's own overdue? rather than a raw past+ date, so a bill due yesterday reads the same here as everywhere in Bills. %><% series = item.occurrence.recurring_transaction %>+<% overdue = item.occurrence.overdue? %>-<div class="flex items-baseline gap-3 py-1">- <span class="w-14 shrink-0 text-xs text-secondary tabular-nums">- <%= l(item.occurrence.effective_due_on, format: :short) %>- </span>+<div class="flex items-baseline gap-3 py-1" data-paycheck-item>+ <span class="w-14 shrink-0 text-xs tabular-nums <%= overdue ? "text-destructive" : "text-secondary" %>" data-paycheck-item-date><%= l(item.occurrence.effective_due_on, format: :short) %></span><span class="min-w-0 flex-1 truncate"><%= link_to series.display_name, recurring_occurrence_path(item.occurrence),@@ -19,6 +20,10 @@<% end %></span>+ <% if overdue %>+ <span class="shrink-0 text-xs text-destructive" data-paycheck-overdue><%= t("bills.paycheck.item_overdue") %></span>+ <% end %>+<span class="text-sm text-primary privacy-sensitive shrink-0 tabular-nums"><%= format_money(Money.new(item.share, currency)) %></span>diff --git a/config/locales/views/bills/ca.yml b/config/locales/views/bills/ca.ymlindex c592d75278..2b4bcd72f3 100644--- a/config/locales/views/bills/ca.yml+++ b/config/locales/views/bills/ca.yml@@ -190,6 +190,12 @@ ca:bridge_label: Abans del teu proper cobramentbridge_amount: · %{amount} per pagarbridge_split: Venç abans del %{date} i queda cobert pels %{cash} dels teus comptes.+ bridge_left: "%{amount} disponibles després de les factures"+ bridge_show_more:+ one: Mostra 1 més+ other: "Mostra %{count} més"+ bridge_allocation_aria: "Dels %{cash} dels teus comptes, %{due} vencen abans del teu proper cobrament i en queden %{left}."+ item_overdue: Vençudashortfall_label: Falta abans del teu proper cobramentshortfall_amount: · %{amount}shortfall_split: '%{obligations} vencen abans del %{date} i els teus comptes tenen %{cash}.'diff --git a/config/locales/views/bills/en.yml b/config/locales/views/bills/en.ymlindex 95e9a28afa..ca24112fa8 100644--- a/config/locales/views/bills/en.yml+++ b/config/locales/views/bills/en.yml@@ -206,6 +206,12 @@ en:bridge_label: Before your next paycheckbridge_amount: "· %{amount} due"bridge_split: "Due before %{date}, covered by the %{cash} in your accounts."+ bridge_left: "%{amount} left after bills"+ bridge_show_more:+ one: Show 1 more+ other: "Show %{count} more"+ bridge_allocation_aria: "Of %{cash} in your accounts, %{due} is due before your next paycheck and %{left} is left."+ item_overdue: Overdueshortfall_label: Short before your next paydayshortfall_amount: "· %{amount}"shortfall_split: "%{obligations} is due before %{date} and your accounts hold %{cash}."diff --git a/config/locales/views/bills/fr.yml b/config/locales/views/bills/fr.ymlindex 737e856c45..0a6d48f561 100644--- a/config/locales/views/bills/fr.yml+++ b/config/locales/views/bills/fr.yml@@ -232,6 +232,12 @@ fr:bridge_amount: "· %{amount} à payer"bridge_label: Avant votre prochaine paiebridge_split: À payer avant le %{date}, couvert par les %{cash} présents sur vos comptes.+ bridge_left: "%{amount} restants après les factures"+ bridge_show_more:+ one: Afficher 1 de plus+ other: "Afficher %{count} de plus"+ bridge_allocation_aria: "Sur les %{cash} de vos comptes, %{due} sont à payer avant votre prochaine paie et il reste %{left}."+ item_overdue: En retarddue_this_period: À payer cette périodeempty:action: Ajouter un revenudiff --git a/config/locales/views/bills/pl.yml b/config/locales/views/bills/pl.ymlindex e133388600..b773cd7083 100644--- a/config/locales/views/bills/pl.yml+++ b/config/locales/views/bills/pl.yml@@ -285,6 +285,14 @@ pl:bridge_amount: "· do zapłaty %{amount}"bridge_label: Przed następną wypłatąbridge_split: Termin przed %{date}, pokryte przez %{cash} na Twoich kontach.+ bridge_left: "%{amount} zostaje po rachunkach"+ bridge_show_more:+ few: "Pokaż %{count} więcej"+ many: "Pokaż %{count} więcej"+ one: Pokaż 1 więcej+ other: "Pokaż %{count} więcej"+ bridge_allocation_aria: "Z %{cash} na Twoich kontach %{due} jest do zapłaty przed następną wypłatą, a zostaje %{left}."+ item_overdue: Zaległedue_this_period: Termin w tym okresieempty:action: Dodaj przychóddiff --git a/config/locales/views/bills/ru.yml b/config/locales/views/bills/ru.ymlindex 09dd6585ec..3d58f5b766 100644--- a/config/locales/views/bills/ru.yml+++ b/config/locales/views/bills/ru.yml@@ -218,6 +218,14 @@ ru:bridge_label: До следующей зарплатыbridge_amount: · к оплате %{amount}bridge_split: К оплате до %{date}; покрывается суммой %{cash} на ваших счетах.+ bridge_left: "%{amount} останется после платежей"+ bridge_show_more:+ one: "Показать ещё %{count}"+ few: "Показать ещё %{count}"+ many: "Показать ещё %{count}"+ other: "Показать ещё %{count}"+ bridge_allocation_aria: "Из %{cash} на ваших счетах %{due} нужно оплатить до следующей зарплаты, остаётся %{left}."+ item_overdue: Просроченоshortfall_label: Не хватает до следующей зарплатыshortfall_amount: · %{amount}shortfall_split: До %{date} нужно оплатить %{obligations}, а на ваших счетах %{cash}.diff --git a/config/locales/views/bills/tr.yml b/config/locales/views/bills/tr.ymlindex 1a5f3ee23b..e7d20a7bfb 100644--- a/config/locales/views/bills/tr.yml+++ b/config/locales/views/bills/tr.yml@@ -190,6 +190,12 @@ tr:bridge_label: Bir sonraki maaşınızdan öncebridge_amount: · %{amount} ödenecekbridge_split: '%{date} tarihinden önce ödenecek; hesaplarınızdaki %{cash} ile karşılanıyor.'+ bridge_left: "Faturalardan sonra %{amount} kalıyor"+ bridge_show_more:+ one: 1 tane daha göster+ other: "%{count} tane daha göster"+ bridge_allocation_aria: "Hesaplarınızdaki %{cash} tutarın %{due} kadarı bir sonraki maaşınızdan önce ödenecek ve %{left} kalıyor."+ item_overdue: Gecikmişshortfall_label: Bir sonraki maaş gününüzden önce eksikshortfall_amount: · %{amount}shortfall_split: '%{date} tarihinden önce %{obligations} ödenecek ve hesaplarınızda %{cash} var.'diff --git a/config/locales/views/bills/uk.yml b/config/locales/views/bills/uk.ymlindex ce2a1dfa48..4d6f0e7e3e 100644--- a/config/locales/views/bills/uk.yml+++ b/config/locales/views/bills/uk.yml@@ -218,6 +218,14 @@ uk:bridge_label: До наступної зарплатиbridge_amount: · %{amount} до сплатиbridge_split: Сплатити до %{date}; покривається коштами на ваших рахунках (%{cash}).+ bridge_left: "%{amount} залишиться після платежів"+ bridge_show_more:+ one: "Показати ще %{count}"+ few: "Показати ще %{count}"+ many: "Показати ще %{count}"+ other: "Показати ще %{count}"+ bridge_allocation_aria: "З %{cash} на ваших рахунках %{due} треба сплатити до наступної зарплати, залишається %{left}."+ item_overdue: Простроченоshortfall_label: Бракує до наступної зарплатиshortfall_amount: · %{amount}shortfall_split: До %{date} треба сплатити %{obligations}, а на ваших рахунках %{cash}.diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 0edc6b7f5b..570e105f2b 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -2595,6 +2595,85 @@ class BillsControllerTest < ActionDispatch::IntegrationTest"a timeline row prints income minus obligations, which is a deficit on a window that earns nothing")end+ # The strip printed a name and an amount only, so a bill due last week and+ # the same bill next month read as one entry listed twice. Its rows also+ # opened the bill drawer while the same bill in a period card opened the+ # occurrence drawer.+ test "the covered bridge shows each bill's due date and opens its occurrence" do+ @family.accounts.where(accountable_type: "Depository").update_all(balance: 5_000)+ declare_income(name: "Frito Lay", amount: -1200, payday: Date.current + 5)+ due = Date.current + 2+ bill = declare_bill(name: "Curbside Cuts", amount: 150, due: due)+ occurrence = bill.recurring_occurrences.find_by!(due_on: due)++ get bills_url(view: "paycheck")++ assert_response :success+ assert_select "[data-paycheck-bridge] [data-paycheck-item]", 1 do+ assert_select "[data-paycheck-item-date]", text: I18n.l(due, format: :short)+ assert_select "a[href=?][data-turbo-frame=drawer]", recurring_occurrence_path(occurrence), text: "Curbside Cuts"+ end+ end++ # Keyed on the occurrence's own overdue? (grace included), not on a raw+ # past date: the row inside its grace is what tells the two apart.+ test "only an occurrence past its grace is marked overdue" do+ travel_to Date.current.beginning_of_month + 14.days++ @family.accounts.where(accountable_type: "Depository").update_all(balance: 5_000)+ declare_income(name: "Frito Lay", amount: -1200, payday: Date.current + 5)+ declare_bill(name: "Late Gym", amount: 40, due: Date.current - 5)+ declare_bill(name: "Grace Water", amount: 60, due: Date.current - 1)+ declare_bill(name: "Curbside Cuts", amount: 150, due: Date.current + 2)+ declare_bill(name: "Next Period Phone", amount: 70, due: Date.current + 8)++ get bills_url(view: "paycheck")++ assert_response :success+ assert_select "[data-paycheck-bridge] [data-paycheck-item]", 3+ assert_select "[data-paycheck-overdue]", 1,+ "one row is past its grace; the in-grace, upcoming and next-period rows are not overdue"+ assert_select "[data-paycheck-bridge] [data-paycheck-item]", text: /Late Gym/ do+ assert_select "[data-paycheck-overdue]", text: I18n.t("bills.paycheck.item_overdue")+ end+ end++ test "the covered bridge lists five bills and folds the rest away" do+ @family.accounts.where(accountable_type: "Depository").update_all(balance: 50_000)+ declare_income(name: "Frito Lay", amount: -1200, payday: Date.current + 5)+ 7.times do |index|+ declare_bill(name: "Bill #{index}", amount: 100 + index, due: Date.current + 1 + (index % 3))+ end++ get bills_url(view: "paycheck")++ assert_response :success+ assert_select "[data-paycheck-bridge]" do+ assert_select "[data-paycheck-item]", 7+ assert_select "details [data-paycheck-item]", 2+ assert_select "details summary", text: /#{Regexp.escape(I18n.t("bills.paycheck.bridge_show_more", count: 2))}/+ end+ end++ test "the covered bridge reports what is left after its bills" do+ @family.accounts.where(accountable_type: "Depository").update_all(balance: 5_000)+ declare_income(name: "Frito Lay", amount: -1200, payday: Date.current + 5)+ declare_bill(name: "Curbside Cuts", amount: 150, due: Date.current + 2)++ get bills_url(view: "paycheck")++ assert_response :success+ bridge = RecurringTransaction::PaycheckPlanner.new(@family, user: @user).plan.find(&:bridge?)+ left = I18n.t("bills.paycheck.bridge_left", amount: money_string(bridge.cash_after_obligations))++ assert_select "[data-paycheck-bridge]" do+ assert_select "*", text: left+ assert_select "[role=img][aria-label]", 1+ end+ assert_no_match I18n.t("bills.paycheck.review_plan"), response.body,+ "the covered strip sits inside the plan, so a link back to the plan goes nowhere new"+ end+privatedef declare_scheduled_income(frequency:, weekday: nil, day_of_month: nil)diff --git a/test/helpers/bills_helper_test.rb b/test/helpers/bills_helper_test.rbindex a96fe4a923..dbeaac4063 100644--- a/test/helpers/bills_helper_test.rb+++ b/test/helpers/bills_helper_test.rb@@ -159,6 +159,20 @@ class BillsHelperTest < ActionView::TestCaseassert_empty paycheck_allocation_segments(build_period(income: 0, due: 28.71, reserved: 695.62))end+ # The covered bridge earns nothing, so its bar measures the window's bills+ # against the cash on hand instead of a paycheck.+ test "a covered bridge divides its cash into due and left" do+ period = build_period(income: 0, due: 150, reserved: 0, leading: true, cash_on_hand: BigDecimal("5000"))++ assert_equal [ [ :due, 3.0 ], [ :left, 97.0 ] ], paycheck_bridge_segments(period)+ end++ # Unknown is not zero: with no balance there is nothing to divide, and the+ # bar must not draw one.+ test "a bridge with an unknown balance has no bar" do+ assert_empty paycheck_bridge_segments(build_period(income: 0, due: 150, reserved: 0, leading: true))+ end+test "a zero part is dropped rather than drawn as a hairline" dosegments = paycheck_allocation_segments(build_period(income: 1200, due: 0, reserved: 338.12))