1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 07:25:19 +02:00

Fix turbo stream broadcasts

This commit is contained in:
Zach Gollwitzer 2025-06-10 18:09:13 -04:00
parent 4a81c41ebf
commit 77e425f0e0
5 changed files with 41 additions and 18 deletions

View file

@ -16,13 +16,13 @@ class Account::SyncCompleteEvent
locals: { account: account } locals: { account: account }
) )
# Replace the groups this account belongs to in the sidebar # Replace the groups this account belongs to in both desktop and mobile sidebars
account_group_ids.each do |id| sidebar_targets.each do |(tab, mobile_flag)|
account.broadcast_replace_to( account.broadcast_replace_to(
account.family, account.family,
target: id, target: account_group.dom_id(tab: tab, mobile: mobile_flag),
partial: "accounts/accountable_group", partial: "accounts/accountable_group",
locals: { account_group: account_group, open: true } locals: { account_group: account_group, open: true, all_tab: tab == :all, mobile: mobile_flag }
) )
end end
@ -37,18 +37,18 @@ class Account::SyncCompleteEvent
end end
private private
# The sidebar will show the account in both its classification tab and the "all" tab, # Returns an array of [tab, mobile?] tuples that should receive an update.
# so we need to broadcast to both. # We broadcast to both the classification-specific tab and the "all" tab,
def account_group_ids # for desktop (mobile: false) and mobile (mobile: true) variants.
unless account_group.present? def sidebar_targets
error = Error.new("Account #{account.id} is not part of an account group") return [] unless account_group.present?
Rails.logger.warn(error.message)
Sentry.capture_exception(error, level: :warning)
return []
end
id = account_group.id [
[ id, "#{account_group.classification}_#{id}" ] [ account_group.classification.to_sym, false ],
[ :all, false ],
[ account_group.classification.to_sym, true ],
[ :all, true ]
]
end end
def account_group def account_group

View file

@ -13,6 +13,22 @@ class BalanceSheet::AccountGroup
@classification_group = classification_group @classification_group = classification_group
end end
# A stable DOM id for this group.
# Example outputs:
# dom_id(tab: :asset) # => "asset_depository"
# dom_id(tab: :all, mobile: true) # => "mobile_all_depository"
#
# Keeping all of the logic here means the view layer and broadcaster only
# need to ask the object for its DOM id instead of rebuilding string
# fragments in multiple places.
def dom_id(tab: nil, mobile: false)
parts = []
parts << "mobile" if mobile
parts << (tab ? tab.to_s : classification.to_s)
parts << key
parts.compact.join("_")
end
def key def key
accountable_type.to_s.underscore accountable_type.to_s.underscore
end end
@ -31,6 +47,11 @@ class BalanceSheet::AccountGroup
accounts.any?(&:syncing?) accounts.any?(&:syncing?)
end end
# "asset" or "liability"
def classification
classification_group.classification
end
def currency def currency
classification_group.currency classification_group.currency
end end

View file

@ -65,6 +65,8 @@ class Sync < ApplicationRecord
start! start!
sleep 10
begin begin
syncable.perform_sync(self) syncable.perform_sync(self)
rescue => e rescue => e

View file

@ -82,7 +82,7 @@
<div> <div>
<% family.balance_sheet.account_groups.each do |group| %> <% family.balance_sheet.account_groups.each do |group| %>
<%= render "accounts/accountable_group", account_group: group, mobile: mobile %> <%= render "accounts/accountable_group", account_group: group, mobile: mobile, all_tab: true %>
<% end %> <% end %>
</div> </div>
</div> </div>

View file

@ -1,6 +1,6 @@
<%# locals: (account_group:, mobile: false, open: nil, **args) %> <%# locals: (account_group:, mobile: false, all_tab: false, open: nil, **args) %>
<div id="<%= mobile ? "mobile_#{account_group.key}" : account_group.key %>"> <div id="<%= account_group.dom_id(tab: all_tab ? :all : nil, mobile: mobile) %>">
<% is_open = open.nil? ? account_group.accounts.any? { |account| page_active?(account_path(account)) } : open %> <% is_open = open.nil? ? account_group.accounts.any? { |account| page_active?(account_path(account)) } : open %>
<%= render DisclosureComponent.new(title: account_group.name, align: :left, open: is_open) do |disclosure| %> <%= render DisclosureComponent.new(title: account_group.name, align: :left, open: is_open) do |disclosure| %>
<% disclosure.with_summary_content do %> <% disclosure.with_summary_content do %>