Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Compare the same elapsed days in the spending widget header (#3457)

jaysbeekay079f5db60c09
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 9b4c11e51f..a3218a06c4 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -481,7 +481,9 @@ class PagesController < ApplicationController
# Cumulative daily spending for the selected month (capped at today while
# the month is in progress) against the previous month's full curve, so
- # the two lines share one day-of-month axis.
+ # the two lines share one day-of-month axis. The header totals compare the
+ # same number of elapsed days; only the chart draws the previous month out
+ # to its final day.
def build_spending_trend_data(income_statement, selected_month)
month_start = selected_month.beginning_of_month
month_end = month_start.end_of_month
@@ -501,13 +503,17 @@ class PagesController < ApplicationController
axis_days = month_end.day
current_series = cumulative_spending_series(current_period, current_daily)
- previous_series = fold_extra_days(
- cumulative_spending_series(previous_period, previous_daily),
- axis_days
- )
+ previous_header_series = cumulative_spending_series(previous_period, previous_daily)
+ previous_series = fold_extra_days(previous_header_series, axis_days)
current_total = current_series.last&.fetch(:value) || 0
- previous_total = previous_series.last&.fetch(:value) || 0
+ comparison_days = if month_start == Date.current.beginning_of_month
+ [ current_series.size, previous_header_series.size ].min
+ else
+ previous_header_series.size
+ end
+ previous_total = comparison_days.positive? ? previous_header_series[comparison_days - 1][:value] : 0
+ previous_comparison_day = comparison_days if comparison_days.positive? && comparison_days < previous_header_series.size
currency = income_statement.family.currency
@@ -524,6 +530,7 @@ class PagesController < ApplicationController
previous_total: Money.new(previous_total, currency),
delta: Money.new(current_total - previous_total, currency),
previous_label: I18n.l(previous_month_start, format: :month_year).capitalize,
+ previous_comparison_day: previous_comparison_day,
date_range_short: spending_trend_compact_date_range(current_period)
}
end
diff --git a/app/views/pages/dashboard/_spending_trend.html.erb b/app/views/pages/dashboard/_spending_trend.html.erb
index 45b25038d4..57fa881efc 100644
--- a/app/views/pages/dashboard/_spending_trend.html.erb
+++ b/app/views/pages/dashboard/_spending_trend.html.erb
@@ -12,6 +12,7 @@
previous: data[:previous]
}
previous_label = data[:previous_label]
+ previous_comparison_day = data[:previous_comparison_day]
date_range_short = data[:date_range_short]
%>
<div id="spending-trend-section" class="px-4 space-y-4">
@@ -63,7 +64,14 @@
<div class="flex items-center gap-3 min-w-0">
<span class="w-1 h-10 rounded-full bg-subdued shrink-0"></span>
<div class="min-w-0">
- <p class="text-sm text-secondary capitalize truncate"><%= previous_label %></p>
+ <p class="text-sm text-secondary truncate">
+ <span class="capitalize"><%= previous_label %></span>
+ <%# While the selected month is still running, the figure below covers %>
+ <%# the same elapsed days rather than the whole previous month. %>
+ <% if previous_comparison_day %>
+ <span class="text-xs"><%= t(".previous_comparison_days", end_day: previous_comparison_day) %></span>
+ <% end %>
+ </p>
<p class="font-medium text-lg tabular-nums text-secondary privacy-sensitive whitespace-nowrap"><%= format_money data[:previous_total] %></p>
</div>
</div>
diff --git a/config/locales/views/pages/en.yml b/config/locales/views/pages/en.yml
index 3ff81fca34..3d60596ba6 100644
--- a/config/locales/views/pages/en.yml
+++ b/config/locales/views/pages/en.yml
@@ -126,3 +126,4 @@ en:
month_picker_aria_label: "Select month"
period_scope_hint: "Shows the month you pick here — independent of the dashboard's time period at the top."
no_data: "No spending data for this month"
+ previous_comparison_days: "(1-%{end_day})"
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb
index 137fcbd506..46bbfccb81 100644
--- a/test/controllers/pages_controller_test.rb
+++ b/test/controllers/pages_controller_test.rb
@@ -546,6 +546,160 @@ class PagesControllerTest < ActionDispatch::IntegrationTest
assert_equal Date.current.beginning_of_month.iso8601, chart.fetch("current").first.fetch("date")
end
+ test "dashboard spending trend header compares the same elapsed days in both months" do
+ # Mid-month so the previous month always has spending both on or before and
+ # after the current day-of-month, whatever day the suite runs on.
+ travel_to Date.current.beginning_of_month + 14.days do
+ account = @family.accounts.create!(name: "Spending Trend Aligned Checking", currency: @family.currency, balance: 0, accountable: Depository.new)
+ previous_month = (Date.current.beginning_of_month - 1.month).beginning_of_month
+
+ create_transaction(account: account, name: "Prev early", amount: 111, date: previous_month)
+ create_transaction(account: account, name: "Prev late", amount: 999, date: previous_month.end_of_month)
+ create_transaction(account: account, name: "Current", amount: 7, date: Date.current)
+
+ get root_path, params: { spending_month: Date.current.beginning_of_month.iso8601 }
+ assert_response :ok
+
+ chart = spending_trend_chart_data
+ previous_series = chart.fetch("previous")
+ aligned = previous_series.fetch(Date.current.day - 1).fetch("value")
+ full_month = previous_series.last.fetch("value")
+ current_value = chart.fetch("current").last.fetch("value")
+
+ # The late transaction guarantees the two figures differ, so this pins
+ # which one the header reads.
+ assert_operator full_month, :>, aligned
+
+ _current_total, previous_total = spending_trend_header_totals
+ assert_equal money_text(aligned), previous_total
+ refute_equal money_text(full_month), previous_total
+
+ # The delta must be built from the same day-aligned figure.
+ assert_equal money_text(current_value - aligned), spending_trend_header_delta
+ end
+ end
+
+ test "dashboard spending trend header labels the days it compares while the month is in progress" do
+ travel_to Date.current.beginning_of_month + 14.days do
+ account = @family.accounts.create!(name: "Spending Trend Label Days Checking", currency: @family.currency, balance: 0, accountable: Depository.new)
+ create_transaction(account: account, name: "Spend", amount: 10, date: Date.current)
+
+ get root_path, params: { spending_month: Date.current.beginning_of_month.iso8601 }
+ assert_response :ok
+
+ assert_select "#spending-trend-section span",
+ text: I18n.t("pages.dashboard.spending_trend.previous_comparison_days", end_day: Date.current.day)
+ end
+ end
+
+ test "dashboard spending trend header treats current month final day as in progress" do
+ selected_month = (0..24).map { |i| i.months.ago.beginning_of_month.to_date }
+ .find { |m| (m - 1.month).end_of_month.day > m.end_of_month.day }
+ previous_month = (selected_month - 1.month).beginning_of_month
+
+ travel_to selected_month.end_of_month do
+ account = @family.accounts.create!(name: "Spending Trend Final Day Checking", currency: @family.currency, balance: 0, accountable: Depository.new)
+
+ create_transaction(account: account, name: "Prev early", amount: 111, date: previous_month)
+ create_transaction(account: account, name: "Prev extra day", amount: 999, date: previous_month.end_of_month)
+ create_transaction(account: account, name: "Current", amount: 7, date: Date.current)
+
+ get root_path, params: { spending_month: selected_month.iso8601 }
+ assert_response :ok
+
+ previous_series = spending_trend_chart_data.fetch("previous")
+ _current_total, previous_total = spending_trend_header_totals
+
+ assert_equal previous_month.end_of_month.iso8601, previous_series.last.fetch("date")
+ assert_equal money_text(111), previous_total
+ refute_equal money_text(previous_series.last.fetch("value")), previous_total
+ assert_select "#spending-trend-section span",
+ text: I18n.t("pages.dashboard.spending_trend.previous_comparison_days", end_day: Date.current.day)
+ end
+ end
+
+ test "dashboard spending trend header compares complete months once the month is over" do
+ account = @family.accounts.create!(name: "Spending Trend Past Checking", currency: @family.currency, balance: 0, accountable: Depository.new)
+ selected_month = 2.months.ago.beginning_of_month.to_date
+ previous_month = 3.months.ago.beginning_of_month.to_date
+
+ create_transaction(account: account, name: "Selected", amount: 50, date: selected_month)
+ create_transaction(account: account, name: "Previous early", amount: 111, date: previous_month)
+ create_transaction(account: account, name: "Previous late", amount: 999, date: previous_month.end_of_month)
+
+ get root_path, params: { spending_month: selected_month.iso8601 }
+ assert_response :ok
+
+ previous_series = spending_trend_chart_data.fetch("previous")
+ _current_total, previous_total = spending_trend_header_totals
+
+ # A month that has fully elapsed is still compared whole-to-whole.
+ assert_equal money_text(previous_series.last.fetch("value")), previous_total
+ # ...and the header does not claim to be comparing a partial range.
+ assert_select "#spending-trend-section span",
+ text: I18n.t("pages.dashboard.spending_trend.previous_comparison_days", end_day: selected_month.end_of_month.day),
+ count: 0
+ end
+
+ test "dashboard spending trend header compares complete months when the selected month is the shorter one" do
+ account = @family.accounts.create!(name: "Spending Trend Short Month Checking", currency: @family.currency, balance: 0, accountable: Depository.new)
+ # A past month shorter than the one before it (e.g. February after January),
+ # where clamping to the shorter series would truncate the previous month.
+ selected_month = (1..11).map { |i| i.months.ago.beginning_of_month.to_date }
+ .find { |m| (m - 1.month).end_of_month.day > m.end_of_month.day }
+ previous_month = (selected_month - 1.month).beginning_of_month
+
+ create_transaction(account: account, name: "Selected", amount: 50, date: selected_month)
+ create_transaction(account: account, name: "Previous early", amount: 111, date: previous_month)
+ create_transaction(account: account, name: "Previous last day", amount: 999, date: previous_month.end_of_month)
+
+ get root_path, params: { spending_month: selected_month.iso8601 }
+ assert_response :ok
+
+ previous_series = spending_trend_chart_data.fetch("previous")
+ _current_total, previous_total = spending_trend_header_totals
+
+ # Both months are over, so both are compared whole - the selected month
+ # being shorter must not truncate the previous month's total.
+ assert_equal previous_month.end_of_month.iso8601, previous_series.last.fetch("date")
+ assert_equal 1110.0, previous_series.last.fetch("value")
+ assert_equal money_text(previous_series.last.fetch("value")), previous_total
+ assert_select "#spending-trend-section span",
+ text: I18n.t("pages.dashboard.spending_trend.previous_comparison_days", end_day: selected_month.end_of_month.day),
+ count: 0
+ end
+
+ test "dashboard spending trend header clamps to a previous month shorter than today" do
+ # A month longer than the one before it (e.g. March after February), frozen
+ # to a day that the previous month never reaches.
+ selected_month = (1..24).map { |i| i.months.ago.beginning_of_month.to_date }
+ .find { |m| (m - 1.month).end_of_month.day < m.end_of_month.day - 1 }
+ previous_month = (selected_month - 1.month).beginning_of_month
+
+ # The day after the previous month's last day, which the selected month
+ # always reaches because it is the longer of the two.
+ travel_to selected_month + previous_month.end_of_month.day.days do
+ account = @family.accounts.create!(name: "Spending Trend Clamp Checking 2", currency: @family.currency, balance: 0, accountable: Depository.new)
+ create_transaction(account: account, name: "Prev", amount: 40, date: previous_month.end_of_month)
+ create_transaction(account: account, name: "Current", amount: 5, date: selected_month)
+
+ assert_operator Date.current.day, :>, previous_month.end_of_month.day
+ assert_operator Date.current, :<, selected_month.end_of_month
+
+ get root_path, params: { spending_month: selected_month.iso8601 }
+ assert_response :ok
+
+ previous_series = spending_trend_chart_data.fetch("previous")
+ _current_total, previous_total = spending_trend_header_totals
+
+ # There is no day 30 in February: the previous month has fully elapsed, so
+ # its complete total is the comparison. Without the clamp this read past
+ # the end of the series and showed zero.
+ assert_equal money_text(previous_series.last.fetch("value")), previous_total
+ refute_equal money_text(0), previous_total
+ end
+ end
+
private
def money_flow_bars
JSON.parse(css_select("[data-controller='bar-chart']").first["data-bar-chart-data-value"])
@@ -554,4 +708,18 @@ class PagesControllerTest < ActionDispatch::IntegrationTest
def spending_trend_chart_data
JSON.parse(css_select("[data-controller='spending-chart']").first["data-spending-chart-data-value"])
end
+
+ # The two large figures in the widget header: the selected month's
+ # month-to-date total and the previous month's comparison total.
+ def spending_trend_header_totals
+ css_select("#spending-trend-section .text-lg").map { |node| node.text.strip }
+ end
+
+ def spending_trend_header_delta
+ css_select("#spending-trend-section span.text-sm.tabular-nums").first.text.strip
+ end
+
+ def money_text(amount)
+ ApplicationController.helpers.format_money(Money.new(amount, @family.currency))
+ end
end