The personal finance app for everyone (by everyone)
Ruby
Give each Income plan period one hero figure (#3879)
Guillem Arias Faustef50c67017b10
diff --git a/app/views/bills/_paycheck_period.html.erb b/app/views/bills/_paycheck_period.html.erbindex 40976f6de1..1f8f3ca139 100644--- a/app/views/bills/_paycheck_period.html.erb+++ b/app/views/bills/_paycheck_period.html.erb@@ -3,7 +3,7 @@due in the window, and an expandable footer for what is held for later. %><%- segments = paycheck_allocation_segments(period)+ segments = paycheck_allocation_segments(period).to_hsegment_class = {due: "bg-inverse",reserved: "bg-subdued",@@ -14,6 +14,12 @@safe_tone = period.short? ? "text-destructive" : "text-primary"safe_amount = format_money(Money.new(period.short? ? period.shortfall : period.remaining, currency))safe_label = period.short? ? t("bills.paycheck.short_after_bills") : t("bills.paycheck.safe_after_bills")+ safe_key = period.short? ? :short : :safe+ legend = [+ [ :due, t("bills.paycheck.due_this_period"), format_money(Money.new(period.due_total, currency)), "text-secondary" ],+ [ :reserved, t("bills.paycheck.reserved_ahead"), format_money(Money.new(period.reserved_total, currency)), "text-secondary" ],+ [ safe_key, period.short? ? t("bills.paycheck.short_short") : t("bills.paycheck.safe_short"), safe_amount, "font-medium #{safe_tone}" ]+ ]%><section class="flex gap-3 px-4 sm:px-5">@@ -42,29 +48,33 @@</div><div class="text-right shrink-0">- <p class="text-lg font-medium leading-7 tabular-nums privacy-sensitive <%= safe_tone %>"><%= safe_amount %></p>+ <p class="text-2xl font-medium leading-8 tabular-nums privacy-sensitive <%= safe_tone %>"><%= safe_amount %></p><p class="text-xs text-secondary"><%= safe_label %></p></div></div><div>+ <%# The breakdown is the bar's legend. A figure with no segment on the bar+ keeps the swatch's space, labels wrap rather than truncate on a phone,+ and each figure sits at its column's foot, so every column lines up. %><div class="grid grid-cols-3 gap-3">- <div class="min-w-0">- <p class="text-xs text-secondary truncate"><%= t("bills.paycheck.due_this_period") %></p>- <p class="text-sm font-medium text-primary tabular-nums privacy-sensitive"><%= format_money(Money.new(period.due_total, currency)) %></p>- </div>- <div class="min-w-0">- <p class="text-xs text-secondary truncate"><%= t("bills.paycheck.reserved_ahead") %></p>- <p class="text-sm font-medium text-primary tabular-nums privacy-sensitive"><%= format_money(Money.new(period.reserved_total, currency)) %></p>- </div>- <div class="min-w-0">- <p class="text-xs text-secondary truncate"><%= period.short? ? t("bills.paycheck.short_short") : t("bills.paycheck.safe_short") %></p>- <p class="text-sm font-medium tabular-nums privacy-sensitive <%= safe_tone %>"><%= safe_amount %></p>- </div>+ <% legend.each do |key, label, amount, figure_class| %>+ <div class="min-w-0 flex flex-col justify-between">+ <p class="flex gap-1.5 text-xs text-secondary">+ <% if segments.key?(key) %>+ <span class="shrink-0 size-2 mt-1 rounded-full <%= segment_class[key] %>" data-paycheck-legend-swatch aria-hidden="true"></span>+ <% else %>+ <span class="shrink-0 size-2" aria-hidden="true"></span>+ <% end %>+ <span class="min-w-0"><%= label %></span>+ </p>+ <p class="pl-3.5 text-sm tabular-nums privacy-sensitive <%= figure_class %>"><%= amount %></p>+ </div>+ <% end %></div><% if segments.any? %>- <div class="flex h-0.5 rounded-full overflow-hidden bg-surface-inset mt-2"+ <div class="flex h-1.5 rounded-full overflow-hidden bg-surface-inset mt-2"role="img"aria-label="<%= paycheck_allocation_aria(period, currency) %>"><% segments.each do |key, percent| %>diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 44b9529e9e..09f8741e3c 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -774,6 +774,28 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_nil I18n.t("bills.paycheck.obligations_line", default: nil)end+ # The breakdown is the allocation bar's legend: each figure carries a swatch+ # in its segment's colour, so the bar reads without a second key.+ test "the paycheck breakdown keys each figure to its allocation bar segment" do+ payday = Date.current + 3+ declare_income(name: "Frito Lay", amount: -1200, payday: payday)+ declare_bill(name: "Streaming", amount: 20, due: Date.current + 5)+ declare_bill(name: "Insurance", amount: 1500, due: payday + 31)++ get bills_url(view: "paycheck")++ assert_response :success+ bars = css_select("section div.flex[role=img]")+ assert_equal 2, bars.size++ bars.each do |bar|+ segment_colours = bar.css("div").map { |segment| segment["class"] }+ swatch_colours = bar.ancestors("section").first.css("[data-paycheck-legend-swatch]").map { |swatch| swatch["class"][/bg-\S+/] }+ assert_equal segment_colours, swatch_colours+ end+ assert_equal %w[bg-inverse bg-subdued bg-success], bars.first.css("div").map { |segment| segment["class"] }+ end+# The window before the first payday has no income to allocate, so it is# reported above the timeline as a warning rather than drawn as a pay period# with an empty paycheck. The shortfall names the obligation, not the slice