Module: Motor::Configs::SyncFromHash
- Defined in:
- lib/motor/configs/sync_from_hash.rb
Class Method Summary collapse
- .archive_api_configs(configs_index, api_configs) ⇒ Object
- .archive_taggable_items(records_to_remove, configs_timestamp) ⇒ Object
- .call(configs_hash) ⇒ Object
- .create_taggable_items(create_items, records_class, update_proc) ⇒ Object
- .sync_alerts(configs_hash) ⇒ Object
- .sync_api_configs(configs_hash) ⇒ Object
- .sync_configs(configs_hash) ⇒ Object
- .sync_dashboards(configs_hash) ⇒ Object
- .sync_forms(configs_hash) ⇒ Object
- .sync_queries(configs_hash) ⇒ Object
- .sync_resources(configs_hash) ⇒ Object
- .sync_taggable(records, config_items, configs_timestamp, update_proc) ⇒ Object
- .update_taggable_items(records, config_items, update_proc) ⇒ Object
Class Method Details
.archive_api_configs(configs_index, api_configs) ⇒ Object
88 89 90 91 92 |
# File 'lib/motor/configs/sync_from_hash.rb', line 88 def archive_api_configs(configs_index, api_configs) configs_index.except(*api_configs.pluck('name')).each_value do |config| config.update!(deleted_at: Time.current) if config.deleted_at.blank? end end |
.archive_taggable_items(records_to_remove, configs_timestamp) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/motor/configs/sync_from_hash.rb', line 144 def archive_taggable_items(records_to_remove, ) records_to_remove.each do |record| next if record.updated_at > 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 22 |
# 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) sync_api_configs(configs_hash) end end |
.create_taggable_items(create_items, records_class, update_proc) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/motor/configs/sync_from_hash.rb', line 136 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
33 34 35 36 37 38 39 40 |
# File 'lib/motor/configs/sync_from_hash.rb', line 33 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_api_configs(configs_hash) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/motor/configs/sync_from_hash.rb', line 72 def sync_api_configs(configs_hash) return if configs_hash[:api_configs].blank? configs_index = Motor::Configs::LoadFromCache.load_api_configs.index_by(&:name) configs_hash[:api_configs].each do |attrs| record = configs_index[attrs[:name]] || Motor::ApiConfig.new next if record.updated_at && attrs[:updated_at] <= record.updated_at record.update!(attrs.merge(deleted_at: nil)) end archive_api_configs(configs_index, configs_hash[:api_configs]) end |
.sync_configs(configs_hash) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/motor/configs/sync_from_hash.rb', line 60 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
51 52 53 54 55 56 57 58 |
# File 'lib/motor/configs/sync_from_hash.rb', line 51 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
42 43 44 45 46 47 48 49 |
# File 'lib/motor/configs/sync_from_hash.rb', line 42 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
24 25 26 27 28 29 30 31 |
# File 'lib/motor/configs/sync_from_hash.rb', line 24 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/motor/configs/sync_from_hash.rb', line 94 def sync_resources(configs_hash) resources_index = Motor::Configs::LoadFromCache.load_resources.index_by(&:name) configs_hash[:resources].each do |attrs| record = resources_index.fetch(attrs[:name], Motor::Resource.new) next if record.updated_at && attrs[:updated_at] < record.updated_at next if record.updated_at && attrs[:updated_at] == record.updated_at && attrs[:preferences] == record.preferences record.updated_at_will_change! record.update!(attrs) end end |
.sync_taggable(records, config_items, configs_timestamp, update_proc) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/motor/configs/sync_from_hash.rb', line 110 def sync_taggable(records, config_items, , 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, ) ActiveRecordUtils.reset_id_sequence!(records.klass) end |
.update_taggable_items(records, config_items, update_proc) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/motor/configs/sync_from_hash.rb', line 120 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 |