Sha256: 572f77640068c6a2bc23f44ae0808a5879fc87a8e7dc9a7caed4201dbca7220d
Contents?: true
Size: 1.54 KB
Versions: 3
Compression:
Stored size: 1.54 KB
Contents
require "active_support/json" require "active_support/core_ext/object/json" require "wisper" module SyncMachine # Listen to changes in any model that could result in a change to the # published document, and enqueue a FindSubjectsWorker job when the change # occurs. class ChangeListener def self.inherited(base) base.cattr_accessor :model_syms base.subscribe end def self.listen_to_models(*model_syms) self.model_syms = model_syms model_syms.each do |model_sym| send( :alias_method, "update_#{model_sym}_successful".to_sym, :after_record_saved ) end end def self.subscribe Wisper.subscribe(new) end def after_create(record) model_sym = record.class.name.underscore.to_sym return unless self.class.model_syms.include?(model_sym) after_record_saved(record) end def after_record_saved(record) return unless orm_adapter.sufficient_changes_to_find_subjects?(record) sync_module = SyncMachine.sync_module(self.class) finder_class = sync_module.const_get('FindSubjectsWorker') finder_class.perform_async( record.class.name, record_id_for_job(record.id), changed_keys(record), Time.now.to_json ) end private def changed_keys(record) orm_adapter.change_listener_changed_keys(record) end def record_id_for_job(record_id) orm_adapter.record_id_for_job(record_id) end def orm_adapter SyncMachine.orm_adapter end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
sync_machine-0.8.1 | lib/sync_machine/change_listener.rb |
sync_machine-0.8.0 | lib/sync_machine/change_listener.rb |
sync_machine-0.7.0 | lib/sync_machine/change_listener.rb |