The personal finance app for everyone (by everyone)
Ruby
fix(sparklines): prevent crash when trend is nil for empty series (#3171)
sentry[bot]db181eb84fbb
diff --git a/app/views/accountable_sparklines/show.html.erb b/app/views/accountable_sparklines/show.html.erbindex 05b0339c03..54072ad642 100644--- a/app/views/accountable_sparklines/show.html.erb+++ b/app/views/accountable_sparklines/show.html.erb@@ -5,7 +5,7 @@</div><%# 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.erbindex ba8339049b..f2e75272a7 100644--- a/app/views/accounts/sparkline.html.erb+++ b/app/views/accounts/sparkline.html.erb@@ -5,7 +5,7 @@</div><%# 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.rbindex 80399b42a8..4dfd678341 100644--- a/test/controllers/accountable_sparklines_controller_test.rb+++ b/test/controllers/accountable_sparklines_controller_test.rb@@ -10,6 +10,22 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTestassert_response :successend+ 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" doAccountProvider.create!(account: accounts(:investment),diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 9ae0139a48..66b3c0f03a 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -345,6 +345,21 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_response :successend+ 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" dodelete account_url(@account)assert_redirected_to accounts_path