Module: SyncableModels::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/syncable_models/controller.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BATCH_COUNT =
50

Instance Method Summary collapse

Instance Method Details

#do_authorize_by_keyObject



74
75
76
77
78
# File 'lib/syncable_models/controller.rb', line 74

def do_authorize_by_key
  if params[:key] != api_key
    return render json: { status: 401, message: 'Unauthorized'}
  end
end

#fetch(klass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/syncable_models/controller.rb', line 31

def fetch(klass)
  if params[:destination]
    count = params[:count].present? ? params[:count].to_i : BATCH_COUNT

    for_sync = klass.syncable_models_suitable.not_synced(params[:destination]).limit(count).map(&:to_import_hash)
    count = count - for_sync.count

    for_destroy = SyncableModels::Sync
      .by_destination(params[:destination])
      .for_destroying
      .limit(count)
      .map(&:subject_external_id)

    render json: { status: 200, for_sync: for_sync, for_destroy: for_destroy }
  else
    render_argument_error
  end
end

#render_argument_errorObject



70
71
72
# File 'lib/syncable_models/controller.rb', line 70

def render_argument_error
  return render json: { status: 400, message: 'Not enough arguments' }
end

#set_synced(klass, id_key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/syncable_models/controller.rb', line 50

def set_synced(klass, id_key)
  if params[:ids] && params[:destination]
    ids = params[:ids]

    destruction_syncs = SyncableModels::Sync
      .by_destination(params[:destination])
      .for_destroying
      .where(subject_external_id: ids)

    destruction_sync_ids = destruction_syncs.pluck(:subject_external_id).map(&:to_s)
    destruction_syncs.each(&:destroy)
    ids -= destruction_sync_ids

    klass.where(id_key => ids).sync(params[:destination])
    render json: { status: 200 }
  else
    render_argument_error
  end
end