diff --git a/app/models/account/sync_complete_event.rb b/app/models/account/sync_complete_event.rb index 32315375..d26b62f8 100644 --- a/app/models/account/sync_complete_event.rb +++ b/app/models/account/sync_complete_event.rb @@ -16,13 +16,13 @@ class Account::SyncCompleteEvent locals: { account: account } ) - # Replace the groups this account belongs to in the sidebar - account_group_ids.each do |id| + # Replace the groups this account belongs to in both desktop and mobile sidebars + sidebar_targets.each do |(tab, mobile_flag)| account.broadcast_replace_to( account.family, - target: id, + target: account_group.dom_id(tab: tab, mobile: mobile_flag), 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 @@ -37,18 +37,18 @@ class Account::SyncCompleteEvent end private - # The sidebar will show the account in both its classification tab and the "all" tab, - # so we need to broadcast to both. - def account_group_ids - unless account_group.present? - error = Error.new("Account #{account.id} is not part of an account group") - Rails.logger.warn(error.message) - Sentry.capture_exception(error, level: :warning) - return [] - end + # Returns an array of [tab, mobile?] tuples that should receive an update. + # We broadcast to both the classification-specific tab and the "all" tab, + # for desktop (mobile: false) and mobile (mobile: true) variants. + def sidebar_targets + return [] unless account_group.present? - 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 def account_group diff --git a/app/models/balance_sheet/account_group.rb b/app/models/balance_sheet/account_group.rb index 4dc68fa5..a4b23d1f 100644 --- a/app/models/balance_sheet/account_group.rb +++ b/app/models/balance_sheet/account_group.rb @@ -13,6 +13,22 @@ class BalanceSheet::AccountGroup @classification_group = classification_group 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 accountable_type.to_s.underscore end @@ -31,6 +47,11 @@ class BalanceSheet::AccountGroup accounts.any?(&:syncing?) end + # "asset" or "liability" + def classification + classification_group.classification + end + def currency classification_group.currency end diff --git a/app/models/sync.rb b/app/models/sync.rb index 7baf9e63..f551ca5a 100644 --- a/app/models/sync.rb +++ b/app/models/sync.rb @@ -65,6 +65,8 @@ class Sync < ApplicationRecord start! + sleep 10 + begin syncable.perform_sync(self) rescue => e diff --git a/app/views/accounts/_account_sidebar_tabs.html.erb b/app/views/accounts/_account_sidebar_tabs.html.erb index 28174cc9..182bdd6e 100644 --- a/app/views/accounts/_account_sidebar_tabs.html.erb +++ b/app/views/accounts/_account_sidebar_tabs.html.erb @@ -82,7 +82,7 @@
<% 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 %>
diff --git a/app/views/accounts/_accountable_group.html.erb b/app/views/accounts/_accountable_group.html.erb index 3417a3e2..26b69be3 100644 --- a/app/views/accounts/_accountable_group.html.erb +++ b/app/views/accounts/_accountable_group.html.erb @@ -1,6 +1,6 @@ -<%# locals: (account_group:, mobile: false, open: nil, **args) %> +<%# locals: (account_group:, mobile: false, all_tab: false, open: nil, **args) %> -
"> +
<% 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| %> <% disclosure.with_summary_content do %>