From db181eb84fbb9ed9fdcf9c2acaa7efacd6ba3a61 Mon Sep 17 00:00:00 2001 From: "sentry[bot]" <39604003+sentry[bot]@users.noreply.github.com> Date: Thu, 27 Aug 2026 21:51:11 +0200 Subject: [PATCH] fix(sparklines): prevent crash when trend is nil for empty series (#3171) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sparklines): prevent crash when trend is nil for empty series * Test sparklines without trend data --------- Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> Co-authored-by: Juan José Mata --- app/views/accountable_sparklines/show.html.erb | 2 +- app/views/accounts/sparkline.html.erb | 2 +- .../accountable_sparklines_controller_test.rb | 16 ++++++++++++++++ test/controllers/accounts_controller_test.rb | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/views/accountable_sparklines/show.html.erb b/app/views/accountable_sparklines/show.html.erb index 05b0339c0..54072ad64 100644 --- a/app/views/accountable_sparklines/show.html.erb +++ b/app/views/accountable_sparklines/show.html.erb @@ -5,7 +5,7 @@ <%# A previous value of zero makes the percentage infinite, with nothing useful to print %> - <% if @series.trend.percent.finite? %> + <% if @series.trend && @series.trend.percent.finite? %> <%= tag.p @series.trend.percent_formatted, style: "color: #{@series.trend.color}", class: "font-mono text-right text-xs font-medium text-primary" %> diff --git a/app/views/accounts/sparkline.html.erb b/app/views/accounts/sparkline.html.erb index ba8339049..f2e75272a 100644 --- a/app/views/accounts/sparkline.html.erb +++ b/app/views/accounts/sparkline.html.erb @@ -5,7 +5,7 @@ <%# A previous value of zero makes the percentage infinite, with nothing useful to print %> - <% if @sparkline_series.trend.percent.finite? %> + <% if @sparkline_series.trend && @sparkline_series.trend.percent.finite? %> <%= tag.p @sparkline_series.trend.percent_formatted, style: "color: #{@sparkline_series.trend.color}", class: "font-mono text-right text-xs font-medium text-primary" %> diff --git a/test/controllers/accountable_sparklines_controller_test.rb b/test/controllers/accountable_sparklines_controller_test.rb index 80399b42a..4dfd67834 100644 --- a/test/controllers/accountable_sparklines_controller_test.rb +++ b/test/controllers/accountable_sparklines_controller_test.rb @@ -10,6 +10,22 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test "show renders an empty series without a trend" do + empty_series = Series.new( + start_date: 1.day.ago.to_date, + end_date: Date.current, + interval: "1 day", + values: [] + ) + Rails.cache.clear + Balance::ChartSeriesBuilder.any_instance.expects(:balance_series).returns(empty_series) + + get accountable_sparkline_url("depository") + + assert_response :success + assert_select "p.font-mono", count: 0 + end + test "linked investment sparkline does not load full account records" do AccountProvider.create!( account: accounts(:investment), diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 9ae0139a4..66b3c0f03 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -345,6 +345,21 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test "sparkline renders an empty series without a trend" do + empty_series = Series.new( + start_date: 1.day.ago.to_date, + end_date: Date.current, + interval: "1 day", + values: [] + ) + Account.any_instance.expects(:sparkline_series).returns(empty_series) + + get sparkline_account_url(@account) + + assert_response :success + assert_select "p.font-mono", count: 0 + end + test "destroys account" do delete account_url(@account) assert_redirected_to accounts_path