Sha256: 38bf53f2b02e1c396e2685f1b0776d3cc05a021a0145b3eeecdb11125eb3c21d

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

require 'active_support/core_ext/string/inflections'

require 'cony'
require 'cony/amqp_connection_handler'

module Cony
  module ActiveRecord

    extend ActiveSupport::Concern

    included do
      after_create :cony_send_create_notify
      after_update :cony_send_update_notify
      after_destroy :cony_send_destroy_notify
    end

    def cony_send_create_notify
      publish(:create)
    end

    def cony_send_update_notify
      publish(:update)
    end

    def cony_send_destroy_notify
      publish(:destroy)
    end


    private
    def publish(type)
      return if Cony.config.test_mode
      amqp_connection.publish(
        {id: self.id, changes: cony_changes},
        "#{self.class.name.underscore}.mutation.#{type}")
    end

    def amqp_connection
      @amqp_connection ||= Cony::AMQPConnectionHandler.new(Cony.config.amqp)
    end

    def cony_changes
      changes.map do |name, change|
        {name => {old: change.first, new: change.last}}
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cony-1.1.1 lib/cony/active_record.rb
cony-1.1.0 lib/cony/active_record.rb