lib/syncify/sync.rb in syncify-0.1.4 vs lib/syncify/sync.rb in syncify-0.1.5
- old
+ new
@@ -1,33 +1,53 @@
# frozen_string_literal: true
module Syncify
class Sync < ActiveInteraction::Base
object :klass, class: Class
- integer :id
+ integer :id, default: nil
+ object :where, class: Object, default: nil
object :association, class: Object, default: []
object :callback, class: Proc, default: nil
symbol :remote_database
attr_accessor :identified_records
+ validate :id_xor_where_present?
+
def execute
puts 'Identifying records to sync...'
@identified_records = Set[]
remote do
- identify_associated_records(klass.find(id), normalized_associations(association))
+ initial_query.each do |root_record|
+ identify_associated_records(root_record, normalized_associations(association))
+ end
end
puts "Identified #{identified_records.size} records to sync."
callback.call(identified_records) if callback.present?
sync_records
end
private
+
+ def initial_query
+ if id?
+ klass.where(id: id)
+ else
+ klass.where(where)
+ end
+ end
+
+ def id_xor_where_present?
+ unless id? ^ where?
+ errors.add(:id,
+ 'Please provide either the id argument or the where argument, but not both.')
+ end
+ end
def print_status
print "\rIdentified #{identified_records.size} records..."
end