From 3207077f9e1accd25f7306d07adff5bedf1ed262 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Sat, 10 May 2025 16:38:47 -0400 Subject: [PATCH] Put sync implementations in own concerns --- app/models/account.rb | 21 --------------------- app/models/account/syncable.rb | 26 ++++++++++++++++++++++++++ app/models/family/syncable.rb | 3 +-- app/models/plaid_item.rb | 23 ----------------------- app/models/plaid_item/syncable.rb | 28 ++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 46 deletions(-) create mode 100644 app/models/account/syncable.rb create mode 100644 app/models/plaid_item/syncable.rb diff --git a/app/models/account.rb b/app/models/account.rb index 352335e0..ee2fc963 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -81,21 +81,6 @@ class Account < ApplicationRecord DestroyJob.perform_later(self) end - def sync_data(sync, start_date: nil) - Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})") - sync_balances - end - - def post_sync(sync) - family.remove_syncing_notice! - - accountable.post_sync(sync) - - unless sync.child? - family.auto_match_transfers! - end - end - def current_holdings holdings.where(currency: currency, date: holdings.maximum(:date)).order(amount: :desc) end @@ -172,10 +157,4 @@ class Account < ApplicationRecord def long_subtype_label accountable_class.long_subtype_label_for(subtype) || accountable_class.display_name end - - private - def sync_balances - strategy = linked? ? :reverse : :forward - Balance::Syncer.new(self, strategy: strategy).sync_balances - end end diff --git a/app/models/account/syncable.rb b/app/models/account/syncable.rb new file mode 100644 index 00000000..f4547ce9 --- /dev/null +++ b/app/models/account/syncable.rb @@ -0,0 +1,26 @@ +module Account::Syncable + extend ActiveSupport::Concern + + include Syncable + + def sync_data(sync, start_date: nil) + Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})") + sync_balances + end + + def post_sync(sync) + family.remove_syncing_notice! + + accountable.post_sync(sync) + + unless sync.child? + family.auto_match_transfers! + end + end + + private + def sync_balances + strategy = linked? ? :reverse : :forward + Balance::Syncer.new(self, strategy: strategy).sync_balances + end +end diff --git a/app/models/family/syncable.rb b/app/models/family/syncable.rb index 9b1c276a..8615a528 100644 --- a/app/models/family/syncable.rb +++ b/app/models/family/syncable.rb @@ -1,8 +1,7 @@ module Family::Syncable extend ActiveSupport::Concern - # Top-level, generic Syncable concern - include ::Syncable + include Syncable def sync_data(sync, start_date: nil) # We don't rely on this value to guard the app, but keep it eventually consistent diff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rb index 9bf91d70..c387dcc1 100644 --- a/app/models/plaid_item.rb +++ b/app/models/plaid_item.rb @@ -22,24 +22,6 @@ class PlaidItem < ApplicationRecord scope :ordered, -> { order(created_at: :desc) } scope :needs_update, -> { where(status: :requires_update) } - def sync_data(sync, start_date: nil) - begin - Rails.logger.info("Fetching and loading Plaid data") - fetch_and_load_plaid_data(sync) - update!(status: :good) if requires_update? - - # Schedule account syncs - accounts.each do |account| - account.sync_later(start_date: start_date, parent_sync: sync) - end - - Rails.logger.info("Plaid data fetched and loaded") - rescue Plaid::ApiError => e - handle_plaid_error(e) - raise e - end - end - def get_update_link_token(webhooks_url:, redirect_url:) begin family.get_link_token( @@ -61,11 +43,6 @@ class PlaidItem < ApplicationRecord end end - def post_sync(sync) - auto_match_categories! - family.broadcast_refresh - end - def destroy_later update!(scheduled_for_deletion: true) DestroyJob.perform_later(self) diff --git a/app/models/plaid_item/syncable.rb b/app/models/plaid_item/syncable.rb new file mode 100644 index 00000000..760887cf --- /dev/null +++ b/app/models/plaid_item/syncable.rb @@ -0,0 +1,28 @@ +module PlaidItem::Syncable + extend ActiveSupport::Concern + + include Syncable + + def sync_data(sync, start_date: nil) + begin + Rails.logger.info("Fetching and loading Plaid data") + fetch_and_load_plaid_data(sync) + update!(status: :good) if requires_update? + + # Schedule account syncs + accounts.each do |account| + account.sync_later(start_date: start_date, parent_sync: sync) + end + + Rails.logger.info("Plaid data fetched and loaded") + rescue Plaid::ApiError => e + handle_plaid_error(e) + raise e + end + end + + def post_sync(sync) + auto_match_categories! + family.broadcast_refresh + end +end