The personal finance app for everyone (by everyone)
Ruby
class Family::Syncer
attr_reader :family
def initialize(family)
@family = family
end
def perform_sync(sync)
# We don't rely on this value to guard the app, but keep it eventually consistent
family.sync_trial_status!
# Schedule child syncs
child_syncables.each do |syncable|
# Cooperative cancellation: stop fanning out child syncs once a cancel
# has been requested (fresh read — the flag is set from another process).
break if sync.cancel_requested?
syncable.sync_later(parent_sync: sync, window_start_date: sync.window_start_date, window_end_date: sync.window_end_date)
end
end
def perform_post_sync
family.auto_match_transfers!
family.backfill_provider_merchant_logos
Rails.logger.info("Applying rules for family #{family.id}")
ApplyRulesJob.perform_later(family, warm_reports: true)
end
# Every `*_items` association whose model is Syncable, found by reflection
# so a new integration takes part in the nightly family sync (and may carry
# a SyncSchedule) as soon as it includes Syncable and exposes a `syncable`
# scope. This list also drives the destructive Family::FinancialDataReset, so
# an association added here is wiped by a reset as well.
def self.syncable_item_associations
Family.reflect_on_all_associations(:has_many).filter_map do |association|
next unless association.name.to_s.end_with?("_items")
next unless association.klass.included_modules.include?(Syncable)
association.name
rescue NameError
nil
end
end
private
def child_syncables
provider_items = self.class.syncable_item_associations.flat_map do |association|
family.public_send(association).syncable
end
provider_items + family.accounts.manual
end
end