The personal finance app for everyone (by everyone)
Ruby
Stop broadcasting account rows to the whole family (#4050)
jaysbeekaya291ab5996b2
diff --git a/app/models/account/sync_complete_event.rb b/app/models/account/sync_complete_event.rbindex 79e8ec16cc..9c242b17f4 100644--- a/app/models/account/sync_complete_event.rb+++ b/app/models/account/sync_complete_event.rb@@ -8,19 +8,13 @@ class Account::SyncCompleteEventenddef broadcast- # Replace account row in accounts list- account.broadcast_replace_to(- account.family,- target: "account_#{account.id}",- partial: "accounts/account",- locals: { account: account }- )-- # If this is a manual, unlinked account (i.e. not part of a Plaid Item),- # trigger the family sync complete broadcast so net worth graph is updated- unless account.linked?- account.family.broadcast_sync_complete- end+ # The accounts list is not updated by replacing the row here: the row+ # shows the account's name and balance, and every family member holds+ # the family stream, including members the account is not shared with.+ # The sync toast makes each browser re-fetch its own page instead. It is+ # sent for linked accounts too, since one can sync on its own with no+ # provider event after it.+ account.family.broadcast_sync_complete# Refresh entire account page (only applies if currently viewing this account)account.broadcast_refreshdiff --git a/test/models/account/sync_complete_event_test.rb b/test/models/account/sync_complete_event_test.rbnew file mode 100644index 0000000000..1b7e871eda--- /dev/null+++ b/test/models/account/sync_complete_event_test.rb@@ -0,0 +1,54 @@+require "test_helper"+require "turbo/broadcastable/test_helper"++class Account::SyncCompleteEventTest < ActiveSupport::TestCase+ include Turbo::Broadcastable::TestHelper++ setup do+ @account = accounts(:depository)+ @account.update!(name: "Private Savings Row Marker")+ Current.reset+ end++ # The row carries the account's name and balance, and every family member+ # subscribes to the family stream, including members the account is not+ # shared with.+ test "an account sync does not broadcast the rendered row to the family" do+ streams = capture_turbo_stream_broadcasts(@account.family) do+ Account::SyncCompleteEvent.new(@account).broadcast+ end++ assert_not_includes streams.map { |stream| stream["target"] }, "account_#{@account.id}"+ streams.each { |stream| assert_not_includes stream.to_html, @account.name }+ end++ # A linked account can sync on its own, with no provider event after it, so+ # the toast is what refreshes /accounts for each viewer.+ test "a linked account syncing alone still sends the family toast" do+ @account.stubs(:linked?).returns(true)++ targets = capture_turbo_stream_broadcasts(@account.family) do+ Account::SyncCompleteEvent.new(@account).broadcast+ end.map { |stream| stream["target"] }++ assert_includes targets, "sync-toast"+ end++ test "an unlinked account still sends the family toast" do+ @account.stubs(:linked?).returns(false)++ targets = capture_turbo_stream_broadcasts(@account.family) do+ Account::SyncCompleteEvent.new(@account).broadcast+ end.map { |stream| stream["target"] }++ assert_includes targets, "sync-toast"+ end++ test "the account page still refreshes on its own stream" do+ actions = capture_turbo_stream_broadcasts(@account) do+ Account::SyncCompleteEvent.new(@account).broadcast+ end.map { |stream| stream["action"] }++ assert_includes actions, "refresh"+ end+end