Skip to content

Commit 68b50f6

Browse files
ellnixcomandeo-mongo
authored andcommitted
Fix after_action_commit callbacks (mongodb#5968)
None of them worked due to a syntax error where `set_options_for_callbacks` did not accept a second argument.
1 parent 437501d commit 68b50f6

File tree

3 files changed

+252
-2
lines changed

3 files changed

+252
-2
lines changed

lib/mongoid/clients/sessions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def run_abort_callbacks(session)
213213

214214
# Transforms custom options for after_commit and after_rollback callbacks
215215
# into options for +set_callback+.
216-
def set_options_for_callbacks!(args)
217-
options = args.extract_options!
216+
def set_options_for_callbacks!(args, enforced_options = {})
217+
options = args.extract_options!.merge(enforced_options)
218218
args << options
219219

220220
if options[:on]

spec/mongoid/clients/transactions_spec.rb

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,12 @@ def capture_exception
787787
Mongoid::Clients.with_name(:default).database.collections.each(&:drop)
788788
TransactionsSpecPerson.collection.create
789789
TransactionsSpecPersonWithOnCreate.collection.create
790+
TransactionsSpecPersonWithAfterCreateCommit.collection.create
790791
TransactionsSpecPersonWithOnUpdate.collection.create
792+
TransactionsSpecPersonWithAfterUpdateCommit.collection.create
793+
TransactionsSpecPersonWithAfterSaveCommit.collection.create
791794
TransactionsSpecPersonWithOnDestroy.collection.create
795+
TransactionsSpecPersonWithAfterDestroyCommit.collection.create
792796
TransactionSpecRaisesBeforeSave.collection.create
793797
TransactionSpecRaisesAfterSave.collection.create
794798
end
@@ -818,6 +822,18 @@ def capture_exception
818822

819823
it_behaves_like 'commit callbacks are called'
820824
end
825+
826+
context 'when callback is after_create_commit' do
827+
let!(:subject) do
828+
person = nil
829+
TransactionsSpecPersonWithAfterCreateCommit.transaction do
830+
person = TransactionsSpecPersonWithAfterCreateCommit.create!(name: 'James Bond')
831+
end
832+
person
833+
end
834+
835+
it_behaves_like 'commit callbacks are called'
836+
end
821837
end
822838

823839
context 'save' do
@@ -886,6 +902,94 @@ def capture_exception
886902
it_behaves_like 'commit callbacks are called'
887903
end
888904
end
905+
906+
context 'with after_update_commit callback' do
907+
let(:subject) do
908+
TransactionsSpecPersonWithAfterUpdateCommit.create!(name: 'James Bond').tap do |subject|
909+
subject.after_commit_counter.reset
910+
subject.after_rollback_counter.reset
911+
end
912+
end
913+
914+
context 'when modified once' do
915+
before do
916+
subject.transaction do
917+
subject.name = 'Austin Powers'
918+
subject.save!
919+
end
920+
end
921+
922+
it_behaves_like 'commit callbacks are called'
923+
end
924+
925+
context 'when modified multiple times' do
926+
before do
927+
subject.transaction do
928+
subject.name = 'Austin Powers'
929+
subject.save!
930+
subject.name = 'Jason Bourne'
931+
subject.save!
932+
end
933+
end
934+
935+
it_behaves_like 'commit callbacks are called'
936+
end
937+
end
938+
939+
context 'with after_save_commit callback' do
940+
let(:subject) do
941+
TransactionsSpecPersonWithAfterSaveCommit.create!(name: 'James Bond').tap do |subject|
942+
subject.after_commit_counter.reset
943+
subject.after_rollback_counter.reset
944+
end
945+
end
946+
947+
context 'when modified once' do
948+
before do
949+
subject.transaction do
950+
subject.name = 'Austin Powers'
951+
subject.save!
952+
end
953+
end
954+
955+
it_behaves_like 'commit callbacks are called'
956+
end
957+
958+
context 'when created' do
959+
before do
960+
TransactionsSpecPersonWithAfterSaveCommit.transaction do
961+
subject
962+
end
963+
end
964+
965+
it_behaves_like 'commit callbacks are called'
966+
end
967+
968+
context 'when modified multiple times' do
969+
before do
970+
subject.transaction do
971+
subject.name = 'Austin Powers'
972+
subject.save!
973+
subject.name = 'Jason Bourne'
974+
subject.save!
975+
end
976+
end
977+
978+
it_behaves_like 'commit callbacks are called'
979+
end
980+
981+
context 'when created and modified' do
982+
before do
983+
TransactionsSpecPersonWithAfterSaveCommit.transaction do
984+
subject
985+
subject.name = 'Jason Bourne'
986+
subject.save!
987+
end
988+
end
989+
990+
it_behaves_like 'commit callbacks are called'
991+
end
992+
end
889993
end
890994

891995
context 'update_attributes' do
@@ -919,6 +1023,34 @@ def capture_exception
9191023

9201024
it_behaves_like 'commit callbacks are called'
9211025
end
1026+
1027+
context 'when callback is after_update_commit' do
1028+
let(:subject) do
1029+
TransactionsSpecPersonWithAfterUpdateCommit.create!(name: 'Jason Bourne')
1030+
end
1031+
1032+
before do
1033+
TransactionsSpecPersonWithAfterUpdateCommit.transaction do
1034+
subject.update_attributes!(name: 'Foma Kiniaev')
1035+
end
1036+
end
1037+
1038+
it_behaves_like 'commit callbacks are called'
1039+
end
1040+
1041+
context 'when callback is after_save_commit' do
1042+
let(:subject) do
1043+
TransactionsSpecPersonWithAfterSaveCommit.create!(name: 'Jason Bourne')
1044+
end
1045+
1046+
before do
1047+
TransactionsSpecPersonWithAfterSaveCommit.transaction do
1048+
subject.update_attributes!(name: 'Foma Kiniaev')
1049+
end
1050+
end
1051+
1052+
it_behaves_like 'commit callbacks are called'
1053+
end
9221054
end
9231055

9241056
context 'destroy' do
@@ -971,6 +1103,31 @@ def capture_exception
9711103

9721104
it_behaves_like 'commit callbacks are called'
9731105
end
1106+
1107+
context 'with after_destroy_commit' do
1108+
let(:after_commit_counter) do
1109+
TransactionsSpecCounter.new
1110+
end
1111+
1112+
let(:after_rollback_counter) do
1113+
TransactionsSpecCounter.new
1114+
end
1115+
1116+
let(:subject) do
1117+
TransactionsSpecPersonWithAfterDestroyCommit.create!(name: 'James Bond').tap do |p|
1118+
p.after_commit_counter = after_commit_counter
1119+
p.after_rollback_counter = after_rollback_counter
1120+
end
1121+
end
1122+
1123+
before do
1124+
subject.transaction do
1125+
subject.destroy
1126+
end
1127+
end
1128+
1129+
it_behaves_like 'commit callbacks are called'
1130+
end
9741131
end
9751132
end
9761133

spec/mongoid/clients/transactions_spec_models.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ def after_rollback_counter
3333
def after_rollback_counter=(new_counter)
3434
@after_rollback_counter = new_counter
3535
end
36+
37+
def after_save_commit_counter
38+
@after_save_commit_counter ||= TransactionsSpecCounter.new
39+
end
40+
41+
def after_save_commit_counter=(new_counter)
42+
@after_save_commit_counter = new_counter
43+
end
44+
45+
def after_create_commit_counter
46+
@after_create_commit_counter ||= TransactionsSpecCounter.new
47+
end
48+
49+
def after_create_commit_counter=(new_counter)
50+
@after_create_commit_counter = new_counter
51+
end
52+
53+
def after_update_commit_counter
54+
@after_update_commit_counter ||= TransactionsSpecCounter.new
55+
end
56+
57+
def after_update_commit_counter=(new_counter)
58+
@after_update_commit_counter = new_counter
59+
end
60+
61+
def after_destroy_commit_counter
62+
@after_destroy_commit_counter ||= TransactionsSpecCounter.new
63+
end
64+
65+
def after_destroy_commit_counter=(new_counter)
66+
@after_destroy_commit_counter = new_counter
67+
end
3668
end
3769

3870
class TransactionsSpecPerson
@@ -65,6 +97,21 @@ class TransactionsSpecPersonWithOnCreate
6597
end
6698
end
6799

100+
class TransactionsSpecPersonWithAfterCreateCommit
101+
include Mongoid::Document
102+
include TransactionsSpecCountable
103+
104+
field :name, type: String
105+
106+
after_create_commit do
107+
after_commit_counter.inc
108+
end
109+
110+
after_rollback on: :create do
111+
after_rollback_counter.inc
112+
end
113+
end
114+
68115
class TransactionsSpecPersonWithOnUpdate
69116
include Mongoid::Document
70117
include TransactionsSpecCountable
@@ -80,6 +127,36 @@ class TransactionsSpecPersonWithOnUpdate
80127
end
81128
end
82129

130+
class TransactionsSpecPersonWithAfterUpdateCommit
131+
include Mongoid::Document
132+
include TransactionsSpecCountable
133+
134+
field :name, type: String
135+
136+
after_update_commit do
137+
after_commit_counter.inc
138+
end
139+
140+
after_rollback on: :create do
141+
after_rollback_counter.inc
142+
end
143+
end
144+
145+
class TransactionsSpecPersonWithAfterSaveCommit
146+
include Mongoid::Document
147+
include TransactionsSpecCountable
148+
149+
field :name, type: String
150+
151+
after_save_commit do
152+
after_commit_counter.inc
153+
end
154+
155+
after_rollback on: :create do
156+
after_rollback_counter.inc
157+
end
158+
end
159+
83160
class TransactionsSpecPersonWithOnDestroy
84161
include Mongoid::Document
85162
include TransactionsSpecCountable
@@ -94,6 +171,22 @@ class TransactionsSpecPersonWithOnDestroy
94171
after_rollback_counter.inc
95172
end
96173
end
174+
175+
class TransactionsSpecPersonWithAfterDestroyCommit
176+
include Mongoid::Document
177+
include TransactionsSpecCountable
178+
179+
field :name, type: String
180+
181+
after_destroy_commit do
182+
after_commit_counter.inc
183+
end
184+
185+
after_rollback on: :create do
186+
after_rollback_counter.inc
187+
end
188+
end
189+
97190
class TransactionSpecRaisesBeforeSave
98191
include Mongoid::Document
99192
include TransactionsSpecCountable

0 commit comments

Comments
 (0)