Module: Restforce::DB::Model

Defined in:
lib/restforce/db/model.rb

Overview

Restforce::DB::Model is a helper module which attaches some special DSL-style methods to an ActiveRecord class, allowing for easier mapping of the ActiveRecord class to an object type in Salesforce.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



11
12
13
# File 'lib/restforce/db/model.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#force_sync!Object

Public: Force a synchronization to run for this specific record. If the record has not yet been pushed up to Salesforce, create it. In the event that the record has already been synchronized, force the data to be re- synchronized.

NOTE: To ensure that we aren’t attempting to synchronize data which has not actually been committed to the database, this method no-ops for unpersisted records, and discards all local changes to the record prior to syncing.

Returns a Boolean.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/restforce/db/model.rb', line 45

def force_sync!
  return false unless persisted?
  reload

  sync_instances.each do |instance|
    salesforce_record_type = instance.mapping.salesforce_record_type

    if instance.synced?
      salesforce_instance = salesforce_record_type.find(instance.id)
      next unless salesforce_instance

      accumulator = Restforce::DB::Accumulator.new
      accumulator.store(instance.last_update, instance.attributes)
      accumulator.store(salesforce_instance.last_update, salesforce_instance.attributes)

      synchronizer = Restforce::DB::Synchronizer.new(instance.mapping)
      synchronizer.update(instance, accumulator)
      synchronizer.update(salesforce_instance, accumulator)
    else
      salesforce_record_type.create!(instance)
    end
  end

  true
end