Sha256: 71eec38e2c3891265d80b2314d2d13f55055ff3504de0a23150848c6edced80a

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Saviour
  module DbHelpers
    NotInTransaction = Class.new(StandardError)

    class CommitDummy
      def initialize(block)
        @block = block
      end

      def rolledback!(*)
        close_transaction
      end

      def close_transaction(*)
      end

      def before_committed!(*)
      end

      def committed!(*)
        @block.call
      end
    end

    class RollbackDummy
      def initialize(block)
        @block = block
      end

      def rolledback!(*)
        @block.call
        close_transaction
      end

      def close_transaction(*)
      end

      def before_committed!(*)
      end

      def committed!(*)
      end
    end


    class << self

      def run_after_commit(&block)
        unless ActiveRecord::Base.connection.current_transaction.open?
          raise NotInTransaction, 'Trying to use `run_after_commit` but no transaction is currently open.'
        end

        dummy = CommitDummy.new(block)
        ActiveRecord::Base.connection.add_transaction_record(dummy)
      end

      def run_after_rollback(&block)
        unless ActiveRecord::Base.connection.current_transaction.open?
          raise NotInTransaction, 'Trying to use `run_after_commit` but no transaction is currently open.'
        end

        dummy = RollbackDummy.new(block)
        ActiveRecord::Base.connection.add_transaction_record(dummy)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
saviour-0.4.14 lib/saviour/db_helpers.rb
saviour-0.4.13 lib/saviour/db_helpers.rb
saviour-0.4.12 lib/saviour/db_helpers.rb
saviour-0.4.11 lib/saviour/db_helpers.rb
saviour-0.4.10 lib/saviour/db_helpers.rb
saviour-0.4.9 lib/saviour/db_helpers.rb
saviour-0.4.8 lib/saviour/db_helpers.rb
saviour-0.4.7 lib/saviour/db_helpers.rb
saviour-0.4.6 lib/saviour/db_helpers.rb