Sha256: a1e9d321f4c060843b7a24bf026d66691f4224dde53e08913c753d5c51d0962c

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module Alondra
  module ChangesCallbacks
    extend self

    def push_updates(klass, options)
      klass.class_eval do
        after_update do |record|
          ChangesCallbacks.push_event :updated, record, options
        end
      end
    end

    def push_creations(klass, options)
      klass.class_eval do
        after_create do |record|
          ChangesCallbacks.push_event :created, record, options
        end
      end
    end

    def push_destroys(klass, options)
      klass.class_eval do
        after_destroy do |record|
          ChangesCallbacks.push_event :destroyed, record, options
        end
      end
    end

    def push_event(type, record, options)
      channels = channel_names_from(type, record, options)

      channels.each do |channel|
        event = Event.new(:event         => type,              :resource => record,
                          :resource_type => record.class.name, :channel  => channel)

        MessageQueueClient.push event
      end
    end

    def channel_names_from(type, record, options)
      case options[:to]
      when String then
        [options[:to]]
      when Symbol then
        records = record.send options[:to]
        Channel.names_for(records)
      else
        [Channel.default_name_for(record)]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
alondra-0.1.1 lib/alondra/changes_callbacks.rb
alondra-0.1.0 lib/alondra/changes_callbacks.rb
alondra-0.0.4 lib/alondra/changes_callbacks.rb
alondra-0.0.3 lib/alondra/changes_callbacks.rb