From f0e348c7288eb68c5bdd753e3b55f8b24e25463a Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Sat, 17 May 2025 18:24:19 -0400 Subject: [PATCH] Use `visible` sync logic in sidebar groups --- app/models/balance_sheet.rb | 6 +++++- app/models/sync.rb | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/balance_sheet.rb b/app/models/balance_sheet.rb index cc50e3da..a89a9859 100644 --- a/app/models/balance_sheet.rb +++ b/app/models/balance_sheet.rb @@ -107,7 +107,11 @@ class BalanceSheet def totals_query @totals_query ||= active_accounts .joins(ActiveRecord::Base.sanitize_sql_array([ "LEFT JOIN exchange_rates ON exchange_rates.date = CURRENT_DATE AND accounts.currency = exchange_rates.from_currency AND exchange_rates.to_currency = ?", currency ])) - .joins("LEFT JOIN syncs ON syncs.syncable_id = accounts.id AND syncs.syncable_type = 'Account' AND (syncs.status = 'pending' OR syncs.status = 'syncing')") + .joins(ActiveRecord::Base.sanitize_sql_array([ + "LEFT JOIN syncs ON syncs.syncable_id = accounts.id AND syncs.syncable_type = 'Account' AND syncs.status IN (?) AND syncs.created_at > ?", + %w[pending syncing], + Sync::VISIBLE_FOR.ago + ])) .select( "accounts.*", "SUM(accounts.balance * COALESCE(exchange_rates.rate, 1)) as converted_balance", diff --git a/app/models/sync.rb b/app/models/sync.rb index 7ee487d8..456a4b01 100644 --- a/app/models/sync.rb +++ b/app/models/sync.rb @@ -16,8 +16,9 @@ class Sync < ApplicationRecord has_many :children, class_name: "Sync", foreign_key: :parent_id, dependent: :destroy scope :ordered, -> { order(created_at: :desc) } - scope :incomplete, -> { where("syncs.status IN (?)", [ :pending, :syncing ]) } + scope :incomplete, -> { where("syncs.status IN (?)", %w[pending syncing]) } scope :visible, -> { incomplete.where("syncs.created_at > ?", VISIBLE_FOR.ago) } + # In-flight records that have exceeded the allowed runtime scope :stale_candidates, -> { incomplete.where("syncs.created_at < ?", STALE_AFTER.ago) }