Module: Synced::Model
- Defined in:
- lib/synced/model.rb
Instance Method Summary collapse
-
#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.
-
#synced(strategy: :updated_since, **options) ⇒ Object
Enables synced for ActiveRecord model.
-
#synchronize(scope: scope_from_relation, strategy: synced_strategy, **options) ⇒ Object
Performs synchronization of given remote objects to local database.
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) = { scope: scope, only_updated: synced_only_updated, initial_sync_since: synced_initial_sync_since, timestamp_strategy: , synced_endpoint: synced_endpoint } Synced::Synchronizer.new(self, strategy: synced_strategy, **).reset_synced end |
#synced(strategy: :updated_since, **options) ⇒ Object
Enables synced for ActiveRecord model.
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, **) .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 = .fetch(:id_key, :synced_id) self.synced_data_key = .fetch(:data_key) { synced_column_presence(:synced_data) } self.synced_local_attributes = .fetch(:local_attributes, []) self.synced_associations = .fetch(:associations, []) self.synced_only_updated = .fetch(:only_updated, synced_strategy == :updated_since) self.synced_mapper = .fetch(:mapper, nil) self.synced_remove = .fetch(:remove, false) self.synced_include = .fetch(:include, []) self.synced_fields = .fetch(:fields, []) self.synced_globalized_attributes = .fetch(:globalized_attributes, []) self.synced_initial_sync_since = .fetch(:initial_sync_since, nil) self.synced_delegate_attributes = .fetch(:delegate_attributes, []) self.synced_query_params = .fetch(:query_params, {}) self. = .fetch(:timestamp_strategy, nil) self.synced_auto_paginate = .fetch(:auto_paginate, true) self.synced_transaction_per_page = .fetch(:transaction_per_page, false) self.synced_handle_processed_objects_proc = .fetch(:handle_processed_objects_proc, nil) self.synced_tolerance = .fetch(:tolerance, 0).to_i.abs self.synced_endpoint = .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)
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, **) .assert_valid_keys(:api, :fields, :include, :remote, :remove, :query_params, :association_sync, :auto_paginate, :transaction_per_page) [:remove] = synced_remove unless .has_key?(:remove) [:include] = Array.wrap(synced_include) unless .has_key?(:include) [:fields] = Array.wrap(synced_fields) unless .has_key?(:fields) [:query_params] = synced_query_params unless .has_key?(:query_params) [:auto_paginate] = synced_auto_paginate unless .has_key?(:auto_paginate) [:transaction_per_page] = synced_transaction_per_page unless .has_key?(:transaction_per_page) .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: , handle_processed_objects_proc: synced_handle_processed_objects_proc, tolerance: synced_tolerance, synced_endpoint: synced_endpoint }) Synced::Synchronizer.new(self, strategy: strategy, **).perform end |