Module: Motor::Configs::SyncFromHash

Defined in:
lib/motor/configs/sync_from_hash.rb

Class Method Summary collapse

Class Method Details

.archive_taggable_items(records_to_remove, configs_timestamp) ⇒ Object


117
118
119
120
121
122
123
# File 'lib/motor/configs/sync_from_hash.rb', line 117

def archive_taggable_items(records_to_remove, configs_timestamp)
  records_to_remove.each do |record|
    next if record.updated_at > configs_timestamp

    record.update!(deleted_at: Time.current) if record.deleted_at.blank?
  end
end

.call(configs_hash) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/motor/configs/sync_from_hash.rb', line 8

def call(configs_hash)
  return if configs_hash.blank?

  configs_hash = configs_hash.with_indifferent_access

  Motor::ApplicationRecord.transaction do
    sync_queries(configs_hash)
    sync_alerts(configs_hash)
    sync_dashboards(configs_hash)
    sync_forms(configs_hash)
    sync_configs(configs_hash)
    sync_resources(configs_hash)
  end
end

.create_taggable_items(create_items, records_class, update_proc) ⇒ Object


109
110
111
112
113
114
115
# File 'lib/motor/configs/sync_from_hash.rb', line 109

def create_taggable_items(create_items, records_class, update_proc)
  create_items.each do |attrs|
    record = records_class.find_or_initialize_by(id: attrs[:id]).tap { |e| e.deleted_at = nil }

    update_proc.call(record, attrs, force_replace: true)
  end
end

.sync_alerts(configs_hash) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/motor/configs/sync_from_hash.rb', line 32

def sync_alerts(configs_hash)
  sync_taggable(
    Motor::Configs::LoadFromCache.load_alerts,
    configs_hash[:alerts],
    configs_hash[:file_version],
    Motor::Alerts::Persistance.method(:update_from_params!)
  )
end

.sync_configs(configs_hash) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/motor/configs/sync_from_hash.rb', line 59

def sync_configs(configs_hash)
  configs_index = Motor::Configs::LoadFromCache.load_configs.index_by(&:key)

  configs_hash[:configs].each do |attrs|
    record = configs_index[attrs[:key]] || Motor::Config.new

    next if record.updated_at && attrs[:updated_at] <= record.updated_at

    record.update!(attrs)
  end
end

.sync_dashboards(configs_hash) ⇒ Object


50
51
52
53
54
55
56
57
# File 'lib/motor/configs/sync_from_hash.rb', line 50

def sync_dashboards(configs_hash)
  sync_taggable(
    Motor::Configs::LoadFromCache.load_dashboards,
    configs_hash[:dashboards],
    configs_hash[:file_version],
    Motor::Dashboards::Persistance.method(:update_from_params!)
  )
end

.sync_forms(configs_hash) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/motor/configs/sync_from_hash.rb', line 41

def sync_forms(configs_hash)
  sync_taggable(
    Motor::Configs::LoadFromCache.load_forms,
    configs_hash[:forms],
    configs_hash[:file_version],
    Motor::Forms::Persistance.method(:update_from_params!)
  )
end

.sync_queries(configs_hash) ⇒ Object


23
24
25
26
27
28
29
30
# File 'lib/motor/configs/sync_from_hash.rb', line 23

def sync_queries(configs_hash)
  sync_taggable(
    Motor::Configs::LoadFromCache.load_queries,
    configs_hash[:queries],
    configs_hash[:file_version],
    Motor::Queries::Persistance.method(:update_from_params!)
  )
end

.sync_resources(configs_hash) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/motor/configs/sync_from_hash.rb', line 71

def sync_resources(configs_hash)
  resources_index = Motor::Configs::LoadFromCache.load_resources.index_by(&:name)

  configs_hash[:resources].each do |attrs|
    record = resources_index[attrs[:name]] || Motor::Resource.new

    next if record.updated_at && attrs[:updated_at] <= record.updated_at

    record.update!(attrs)
  end
end

.sync_taggable(records, config_items, configs_timestamp, update_proc) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'lib/motor/configs/sync_from_hash.rb', line 83

def sync_taggable(records, config_items, configs_timestamp, update_proc)
  processed_records, create_items = update_taggable_items(records, config_items, update_proc)

  create_taggable_items(create_items, records.klass, update_proc)

  archive_taggable_items(records - processed_records, configs_timestamp)

  ActiveRecordUtils.reset_id_sequence!(records.klass)
end

.update_taggable_items(records, config_items, update_proc) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/motor/configs/sync_from_hash.rb', line 93

def update_taggable_items(records, config_items, update_proc)
  record_ids_hash = records.index_by(&:id)

  config_items.each_with_object([[], []]) do |attrs, (processed_acc, create_acc)|
    record = record_ids_hash[attrs[:id]]

    next create_acc << attrs unless record

    processed_acc << record if record

    next if record.updated_at >= attrs[:updated_at]

    update_proc.call(record, attrs, force_replace: true)
  end
end