Module: AfterCommit

Defined in:
lib/after_commit/after_savepoint.rb,
lib/after_commit.rb,
lib/after_commit/active_record.rb,
lib/after_commit/connection_adapters.rb

Overview

The single transaction means that after_commit callback never happens in tests. Instead use savepoints.

Defined Under Namespace

Modules: ActiveRecord, AfterSavepoint, ConnectionAdapters, TestConnectionAdapters

Class Method Summary collapse

Class Method Details

.add_to_collection(collection, connection, record) ⇒ Object



55
56
57
# File 'lib/after_commit.rb', line 55

def self.add_to_collection(collection, connection, record)
  Thread.current[collection][connection.current_transaction_key] << record
end

.cleanup(connection) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/after_commit.rb', line 38

def self.cleanup(connection)
  [
    :committed_records,
    :committed_records_on_create,
    :committed_records_on_update,
    :committed_records_on_destroy
  ].each do |collection|
    Thread.current[collection] ||= {}
    Thread.current[collection][connection.previous_transaction_key] = []
  end
end

.collection(collection, connection) ⇒ Object



59
60
61
62
# File 'lib/after_commit.rb', line 59

def self.collection(collection, connection)
  Thread.current[collection] ||= {}
  Thread.current[collection][connection.previous_transaction_key] ||= []
end

.created_records(connection) ⇒ Object



26
27
28
# File 'lib/after_commit.rb', line 26

def self.created_records(connection)
  collection :committed_records_on_create, connection
end

.destroyed_records(connection) ⇒ Object



34
35
36
# File 'lib/after_commit.rb', line 34

def self.destroyed_records(connection)
  collection :committed_records_on_destroy, connection
end

.prepare_collection(collection, connection) ⇒ Object



50
51
52
53
# File 'lib/after_commit.rb', line 50

def self.prepare_collection(collection, connection)
  Thread.current[collection] ||= {}
  Thread.current[collection][connection.current_transaction_key] ||= []
end

.record(connection, record) ⇒ Object



2
3
4
5
# File 'lib/after_commit.rb', line 2

def self.record(connection, record)
  prepare_collection :committed_records, connection
  add_to_collection  :committed_records, connection, record
end

.record_created(connection, record) ⇒ Object



7
8
9
10
# File 'lib/after_commit.rb', line 7

def self.record_created(connection, record)
  prepare_collection :committed_records_on_create, connection
  add_to_collection  :committed_records_on_create, connection, record
end

.record_destroyed(connection, record) ⇒ Object



17
18
19
20
# File 'lib/after_commit.rb', line 17

def self.record_destroyed(connection, record)
  prepare_collection :committed_records_on_destroy, connection
  add_to_collection  :committed_records_on_destroy, connection, record
end

.record_updated(connection, record) ⇒ Object



12
13
14
15
# File 'lib/after_commit.rb', line 12

def self.record_updated(connection, record)
  prepare_collection :committed_records_on_update, connection
  add_to_collection  :committed_records_on_update, connection, record
end

.records(connection) ⇒ Object



22
23
24
# File 'lib/after_commit.rb', line 22

def self.records(connection)
  collection :committed_records, connection
end

.updated_records(connection) ⇒ Object



30
31
32
# File 'lib/after_commit.rb', line 30

def self.updated_records(connection)
  collection :committed_records_on_update, connection
end