Module: Synced::Model

Defined in:
lib/synced/model.rb

Instance Method Summary collapse

Instance Method Details

#reset_synced(scope: scope_from_relation) ⇒ Object

Reset last sync timestamp for given scope, this forces synced to sync all the records on the next sync. Useful for cases when you add a new column to be synced and you use updated since strategy for faster synchronization.



151
152
153
154
155
156
157
158
159
160
# File 'lib/synced/model.rb', line 151

def reset_synced(scope: scope_from_relation)
  options = {
    scope:                 scope,
    only_updated:          synced_only_updated,
    initial_sync_since:    synced_initial_sync_since,
    timestamp_strategy:    synced_timestamp_strategy,
    synced_endpoint:       synced_endpoint
  }
  Synced::Synchronizer.new(self,  strategy: synced_strategy, **options).reset_synced
end

#synced(strategy: :updated_since, **options) ⇒ Object

Enables synced for ActiveRecord model.

Parameters:

  • options (Hash)

    Configuration options for synced. They are inherited by subclasses, but can be overwritten in the subclass.

Options Hash (**options):

  • strategy: (Symbol)

    synchronization strategy, one of :full, :updated_since, :check. Defaults to :updated_since

  • id_key: (Symbol)

    attribute name under which remote object’s ID is stored, default is :synced_id.

  • only_updated: (Boolean)

    If true requests to API will take advantage of updated_since param and fetch only created/changed/deleted remote objects

  • data_key: (Symbol)

    attribute name under which remote object’s data is stored.

  • local_attributes: (Array|Hash)

    Array of attributes in the remote object which will be mapped to local object attributes.

  • remove: (Boolean|Symbol)

    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.

  • globalized_attributes: (Array|Hash)

    A list of attributes which will be mapped with their translations.

  • initial_sync_since: (Time|Proc)

    A point in time from which objects will be synchronized on first synchronization. Works only for partial (updated_since param) synchronizations.

  • delegate_attributes: (Array|Hash)

    Given attributes will be defined on synchronized object and delegated to synced_data Hash

  • query_params: (Hash)

    Given attributes and their values which will be passed to api client to perform search

  • 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

  • handle_processed_objects_proc: (Proc)

    Proc taking one argument (persisted remote objects). Called after persisting remote objects (once in case of auto_paginate, after each batch when paginating with block).

  • tolerance: (Integer)

    amount of seconds subtracted from last_request timestamp. Used to ensure records are up to date in case request has been made in the middle of transaction. Defaults to 0.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/synced/model.rb', line 49

def synced(strategy: :updated_since, **options)
  options.assert_valid_keys(:associations, :data_key, :fields, :globalized_attributes,
    :id_key, :include, :initial_sync_since, :local_attributes, :mapper, :only_updated,
    :remove, :auto_paginate, :transaction_per_page, :delegate_attributes, :query_params,
    :timestamp_strategy, :handle_processed_objects_proc, :tolerance, :endpoint)
  class_attribute :synced_id_key, :synced_data_key,
    :synced_local_attributes, :synced_associations, :synced_only_updated,
    :synced_mapper, :synced_remove, :synced_include, :synced_fields, :synced_auto_paginate, :synced_transaction_per_page,
    :synced_globalized_attributes, :synced_initial_sync_since, :synced_delegate_attributes,
    :synced_query_params, :synced_timestamp_strategy, :synced_strategy, :synced_handle_processed_objects_proc,
    :synced_tolerance, :synced_endpoint
  self.synced_strategy              = strategy
  self.synced_id_key                = options.fetch(:id_key, :synced_id)
  self.synced_data_key              = options.fetch(:data_key) { synced_column_presence(:synced_data) }
  self.synced_local_attributes      = options.fetch(:local_attributes, [])
  self.synced_associations          = options.fetch(:associations, [])
  self.synced_only_updated          = options.fetch(:only_updated, synced_strategy == :updated_since)
  self.synced_mapper                = options.fetch(:mapper, nil)
  self.synced_remove                = options.fetch(:remove, false)
  self.synced_include               = options.fetch(:include, [])
  self.synced_fields                = options.fetch(:fields, [])
  self.synced_globalized_attributes = options.fetch(:globalized_attributes,
    [])
  self.synced_initial_sync_since    = options.fetch(:initial_sync_since,
    nil)
  self.synced_delegate_attributes   = options.fetch(:delegate_attributes, [])
  self.synced_query_params          = options.fetch(:query_params, {})
  self.synced_timestamp_strategy    = options.fetch(:timestamp_strategy, nil)
  self.synced_auto_paginate         = options.fetch(:auto_paginate, true)
  self.synced_transaction_per_page  = options.fetch(:transaction_per_page, false)
  self.synced_handle_processed_objects_proc  = options.fetch(:handle_processed_objects_proc, nil)
  self.synced_tolerance             = options.fetch(:tolerance, 0).to_i.abs
  self.synced_endpoint              = options.fetch(:endpoint) { self.to_s.tableize }
  include Synced::DelegateAttributes
  include Synced::HasSyncedData
end

#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