Method: Synced::Model#synchronize

Defined in:
lib/synced/model.rb

#synchronize(options = {}) ⇒ Object

Performs synchronization of given remote objects to local database.

Rental.synchronize(remote: remote_rentals, scope: website)

Examples:

Synchronizing amenities


Amenity.synchronize(remote: [remote_amenity1, remote_amenity2])

Synchronizing rentals within given website. This will

create/remove/update rentals only within website.
It requires relation website.rentals to exist.

Parameters:

  • remote (Array)
    • Remote objects to be synchronized with local db. If

    it’s nil then synchronizer will make request on it’s own.

  • model_class (Class)
    • ActiveRecord model class to which remote objects

    will be synchronized.

  • scope (ActiveRecord::Base)
    • Within this object scope local objects

    will be synchronized. By default it’s model_class.

  • remove (Boolean)
    • If it’s true all local objects within

    current scope which are not present in the remote array will be destroyed. If only_updated is enabled, ids of objects to be deleted will be taken from the meta part. By default if cancel_at column is present, all missing local objects will be canceled with cancel_all, if it’s missing, all will be destroyed with destroy_all. You can also force method to remove local objects by passing it to remove: :mark_as_missing. This option can be defined in the model and then overwritten in the synchronize method.

  • api (BookingSync::API::Client)
    • API client to be used for fetching

    remote objects



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/synced/model.rb', line 83

def synchronize(options = {})
  options.symbolize_keys!
  options.assert_valid_keys(:api, :fields, :include, :remote, :remove,
    :scope)
  options[:remove]  = synced_remove unless options.has_key?(:remove)
  options[:include] = Array(synced_include) unless options.has_key?(:include)
  options[:fields]  = Array(synced_fields) unless options.has_key?(:fields)
  options.merge!({
    id_key:            synced_id_key,
    synced_data_key:   synced_data_key,
    synced_all_at_key: synced_all_at_key,
    data_key:          synced_data_key,
    local_attributes:  synced_local_attributes,
    associations:      synced_associations,
    only_updated:      synced_only_updated,
    mapper:            synced_mapper
  })
  Synced::Synchronizer.new(self, options).perform
end