class Synchronisable::Worker::Associations

Responsible for associations synchronization.

@api private

Private Instance Methods

can_sync_association?(association) click to toggle source
# File lib/synchronisable/worker/associations.rb, line 60
def can_sync_association?(association)
  @includes.nil? || (
    @includes.try(:include?, association.name) ||
    @includes == association.name
  )
end
child_association_options(association) click to toggle source
# File lib/synchronisable/worker/associations.rb, line 67
def child_association_options(association)
  default = @includes.nil? ? nil : {}
  child_includes = @includes.try(:fetch, association.name) || default

  {
    :parent => @source,
    :includes => child_includes
  }
end
find_import(id, association) click to toggle source
# File lib/synchronisable/worker/associations.rb, line 77
def find_import(id, association)
  Import.where(
    :remote_id => id.to_s,
    :synchronisable_type => association.model.to_s
  ).first
end
sync_child_association(id, association) click to toggle source
# File lib/synchronisable/worker/associations.rb, line 47
def sync_child_association(id, association)
  return unless can_sync_association?(association)

  log_info("synchronizing child association with id: #{id}", :blue)

  @synchronizer.with_association_sync_callbacks(@source, id, association) do
    data = @source.data.try(:merge, { id: id }) || id

    Controller.call(association.model, data,
      child_association_options(association))
  end
end
sync_parent_association(id, association) click to toggle source
# File lib/synchronisable/worker/associations.rb, line 29
def sync_parent_association(id, association)
  log_info("synchronizing parent association with id: #{id}", :blue)

  @synchronizer.with_association_sync_callbacks(@source, id, association) do
    import_record = find_import(id, association)

    if import_record.nil? || association.force_sync
      data = @source.data.try(:merge, { id: id }) || id
      Controller.call(association.model, data, { :parent => @source })

      import_record = find_import(id, association)
    end

    @source.local_attrs[association.key] = import_record
      .try(:synchronisable).try(:id) # yep, it can happen
  end
end