Sha256: 4b2eddf627b731d03bf4c2cc234cbe0d4b157a507508613ac499efe4e5c0b074

Contents?: true

Size: 921 Bytes

Versions: 10

Compression:

Stored size: 921 Bytes

Contents

# frozen_string_literal: true

module AfterCommitEverywhere
  # ActiveRecord model-like class to fake ActiveRecord to make it believe
  # that it calls transactional callbacks on real model objects.
  class Wrap
    def initialize(connection: ActiveRecord::Base.connection, **handlers)
      @connection = connection
      @handlers = handlers
    end

    # rubocop: disable Naming/PredicateName
    def has_transactional_callbacks?
      true
    end
    # rubocop: enable Naming/PredicateName

    def before_committed!(*)
      @handlers[:before_commit]&.call
    end

    def trigger_transactional_callbacks?
      true
    end

    def committed!(*)
      @handlers[:after_commit]&.call
    end

    def rolledback!(*)
      @handlers[:after_rollback]&.call
    end

    # Required for +transaction(requires_new: true)+
    def add_to_transaction(*)
      @connection.add_transaction_record(self)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
after_commit_everywhere-1.4.0 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.3.1 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.3.0 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.2.2 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.2.1 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.2.0 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.1.0 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-1.0.0 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-0.1.5 lib/after_commit_everywhere/wrap.rb
after_commit_everywhere-0.1.4 lib/after_commit_everywhere/wrap.rb