2024-11-15 13:49:37 -05:00
|
|
|
module Syncable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :syncs, as: :syncable, dependent: :destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def syncing?
|
|
|
|
syncs.where(status: [ :syncing, :pending ]).any?
|
|
|
|
end
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def sync_later(start_date: nil, parent_sync: nil)
|
|
|
|
new_sync = syncs.create!(start_date: start_date, parent: parent_sync)
|
2024-11-15 13:49:37 -05:00
|
|
|
SyncJob.perform_later(new_sync)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync(start_date: nil)
|
|
|
|
syncs.create!(start_date: start_date).perform
|
|
|
|
end
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def sync_data(sync, start_date: nil)
|
2024-11-15 13:49:37 -05:00
|
|
|
raise NotImplementedError, "Subclasses must implement the `sync_data` method"
|
|
|
|
end
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def post_sync(sync)
|
2024-11-20 11:01:52 -05:00
|
|
|
# no-op, syncable can optionally provide implementation
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def sync_error
|
2025-05-09 14:56:49 -04:00
|
|
|
latest_sync&.error
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_synced_at
|
|
|
|
latest_sync&.last_ran_at
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-05-13 16:14:29 -04:00
|
|
|
def last_sync_created_at
|
|
|
|
latest_sync&.created_at
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
private
|
|
|
|
def latest_sync
|
|
|
|
syncs.order(created_at: :desc).first
|
|
|
|
end
|
|
|
|
end
|