1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00
Maybe/app/models/concerns/syncable.rb

69 lines
1.1 KiB
Ruby
Raw Normal View History

module Syncable
extend ActiveSupport::Concern
included do
has_many :syncs, as: :syncable, dependent: :destroy
end
def syncing?
raise NotImplementedError, "Subclasses must implement the syncing? method"
end
2025-05-26 05:39:14 -04:00
def sync_later(parent_sync: nil)
Sync.transaction do
with_lock do
sync = self.syncs.incomplete.first
2025-05-26 05:39:14 -04:00
unless sync
sync = self.syncs.create!(parent: parent_sync)
end
2025-05-26 05:39:14 -04:00
SyncJob.perform_later(sync)
sync
end
end
end
def perform_sync(sync)
syncer.perform_sync(sync)
end
def perform_post_sync
syncer.perform_post_sync
end
def broadcast_sync_complete
sync_broadcaster.broadcast
2024-11-20 11:01:52 -05:00
end
def sync_error
latest_sync&.error
end
def last_synced_at
latest_sync&.completed_at
end
def last_sync_created_at
latest_sync&.created_at
end
2025-05-26 05:39:14 -04:00
def needs_sync?
data_synced_through.nil? || data_synced_through < Date.current
end
private
def latest_sync
syncs.ordered.first
end
def syncer
self.class::Syncer.new(self)
end
def sync_broadcaster
self.class::SyncCompleteEvent.new(self)
end
end