Sha256: 6129c8499377db830e3ad33cdfc55cc5ba8516b221f17a3881bf96039f67b770
Contents?: true
Size: 1.21 KB
Versions: 1
Compression:
Stored size: 1.21 KB
Contents
module ModelTransporter::NotifiesModelUpdates extend ActiveSupport::Concern included do class_attribute :notifies_model_updates_options self.notifies_model_updates_options = {} end class_methods do def notifies_model_updates(channel:, id_key: :id, on: %i(create update destroy)) self.notifies_model_updates_options = { channel: channel, id_key: id_key } if on.include?(:create) after_create_commit -> { notify_model_updates(:creates, self) } end if on.include?(:update) after_update_commit -> { notify_model_updates(:updates, self) } end if on.include?(:destroy) after_destroy_commit -> { notify_model_updates(:deletes, {}) } end end end def notify_model_updates(update_type, model_state) channel = instance_exec(&(self.class.notifies_model_updates_options[:channel])) id_key = self.public_send(self.class.notifies_model_updates_options[:id_key]) payload = { creates: {}, updates: {}, deletes: {} } payload[update_type] = { self.class.name.pluralize.underscore => { id_key => model_state } } ModelTransporter::BatchModelUpdates.enqueue_model_updates( channel, payload ) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
model_transporter-0.1.0 | lib/model_transporter/notifies_model_updates.rb |