Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Say in the loan chart's description that tooltips split each payment (#4397)

jaysbeekay857f3231e07c
diff --git a/app/models/loan/payoff_chart.rb b/app/models/loan/payoff_chart.rb
index f25e0476e8..e422b1cebc 100644
--- a/app/models/loan/payoff_chart.rb
+++ b/app/models/loan/payoff_chart.rb
@@ -97,7 +97,7 @@ class Loan
# the not-converged notice can quote.
balloon: projection.applicable? && !projection.converged? ? projection.balloon_amount.amount.to_f : nil,
labels: labels,
- aria_description: aria_description(actual: series[:actual], scheduled_visible: visible?(series[:scheduled]))
+ aria_description: aria_description(series)
}
end
@@ -230,9 +230,12 @@ class Loan
)
end
- # Every series the chart draws is named here, with its payoff date, and
- # the shading under the scheduled line in words when that line is drawn.
- def aria_description(actual:, scheduled_visible:)
+ # Every series the chart draws is named here, with its payoff date, the
+ # shading under the scheduled line in words when that line is drawn, and
+ # the principal and interest split the tooltip adds when the window holds
+ # a scheduled payment to split.
+ def aria_description(series)
+ actual, scheduled = series.values_at(:actual, :scheduled)
description = I18n.t(
"UI.account.chart.loan.aria_description",
current_balance: projection.current_balance.format,
@@ -248,9 +251,44 @@ class Loan
) ].join(" ")
end
- description = [ description, interest_share_sentence ].join(" ") if scheduled_visible
+ description = [ description, interest_share_sentence ].join(" ") if visible?(scheduled)
- description
+ # The tooltip's split of each scheduled payment is not drawn anywhere
+ # else, so the description says it is there, whenever the tooltip can
+ # reach one.
+ return description unless split_reachable?(series)
+
+ [ description, I18n.t(
+ "UI.account.chart.loan.aria_composition",
+ principal: I18n.t("loans.tabs.schedule.principal"),
+ interest: I18n.t("loans.tabs.schedule.interest")
+ ) ].join(" ")
+ end
+
+ # Whether the tooltip shows a split at any of its stops, read as the
+ # chart controller reads them. The split rides on the payment points
+ # only; the opening point is the amount borrowed and carries none.
+ # The stops are the scheduled dates in the window, both edges included,
+ # or every plotted date in it when there are none; at a stop within the
+ # schedule's span the scheduled row shows its nearest point, the earlier
+ # on a tie, and past either end it shows nothing.
+ def split_reachable?(series)
+ drawn = series.values.select { |points| points.size > 1 }
+ scheduled = series[:scheduled].map { |point| [ Date.iso8601(point[:date]), point.key?(:principal) ] }
+ return false unless drawn.include?(series[:scheduled])
+
+ in_window = scheduled.select { |date, _| date.between?(domain_start, domain_end) }
+ return in_window.any? { |_, split| split } if in_window.any?
+
+ drawn.flatten.map { |point| Date.iso8601(point[:date]) }.uniq
+ .select { |date| date.between?(domain_start, domain_end) && date.between?(scheduled.first[0], scheduled.last[0]) }
+ .any? { |date| nearest_scheduled(scheduled, date)[1] }
+ end
+
+ def nearest_scheduled(scheduled, date)
+ after = scheduled.index { |point_date, _| point_date >= date } || scheduled.size - 1
+ before = scheduled[[ after - 1, 0 ].max]
+ (date - before[0]) <= (scheduled[after][0] - date) ? before : scheduled[after]
end
# Interest over what was paid, across the first twelve payments and the
diff --git a/config/locales/views/components/de.yml b/config/locales/views/components/de.yml
index 83b0fec39e..b7e8a1777b 100644
--- a/config/locales/views/components/de.yml
+++ b/config/locales/views/components/de.yml
@@ -132,6 +132,7 @@ de:
aria_actual_history_starts: "Die erfasste Saldenhistorie beginnt am %{date}."
aria_interest_share: "Über die Raten des ersten Jahres sind etwa %{first} % des gezahlten Betrags Zinsen, über die des letzten Jahres etwa %{last} %."
aria_interest_share_overall: "Über alle Raten sind etwa %{share} % des gezahlten Betrags Zinsen."
+ aria_composition: "Der Tooltip zu jeder geplanten Zahlung teilt sie in %{principal} und %{interest} auf."
interest_share: "Zinsanteil"
principal_share: "Tilgungsanteil"
interactive_chart: "interaktives Diagramm"
diff --git a/config/locales/views/components/en.yml b/config/locales/views/components/en.yml
index 8f248bfee8..61657aab54 100644
--- a/config/locales/views/components/en.yml
+++ b/config/locales/views/components/en.yml
@@ -71,6 +71,7 @@ en:
aria_actual_history_starts: Recorded balance history begins %{date}.
aria_interest_share: Across the first year's repayments about %{first}% of the amount paid is interest; across the final year's, about %{last}%.
aria_interest_share_overall: Across all the repayments about %{share}% of the amount paid is interest.
+ aria_composition: The tooltip on each scheduled payment splits it into %{principal} and %{interest}.
interest_share: Interest share
principal_share: Principal share
projected_payoff: Projected payoff
diff --git a/test/models/loan/payoff_chart_test.rb b/test/models/loan/payoff_chart_test.rb
index 1e3692f760..139bce5655 100644
--- a/test/models/loan/payoff_chart_test.rb
+++ b/test/models/loan/payoff_chart_test.rb
@@ -225,6 +225,64 @@ class Loan::PayoffChartTest < ActiveSupport::TestCase
)
end
+ # The tooltip on each scheduled payment splits it into principal and interest
+ # (#4006), so a screen reader is told the same detail exists.
+ test "the accessible description says each scheduled payment's tooltip splits principal and interest" do
+ payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload
+
+ composition = composition_sentence
+ assert_match(/Principal.*Interest/, composition)
+ assert_includes payload[:aria_description], composition
+ end
+
+ # The tooltip stops at plotted dates with no split when no scheduled payment
+ # falls in the window, so the description must not promise one there.
+ test "the accessible description does not mention the split for a window past the last scheduled payment" do
+ loan = build_loan
+ as_of = loan.amortization_schedule.payoff_date + 45
+ window = Period.new(key: "current_month", start_date: as_of.beginning_of_month, end_date: as_of)
+
+ payload = Loan::PayoffChart.new(loan, as_of: as_of, period: window).payload
+
+ assert_empty payload[:scheduled].select { |point|
+ point.key?(:principal) && Date.iso8601(point[:date]).between?(Date.iso8601(payload[:domain_start]), Date.iso8601(payload[:domain_end]))
+ }, "the window must hold no scheduled payment for this test to mean anything"
+ assert_not_includes payload[:aria_description], composition_sentence
+
+ whole_life = Loan::PayoffChart.new(loan, as_of: as_of).payload
+ assert_includes whole_life[:aria_description], composition_sentence
+ end
+
+ # The tooltip stops at a payment on the window's start edge, as the chart
+ # controller includes it, and shows its split, so the description says so.
+ test "the accessible description mentions the split for a window starting on the final repayment" do
+ loan = build_loan
+ final = loan.amortization_schedule.payoff_date
+ as_of = final + 5
+ window = Period.new(key: "current_month", start_date: final, end_date: as_of)
+
+ payload = Loan::PayoffChart.new(loan, as_of: as_of, period: window).payload
+
+ assert_equal final.iso8601, payload[:scheduled].last[:date], "the final repayment must sit on the window's start"
+ assert_includes payload[:aria_description], composition_sentence
+ end
+
+ # A window between two payments holds none, so the tooltip stops at the
+ # plotted dates in it, and the scheduled row there shows the nearest
+ # payment's split.
+ test "the accessible description mentions the split for a window between two payments" do
+ loan = build_loan
+ dates = loan.amortization_schedule.payments.map(&:date)
+ gap_start = dates[5] + 1
+ window = Period.new(key: "current_month", start_date: gap_start, end_date: dates[6] - 1)
+
+ payload = Loan::PayoffChart.new(loan, as_of: dates[6] - 1, period: window).payload
+
+ assert_empty payload[:scheduled].select { |point| Date.iso8601(point[:date]).between?(gap_start, dates[6] - 1) },
+ "the window must hold no scheduled point for this test to mean anything"
+ assert_includes payload[:aria_description], composition_sentence
+ end
+
test "the accessible description dates recorded history when it starts after origination" do
loan = on_contract_loan
first_recorded_date = loan.origination_date >> 6
@@ -502,6 +560,14 @@ class Loan::PayoffChartTest < ActiveSupport::TestCase
end
private
+ # The description's sentence saying each payment's tooltip splits it into
+ # principal and interest.
+ def composition_sentence
+ I18n.t("UI.account.chart.loan.aria_composition",
+ principal: I18n.t("loans.tabs.schedule.principal"),
+ interest: I18n.t("loans.tabs.schedule.interest"))
+ end
+
def interest_share_clause(rows)
I18n.t("UI.account.chart.loan.aria_interest_share",
first: interest_percent(rows.first(12)), last: interest_percent(rows.last(12)))