Class: WatermelonDbSync::SyncPush

Inherits:
Sync
  • Object
show all
Defined in:
lib/watermelon_db_sync/sync_push.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Sync

last_global_seqs, next_global_seqs

Constructor Details

#initialize(params) ⇒ SyncPush

Returns a new instance of SyncPush.



5
6
7
8
9
10
# File 'lib/watermelon_db_sync/sync_push.rb', line 5

def initialize(params)
  @models = WatermelonDbSync.configuration.sync_models
  @params = params
  @data = { last_global_seqs: 0 }
  @push_id = rand(1..1_000_000_000)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/watermelon_db_sync/sync_push.rb', line 3

def data
  @data
end

#modelsObject

Returns the value of attribute models.



3
4
5
# File 'lib/watermelon_db_sync/sync_push.rb', line 3

def models
  @models
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/watermelon_db_sync/sync_push.rb', line 3

def params
  @params
end

Instance Method Details

#pushObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/watermelon_db_sync/sync_push.rb', line 12

def push
  if has_conflict_version?
    @data[:success] = false
    @data[:error_code] = "WDBS2"
    @data[:message] = "Conflict version detected. Please pull first."
    return
  end

  ActiveRecord::Base.transaction do
    @models.each do |model_name|
      model = model_name.constantize
      next unless @params.key?(model.table_name)

      submit_records!(model)
    end

    sync_pull = SyncPull.new(
      last_pulled_version: @params["last_pulled_version"],
      push_id: @push_id
    )

    sync_pull.pull

    @data[:success] = true
    @data[:response] = sync_pull.data[:response]
    @data[:last_global_seqs] = sync_pull.data[:last_global_seqs]
  end
rescue => e
  @data[:success] = false
  @data[:message] = e.message
end