Class: SyncAudit
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- SyncAudit
- Defined in:
- app/models/sync_audit.rb
Class Method Summary collapse
- .apply_callback(type, operation: ['update', 'delete', 'insert']) ⇒ Object
- .apply_synchro(type, operation: ['update', 'delete', 'insert']) ⇒ Object
- .synchro_types ⇒ Object
Instance Method Summary collapse
Class Method Details
.apply_callback(type, operation: ['update', 'delete', 'insert']) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'app/models/sync_audit.rb', line 70 def self.apply_callback(type, operation: ['update', 'delete', 'insert']) SyncAudit.where(state: :applied, synchro_type: type, operation: operation).find_each do |sync| SyncAudit.transaction do sync.synchro.respond_to?(:after_sync) && sync.synchro.after_sync sync.update! state: 'finished' end end end |
.apply_synchro(type, operation: ['update', 'delete', 'insert']) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'app/models/sync_audit.rb', line 79 def self.apply_synchro(type, operation: ['update', 'delete', 'insert']) SyncAudit.where(state: 'init', synchro_type: type, operation: operation).find_each do |sync_audit| begin sync_audit.apply_changes rescue SystemStackError, ActiveRecord::ActiveRecordError => e logger.warn e. end end end |
.synchro_types ⇒ Object
66 67 68 |
# File 'app/models/sync_audit.rb', line 66 def self.synchro_types SyncAudit.select(:synchro_type).distinct.pluck(:synchro_type).compact end |
Instance Method Details
#apply_changes ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/sync_audit.rb', line 24 def apply_changes if self.operation_update? _synchro = self.synchro || synchro_model.find_by(synchro_params) _synchro.assign_attributes to_apply_params self.state = 'applied' self.synchro_id = _synchro.id self.class.transaction do _synchro.save! self.save! end elsif self.operation_delete? && self.synchro self.class.transaction do self.synchro.destroy! self.update! state: 'applied' end elsif self.operation_insert? _synchro = synchro_model.find_or_initialize_by(synchro_params) _synchro.assign_attributes to_apply_params self.class.transaction do _synchro.save_sneakily! self.update! synchro_id: _synchro.id, state: 'applied' end end end |
#synchro_model ⇒ Object
49 50 51 |
# File 'app/models/sync_audit.rb', line 49 def synchro_model @synchro_model ||= self.synchro_type.constantize end |
#to_apply_params ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/sync_audit.rb', line 53 def to_apply_params x = {} audited_changes.each do |key, v| if synchro_model.columns_hash[key].type == :string x[key] = v[1].to_s else x[key] = v[1] end end x end |