Module: Synced::Engine::Model

Defined in:
lib/synced/engine/model.rb

Instance Method Summary collapse

Instance Method Details

#synced(options = {}) ⇒ Object

Enables synced for ActiveRecord model.

Parameters:

  • options (Hash) (defaults to: {})

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

Options Hash (options):

  • id_key: (Symbol)

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

  • synced_all_at_key: (Symbol)

    attribute name under which last synchronization time is stored, default is :synced_all_at. It’s only used when only_updated option is enabled.

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

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



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/synced/engine/model.rb', line 20

def synced(options = {})
  class_attribute :synced_id_key, :synced_all_at_key, :synced_data_key,
    :synced_local_attributes, :synced_associations, :synced_only_updated
  self.synced_id_key           = options.fetch(:id_key, :synced_id)
  self.synced_all_at_key       = options.fetch(:synced_all_at_key,
    :synced_all_at)
  self.synced_data_key         = options.fetch(:data_key, :synced_data)
  self.synced_local_attributes = options.fetch(:local_attributes, [])
  self.synced_associations     = options.fetch(:associations, [])
  self.synced_only_updated     = options.fetch(:only_updated, false)
  include Synced::Engine::HasSyncedData
end

#synchronize(remote: nil, model_class: self, scope: nil, remove: false) ⇒ 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) (defaults to: nil)
    • Remote objects to be synchronized with local db. If

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

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

    will be synchronized.

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

    will be synchronized. By default it’s model_class.

  • remove (Boolean) (defaults to: false)
    • 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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/synced/engine/model.rb', line 59

def synchronize(remote: nil, model_class: self, scope: nil, remove: false)
  options = {
    scope: scope,
    id_key: synced_id_key,
    synced_all_at_key: synced_all_at_key,
    data_key: synced_data_key,
    remove: remove,
    local_attributes: synced_local_attributes,
    associations: synced_associations,
    only_updated: synced_only_updated
  }
  synchronizer = Synced::Engine::Synchronizer.new(remote, model_class,
    options)
  synchronizer.perform
end