Sha256: e09f03b47950f508b63e77754e07237f2a79bcec1b3722d73d881d12d5ed011f

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'wisper/activerecord/publisher/version'
require 'active_record'
require 'wisper'

module Wisper
  module ActiveRecord
    # ActiveRecord extension to automatically publish events for CRUD lifecycle
    # see https://github.com/krisleech/wisper/wiki/Rails-CRUD-with-ActiveRecord
    # see https://github.com/krisleech/wisper-activerecord
    module Publisher
      extend ActiveSupport::Concern

      included do
        include Wisper::Publisher

        after_commit :broadcast_create, on: :create
        after_commit :broadcast_update, on: :update
        after_commit :broadcast_destroy, on: :destroy
      end

      private

      # broadcast MODEL_created event to subscribed listeners
      def broadcast_create
        broadcast broadcast_event_name(:created), self
      end

      # broadcast MODEL_updated event to subscribed listeners
      # pass the set of changes for background jobs to know what changed
      # see https://github.com/krisleech/wisper-activerecord/issues/17
      def broadcast_update
        broadcast broadcast_event_name(:updated), self, previous_changes
      end

      # broadcast MODEL_destroyed to subscribed listeners
      # pass a serialized version of the object attributes
      # for listeners since the object is no longer accessible in the database
      def broadcast_destroy
        broadcast broadcast_event_name(:destroyed), attributes
      end

      def broadcast_event_name(lifecycle)
        "#{self.class.model_name.param_key}_#{lifecycle}"
      end
    end
  end
end
ActiveSupport.on_load(:active_record) do
  include Wisper::ActiveRecord::Publisher
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wisper-activerecord-publisher-0.1.1 lib/wisper/activerecord/publisher.rb
wisper-activerecord-publisher-0.1.0 lib/wisper/activerecord/publisher.rb