Sha256: 5722d678fb76da90ebe222b8fc63b512591f1a89709b3859aace11d7a2051736

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'active_record'

module ActiveRecord::AfterTransaction
  module Methods
    def self.included(klass)
      klass.class_eval do
        after_commit :execute_after_transaction
        after_rollback :clear_after_transaction
      end
    end

    def after_transaction(&block)
      return block.call unless self.class.connection.open_transactions
      @after_transaction_queue ||= []
      @after_transaction_queue.push block
      logger && logger.debug("Push #{block}")
    end

    private

    def execute_after_transaction
      queue = @after_transaction_queue ||= []
      if !queue.empty?
        logger && logger.debug("Execute the queue")
        queue = queue.dup
        clear_after_transaction
        queue.each do |block|
          logger && logger.debug("Execute #{block}")
          block.call
        end
      end
    end

    def clear_after_transaction
      logger && logger.debug("Clear the queue")
      @after_transaction_queue ||= []
      @after_transaction_queue.clear
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-after-transaction-0.1.2 lib/activerecord-after-transaction/methods.rb