Method: Synced::Model#synchronize

Defined in:
lib/synced/model.rb

#synchronize(scope: scope_from_relation, strategy: synced_strategy, **options) ⇒ Object

Performs synchronization of given remote objects to local database.

website.rentals.synchronize(remote: remote_rentals)

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) (defaults to: scope_from_relation)
    • Within this object scope local objects

    will be synchronized. By default it’s model_class. Can be infered from active record association scope.

  • 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.

  • auto_paginate (Boolean)
    • If true (default) will fetch and save all

    records at once. If false will fetch and save records in batches.

  • transaction_per_page (Boolean)
    • If false (default) all fetched records

    will be persisted within single transaction. If true the transaction will be per page of fetched records

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

    remote objects



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/synced/model.rb', line 120

def synchronize(scope: scope_from_relation, strategy: synced_strategy, **options)
  options.assert_valid_keys(:api, :fields, :include, :remote, :remove, :query_params, :association_sync, :auto_paginate, :transaction_per_page)
  options[:remove]  = synced_remove unless options.has_key?(:remove)
  options[:include] = Array.wrap(synced_include) unless options.has_key?(:include)
  options[:fields]  = Array.wrap(synced_fields) unless options.has_key?(:fields)
  options[:query_params] = synced_query_params unless options.has_key?(:query_params)
  options[:auto_paginate] = synced_auto_paginate unless options.has_key?(:auto_paginate)
  options[:transaction_per_page] = synced_transaction_per_page unless options.has_key?(:transaction_per_page)
  options.merge!({
    scope:                 scope,
    id_key:                synced_id_key,
    synced_data_key:       synced_data_key,
    data_key:              synced_data_key,
    local_attributes:      synced_local_attributes,
    associations:          synced_associations,
    only_updated:          synced_only_updated,
    mapper:                synced_mapper,
    globalized_attributes: synced_globalized_attributes,
    initial_sync_since:    synced_initial_sync_since,
    timestamp_strategy:    synced_timestamp_strategy,
    handle_processed_objects_proc:  synced_handle_processed_objects_proc,
    tolerance:             synced_tolerance,
    synced_endpoint:       synced_endpoint
  })
  Synced::Synchronizer.new(self, strategy: strategy, **options).perform
end