Sha256: d0fcb9389e621316243d957dfd3f41a58e035e154d853971b517472b7bb88577

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

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_save_create_notify_data
      after_update :cony_save_update_notify_data
      after_touch  :cony_save_update_notify_data if respond_to?(:after_touch)
      after_destroy :cony_save_destroy_notify_data
      after_commit :cony_publish
    end

    def cony_save_create_notify_data
      cony_prepare_notify :created, cony_changes_created
    end

    def cony_save_update_notify_data
      cony_prepare_notify :updated, cony_changes_updated
    end

    def cony_save_destroy_notify_data
      cony_prepare_notify :destroyed, cony_changes_destroyed
    end

    def cony_publish
      return if Cony.config.test_mode
      cony_amqp_connection.publish(cony_notify_hash, cony_notify_routing_key)
    end

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

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

    def cony_changes_created
      cony_mapped_changes
    end

    def cony_changes_updated
      cony_mapped_changes
    end

    def cony_changes_destroyed
      attributes.map do |name, value|
        {name => {old: value, new: nil}}
      end
    end

    def cony_notify_hash
      {id: self.id, changes: @cony_notify[:changes], model: self.class.name, event: @cony_notify[:event]}
    end

    def cony_notify_routing_key
      "#{self.class.name.underscore}.mutation.#{@cony_notify[:event]}"
    end

    def cony_prepare_notify(event, changes)
      @cony_notify = { event: event, changes: changes }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cony-2.0.1 lib/cony/active_record.rb
cony-2.0.0 lib/cony/active_record.rb
cony-1.5.0 lib/cony/active_record.rb
cony-1.4.1 lib/cony/active_record.rb