@@ -786,111 +786,221 @@ def capture_exception
786
786
before do
787
787
Mongoid ::Clients . with_name ( :default ) . database . collections . each ( &:drop )
788
788
TransactionsSpecPerson . collection . create
789
+ TransactionsSpecPersonWithOnCreate . collection . create
790
+ TransactionsSpecPersonWithOnUpdate . collection . create
791
+ TransactionsSpecPersonWithOnDestroy . collection . create
789
792
TransactionSpecRaisesBeforeSave . collection . create
790
793
TransactionSpecRaisesAfterSave . collection . create
791
794
end
792
795
793
796
context 'when commit the transaction' do
794
797
context 'create' do
795
- let! ( :subject ) do
796
- person = nil
797
- TransactionsSpecPerson . transaction do
798
- person = TransactionsSpecPerson . create! ( name : 'James Bond' )
798
+ context 'without :on option' do
799
+ let! ( :subject ) do
800
+ person = nil
801
+ TransactionsSpecPerson . transaction do
802
+ person = TransactionsSpecPerson . create! ( name : 'James Bond' )
803
+ end
804
+ person
799
805
end
800
- person
806
+
807
+ it_behaves_like 'commit callbacks are called'
801
808
end
802
809
803
- it_behaves_like 'commit callbacks are called'
810
+ context 'when callback has :on option' do
811
+ let! ( :subject ) do
812
+ person = nil
813
+ TransactionsSpecPersonWithOnCreate . transaction do
814
+ person = TransactionsSpecPersonWithOnCreate . create! ( name : 'James Bond' )
815
+ end
816
+ person
817
+ end
818
+
819
+ it_behaves_like 'commit callbacks are called'
820
+ end
804
821
end
805
822
806
823
context 'save' do
807
- let ( :subject ) do
808
- TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |subject |
809
- subject . after_commit_counter . reset
810
- subject . after_rollback_counter . reset
824
+ context 'without :on option' do
825
+ let ( :subject ) do
826
+ TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |subject |
827
+ subject . after_commit_counter . reset
828
+ subject . after_rollback_counter . reset
829
+ end
830
+ end
831
+
832
+ context 'when modified once' do
833
+ before do
834
+ subject . transaction do
835
+ subject . name = 'Austin Powers'
836
+ subject . save!
837
+ end
838
+ end
839
+
840
+ it_behaves_like 'commit callbacks are called'
841
+ end
842
+
843
+ context 'when modified multiple times' do
844
+ before do
845
+ subject . transaction do
846
+ subject . name = 'Austin Powers'
847
+ subject . save!
848
+ subject . name = 'Jason Bourne'
849
+ subject . save!
850
+ end
851
+ end
852
+
853
+ it_behaves_like 'commit callbacks are called'
811
854
end
812
855
end
813
856
814
- context 'when modified once' do
857
+ context 'with :on option' do
858
+ let ( :subject ) do
859
+ TransactionsSpecPersonWithOnUpdate . create! ( name : 'James Bond' ) . tap do |subject |
860
+ subject . after_commit_counter . reset
861
+ subject . after_rollback_counter . reset
862
+ end
863
+ end
864
+
865
+ context 'when modified once' do
866
+ before do
867
+ subject . transaction do
868
+ subject . name = 'Austin Powers'
869
+ subject . save!
870
+ end
871
+ end
872
+
873
+ it_behaves_like 'commit callbacks are called'
874
+ end
875
+
876
+ context 'when modified multiple times' do
877
+ before do
878
+ subject . transaction do
879
+ subject . name = 'Austin Powers'
880
+ subject . save!
881
+ subject . name = 'Jason Bourne'
882
+ subject . save!
883
+ end
884
+ end
885
+
886
+ it_behaves_like 'commit callbacks are called'
887
+ end
888
+ end
889
+ end
890
+
891
+ context 'update_attributes' do
892
+ context 'without :on option' do
893
+ let ( :subject ) do
894
+ TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |subject |
895
+ subject . after_commit_counter . reset
896
+ subject . after_rollback_counter . reset
897
+ end
898
+ end
899
+
815
900
before do
816
901
subject . transaction do
817
- subject . name = 'Austin Powers'
818
- subject . save!
902
+ subject . update_attributes! ( name : 'Austin Powers' )
819
903
end
820
904
end
821
905
822
906
it_behaves_like 'commit callbacks are called'
823
907
end
824
908
825
- context 'when modified multiple times' do
909
+ context 'when callback has on option' do
910
+ let ( :subject ) do
911
+ TransactionsSpecPersonWithOnUpdate . create! ( name : 'Jason Bourne' )
912
+ end
913
+
826
914
before do
827
- subject . transaction do
828
- subject . name = 'Austin Powers'
829
- subject . save!
830
- subject . name = 'Jason Bourne'
831
- subject . save!
915
+ TransactionsSpecPersonWithOnUpdate . transaction do
916
+ subject . update_attributes! ( name : 'Foma Kiniaev' )
832
917
end
833
918
end
834
919
835
920
it_behaves_like 'commit callbacks are called'
836
921
end
837
922
end
838
923
839
- context 'update_attributes' do
840
- let ( :subject ) do
841
- TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |subject |
842
- subject . after_commit_counter . reset
843
- subject . after_rollback_counter . reset
924
+ context 'destroy' do
925
+ context 'without :on option' do
926
+ let ( :after_commit_counter ) do
927
+ TransactionsSpecCounter . new
844
928
end
845
- end
846
929
847
- before do
848
- subject . transaction do
849
- subject . update_attributes! ( name : 'Austin Powers' )
930
+ let ( :after_rollback_counter ) do
931
+ TransactionsSpecCounter . new
850
932
end
851
- end
852
933
853
- it_behaves_like 'commit callbacks are called'
854
- end
934
+ let ( :subject ) do
935
+ TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |p |
936
+ p . after_commit_counter = after_commit_counter
937
+ p . after_rollback_counter = after_rollback_counter
938
+ end
939
+ end
855
940
856
- context 'destroy' do
857
- let ( :after_commit_counter ) do
858
- TransactionsSpecCounter . new
859
- end
941
+ before do
942
+ subject . transaction do
943
+ subject . destroy
944
+ end
945
+ end
860
946
861
- let ( :after_rollback_counter ) do
862
- TransactionsSpecCounter . new
947
+ it_behaves_like 'commit callbacks are called'
863
948
end
864
949
865
- let ( :subject ) do
866
- TransactionsSpecPerson . create! ( name : 'James Bond' ) . tap do |p |
867
- p . after_commit_counter = after_commit_counter
868
- p . after_rollback_counter = after_rollback_counter
950
+ context 'with :on option' do
951
+ let ( :after_commit_counter ) do
952
+ TransactionsSpecCounter . new
869
953
end
870
- end
871
954
872
- before do
873
- subject . transaction do
874
- subject . destroy
955
+ let ( :after_rollback_counter ) do
956
+ TransactionsSpecCounter . new
875
957
end
876
- end
877
958
878
- it_behaves_like 'commit callbacks are called'
959
+ let ( :subject ) do
960
+ TransactionsSpecPersonWithOnDestroy . create! ( name : 'James Bond' ) . tap do |p |
961
+ p . after_commit_counter = after_commit_counter
962
+ p . after_rollback_counter = after_rollback_counter
963
+ end
964
+ end
965
+
966
+ before do
967
+ subject . transaction do
968
+ subject . destroy
969
+ end
970
+ end
971
+
972
+ it_behaves_like 'commit callbacks are called'
973
+ end
879
974
end
880
975
end
881
976
882
977
context 'when rollback the transaction' do
883
978
context 'create' do
884
- let! ( :subject ) do
885
- person = nil
886
- TransactionsSpecPerson . transaction do
887
- person = TransactionsSpecPerson . create! ( name : 'James Bond' )
888
- raise Mongoid ::Errors ::Rollback
979
+ context 'without :on option' do
980
+ let! ( :subject ) do
981
+ person = nil
982
+ TransactionsSpecPerson . transaction do
983
+ person = TransactionsSpecPerson . create! ( name : 'James Bond' )
984
+ raise Mongoid ::Errors ::Rollback
985
+ end
986
+ person
889
987
end
890
- person
988
+
989
+ it_behaves_like 'rollback callbacks are called'
891
990
end
892
991
893
- it_behaves_like 'rollback callbacks are called'
992
+ context 'with :on option' do
993
+ let! ( :subject ) do
994
+ person = nil
995
+ TransactionsSpecPersonWithOnCreate . transaction do
996
+ person = TransactionsSpecPersonWithOnCreate . create! ( name : 'James Bond' )
997
+ raise Mongoid ::Errors ::Rollback
998
+ end
999
+ person
1000
+ end
1001
+
1002
+ it_behaves_like 'rollback callbacks are called'
1003
+ end
894
1004
end
895
1005
896
1006
context 'save' do
0 commit comments