Sha256: 2a63392ce12e7828eed9ad33c072bbc8d1037602bea17611812486739f5cb9b0

Contents?: true

Size: 932 Bytes

Versions: 25

Compression:

Stored size: 932 Bytes

Contents

module ForeignOffice
  module Broadcaster

    def self.included(base)
      base.extend ClassMethods
      def handle_broadcast
        self.broadcast_change if self.changed?
      end

      def broadcast_change
        Rails.logger.debug "Broadcasting change for #{self.inspect}..."
        Rails.logger.debug "Class name: #{self.class.name}"
        Rails.logger.debug "ID: #{self.id}"
        Rails.logger.debug "Serialize: #{self.serialize}"
        ForeignOffice.publish(channel: "#{self.class.name}#{self.id}", object: self.serialize)
      rescue => e
        Rails.logger.error "Failed to broadcast change: #{e.inspect}"
        Rails.logger.debug e.backtrace.join("\n")
        raise e
      end

      def serialize
        self.attributes
      end
    end

    module ClassMethods
      def broadcast_changes!
        self.send(:after_save, :handle_broadcast, {unless: :skip_all_callbacks})
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
foreign_office-0.16.6 lib/foreign_office/broadcaster.rb
foreign_office-0.16.5 lib/foreign_office/broadcaster.rb
foreign_office-0.16.4 lib/foreign_office/broadcaster.rb
foreign_office-0.16.3 lib/foreign_office/broadcaster.rb
foreign_office-0.16.2 lib/foreign_office/broadcaster.rb
foreign_office-0.16.1 lib/foreign_office/broadcaster.rb
foreign_office-0.16.0 lib/foreign_office/broadcaster.rb
foreign_office-0.15.12 lib/foreign_office/broadcaster.rb
foreign_office-0.15.11 lib/foreign_office/broadcaster.rb
foreign_office-0.15.10 lib/foreign_office/broadcaster.rb
foreign_office-0.15.9 lib/foreign_office/broadcaster.rb
foreign_office-0.15.8 lib/foreign_office/broadcaster.rb
foreign_office-0.15.7 lib/foreign_office/broadcaster.rb
foreign_office-0.15.6 lib/foreign_office/broadcaster.rb
foreign_office-0.15.5 lib/foreign_office/broadcaster.rb
foreign_office-0.15.4 lib/foreign_office/broadcaster.rb
foreign_office-0.15.3 lib/foreign_office/broadcaster.rb
foreign_office-0.15.2 lib/foreign_office/broadcaster.rb
foreign_office-0.15.1 lib/foreign_office/broadcaster.rb
foreign_office-0.15.0 lib/foreign_office/broadcaster.rb