Module: Synced::Model

Defined in:
lib/synced/model.rb

Instance Method Summary collapse

Instance Method Details

#reset_syncedObject

Reset synced_all_at 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.



113
114
115
116
# File 'lib/synced/model.rb', line 113

def reset_synced
  return unless synced_only_updated
  update_all(synced_all_at_key => nil)
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/synced/model.rb', line 32

def synced(options = {})
  options.symbolize_keys!
  options.assert_valid_keys(:associations, :data_key, :fields,
    :globalized_attributes, :id_key, :include, :local_attributes, :mapper,
    :only_updated, :remove, :synced_all_at_key)
  class_attribute :synced_id_key, :synced_all_at_key, :synced_data_key,
    :synced_local_attributes, :synced_associations, :synced_only_updated,
    :synced_mapper, :synced_remove, :synced_include, :synced_fields,
    :synced_globalized_attributes
  self.synced_id_key                = options.fetch(:id_key, :synced_id)
  self.synced_all_at_key            = options.fetch(:synced_all_at_key,
    synced_column_presence(:synced_all_at))
  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,
    column_names.include?(synced_all_at_key.to_s))
  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,
    [])
  include Synced::HasSyncedData
end

#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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/synced/model.rb', line 88

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,
    globalized_attributes: synced_globalized_attributes
  })
  Synced::Synchronizer.new(self, options).perform
end