Class: Clickwrap::DurableCommitCallback

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/durable_commit_callback.rb

Overview

Rails normally defers a record's after_commit callback to the outermost joinable transaction. A non-joinable outer transaction can instead make a nested savepoint run that callback while the outer transaction is still capable of rolling everything back. This tiny transaction record moves Clickwrap's externally visible work to that real outer boundary.

The transaction-record protocol is stable across every supported Rails version (7.1 through 8.x), while the public ActiveRecord.after_all_transactions_commit helper only exists from 7.2 and deliberately ignores non-joinable transactions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ DurableCommitCallback

Returns a new instance of DurableCommitCallback.



21
22
23
# File 'lib/clickwrap/durable_commit_callback.rb', line 21

def initialize(event)
  @event = event
end

Class Method Details

.defer(event) ⇒ Object



15
16
17
18
19
# File 'lib/clickwrap/durable_commit_callback.rb', line 15

def self.defer(event)
  callback = new(event)
  ::ActiveRecord::Base.connection.add_transaction_record(callback)
  callback
end

Instance Method Details

#before_committed!Object



25
# File 'lib/clickwrap/durable_commit_callback.rb', line 25

def before_committed!; end

#committed!(should_run_callbacks: true) ⇒ Object



27
28
29
# File 'lib/clickwrap/durable_commit_callback.rb', line 27

def committed!(should_run_callbacks: true)
  @event.finalize_durable_commit! if should_run_callbacks
end

#rolledback!(force_restore_state: false, should_run_callbacks: true) ⇒ Object



31
32
33
# File 'lib/clickwrap/durable_commit_callback.rb', line 31

def rolledback!(force_restore_state: false, should_run_callbacks: true)
  @event.invalidate_pending_receipts_after_rollback! if should_run_callbacks
end

#trigger_transactional_callbacks?Boolean

Returns:

  • (Boolean)


35
# File 'lib/clickwrap/durable_commit_callback.rb', line 35

def trigger_transactional_callbacks? = true