diff --git a/app/controllers/accountable_sparklines_controller.rb b/app/controllers/accountable_sparklines_controller.rb
index 832b0a09..38b80cca 100644
--- a/app/controllers/accountable_sparklines_controller.rb
+++ b/app/controllers/accountable_sparklines_controller.rb
@@ -2,6 +2,7 @@ class AccountableSparklinesController < ApplicationController
def show
@accountable = Accountable.from_type(params[:accountable_type]&.classify)
+ # Pre-load the series to catch any errors before rendering
@series = Rails.cache.fetch(cache_key) do
account_ids = family.accounts.active.where(accountable_type: @accountable.name).pluck(:id)
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index d3fbb00e..2477be98 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -23,6 +23,8 @@ class AccountsController < ApplicationController
end
def sparkline
+ # Pre-load the sparkline series to catch any errors before rendering
+ @sparkline_series = @account.sparkline_series
render layout: false
rescue => e
Rails.logger.error "Sparkline error for account #{@account.id}: #{e.message}"
diff --git a/app/views/accounts/sparkline.html.erb b/app/views/accounts/sparkline.html.erb
index ee7f743b..5eb8aa04 100644
--- a/app/views/accounts/sparkline.html.erb
+++ b/app/views/accounts/sparkline.html.erb
@@ -2,11 +2,11 @@
<%= turbo_frame_tag dom_id(@account, :sparkline) do %>
- <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
+ <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @sparkline_series %>
- <%= tag.p @account.sparkline_series.trend.percent_formatted,
- style: "color: #{@account.sparkline_series.trend.color}",
+ <%= tag.p @sparkline_series.trend.percent_formatted,
+ style: "color: #{@sparkline_series.trend.color}",
class: "font-mono text-right text-xs font-medium text-primary" %>
<% end %>
diff --git a/test/controllers/accountable_sparklines_controller_test.rb b/test/controllers/accountable_sparklines_controller_test.rb
new file mode 100644
index 00000000..b43c09e0
--- /dev/null
+++ b/test/controllers/accountable_sparklines_controller_test.rb
@@ -0,0 +1,21 @@
+require "test_helper"
+
+class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ sign_in @user = users(:family_admin)
+ end
+
+ test "should get show for depository" do
+ get accountable_sparkline_url("depository")
+ assert_response :success
+ end
+
+ test "should handle sparkline errors gracefully" do
+ # Mock an error in the balance_series method
+ Balance::ChartSeriesBuilder.any_instance.stubs(:balance_series).raises(StandardError.new("Test error"))
+
+ get accountable_sparkline_url("depository")
+ assert_response :success
+ assert_match /Error/, response.body
+ end
+end
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index e81cf7d0..6c51c525 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -27,11 +27,11 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end
test "should handle sparkline errors gracefully" do
- # Mock an error in the sparkline_series method
- @account.stubs(:sparkline_series).raises(StandardError.new("Test error"))
+ # Mock an error in the balance_series method to bypass the rescue in sparkline_series
+ Balance::ChartSeriesBuilder.any_instance.stubs(:balance_series).raises(StandardError.new("Test error"))
- get sparkline_account_url(@account)
- assert_response :success
- assert_match /Error/, response.body
- end
+ get sparkline_account_url(@account)
+ assert_response :success
+ assert_match /Error/, response.body
+end
end