Class: Synced::Strategies::Check

Inherits:
Full
  • Object
show all
Defined in:
lib/synced/strategies/check.rb,
lib/synced/result_presenter.rb

Overview

This strategy doesn’t do any synchronization it simply verifies if local objects are in sync with the remote ones (taken from the API).

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Full

#reset_synced

Methods included from AttributesAsHash

#synced_attributes_as_hash

Constructor Details

#initialize(model_class, options = {}) ⇒ Check



8
9
10
11
# File 'lib/synced/strategies/check.rb', line 8

def initialize(model_class, options = {})
  super
  @result = Result.new(model_class, options)
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/synced/strategies/check.rb', line 6

def result
  @result
end

Instance Method Details

#performSynced::Strategies::Check::Result

Makes a DRY run of full synchronization. It checks and collects objects which

* are present in the local database, but not in the API. Local AR object is
   returned - additional objects
* are present in the API, but not in the local database, remote object is
   returned - missing objects
* are changed in the API, but not in the local database,
   ActiveRecord::Model #changes hash is returned - changed objects


21
22
23
24
25
# File 'lib/synced/strategies/check.rb', line 21

def perform
  process_remote_objects(remote_objects_tester)
  result.additional = remove_relation.to_a
  result
end

#relation_scopeObject

If we check model which uses cancel instead of destroy, we skip canceled when searching for additional objects by searching in :visible scope



57
58
59
# File 'lib/synced/strategies/check.rb', line 57

def relation_scope
  default_remove_strategy == :cancel_all ? super.visible : super
end

#remote_objects_testerObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/synced/strategies/check.rb', line 27

def remote_objects_tester
  lambda do |remote_objects_batch|
    remote_objects_batch_ids = remote_objects_batch.map(&:id)
    local_objects = relation_scope.where(@id_key => remote_objects_batch_ids)
    local_objects_hash = local_objects.each_with_object({}) do |local_object, hash|
      hash[local_object.public_send(@id_key)] = local_object
    end
    @remote_objects_ids.concat(remote_objects_batch_ids)

    remote_objects_batch.map do |remote|
      if local_object = local_objects_hash[remote.id]
        remote.extend(@mapper) if @mapper
        local_object.attributes = default_attributes_mapping(remote)
        local_object.attributes = local_attributes_mapping(remote)
        if @globalized_attributes.present?
          local_object.attributes = globalized_attributes_mapping(remote,
            local_object.translations.translated_locales)
        end
        if local_object.changed?
          result.changed << [{ id: local_object.id }, local_object.changes]
        end
      else
        result.missing << remote
      end
    end
  end
end