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,
lib/after_commit/active_support_callbacks.rb

Overview

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

Defined Under Namespace

Modules: ActiveRecord, ActiveSupportCallbacks, AfterSavepoint, ConnectionAdapters, TestConnectionAdapters

Class Method Summary collapse

Class Method Details

.add_to_collection(collection, connection, record) ⇒ Object



65
66
67
# File 'lib/after_commit.rb', line 65

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

.cleanup(connection) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/after_commit.rb', line 47

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

.collection(collection, connection) ⇒ Object



69
70
71
72
# File 'lib/after_commit.rb', line 69

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

.created_records(connection) ⇒ Object



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

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

.destroyed_records(connection) ⇒ Object



43
44
45
# File 'lib/after_commit.rb', line 43

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

.prepare_collection(collection, connection) ⇒ Object



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

def self.prepare_collection(collection, connection)
  Thread.current[collection] ||= {}
  Thread.current[collection][connection.unique_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



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

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

.record_saved(connection, record) ⇒ Object



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

def self.record_saved(connection, record)
  prepare_collection :committed_records_on_save, connection
  add_to_collection  :committed_records_on_save, 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



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

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

.saved_records(connection) ⇒ Object



39
40
41
# File 'lib/after_commit.rb', line 39

def self.saved_records(connection)
  collection :committed_records_on_save, connection
end

.updated_records(connection) ⇒ Object



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

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