1
1
# rubocop:todo all
2
2
module InterceptableSpec
3
3
class CallbackRegistry
4
- def initialize
4
+ def initialize ( only : [ ] )
5
5
@calls = [ ]
6
+ @only = only
6
7
end
7
8
8
9
def record_call ( cls , cb )
10
+ return unless @only . empty? || @only . any? { |pat | pat == cb }
9
11
@calls << [ cls , cb ]
10
12
end
11
13
@@ -16,6 +18,8 @@ module CallbackTracking
16
18
extend ActiveSupport ::Concern
17
19
18
20
included do
21
+ field :name , type : String
22
+
19
23
%i(
20
24
validation save create update
21
25
) . each do |what |
@@ -35,199 +39,110 @@ module CallbackTracking
35
39
end
36
40
end
37
41
end
38
- end
39
-
40
- class CbHasOneParent
41
- include Mongoid ::Document
42
42
43
- has_one :child , autosave : true , class_name : "CbHasOneChild" , inverse_of : :parent
43
+ attr_accessor :callback_registry
44
44
45
- def initialize ( callback_registry )
45
+ def initialize ( callback_registry , * args , ** kwargs )
46
46
@callback_registry = callback_registry
47
- super ( )
47
+ super ( * args , ** kwargs )
48
48
end
49
+ end
49
50
50
- attr_accessor :callback_registry
51
-
51
+ module RootInsertable
52
52
def insert_as_root
53
53
@callback_registry &.record_call ( self . class , :insert_into_database )
54
54
super
55
55
end
56
+ end
56
57
58
+ class CbHasOneParent
59
+ include Mongoid ::Document
57
60
include CallbackTracking
61
+ include RootInsertable
62
+
63
+ has_one :child , autosave : true , class_name : "CbHasOneChild" , inverse_of : :parent
58
64
end
59
65
60
66
class CbHasOneChild
61
67
include Mongoid ::Document
68
+ include CallbackTracking
62
69
63
70
belongs_to :parent , class_name : "CbHasOneParent" , inverse_of : :child
64
-
65
- def initialize ( callback_registry )
66
- @callback_registry = callback_registry
67
- super ( )
68
- end
69
-
70
- attr_accessor :callback_registry
71
-
72
- include CallbackTracking
73
71
end
74
72
75
73
class CbHasManyParent
76
74
include Mongoid ::Document
75
+ include CallbackTracking
76
+ include RootInsertable
77
77
78
78
has_many :children , autosave : true , class_name : "CbHasManyChild" , inverse_of : :parent
79
-
80
- def initialize ( callback_registry )
81
- @callback_registry = callback_registry
82
- super ( )
83
- end
84
-
85
- attr_accessor :callback_registry
86
-
87
- def insert_as_root
88
- @callback_registry &.record_call ( self . class , :insert_into_database )
89
- super
90
- end
91
-
92
- include CallbackTracking
93
79
end
94
80
95
81
class CbHasManyChild
96
82
include Mongoid ::Document
83
+ include CallbackTracking
97
84
98
85
belongs_to :parent , class_name : "CbHasManyParent" , inverse_of : :children
99
-
100
- def initialize ( callback_registry )
101
- @callback_registry = callback_registry
102
- super ( )
103
- end
104
-
105
- attr_accessor :callback_registry
106
-
107
- include CallbackTracking
108
86
end
109
87
110
88
class CbEmbedsOneParent
111
89
include Mongoid ::Document
90
+ include CallbackTracking
91
+ include RootInsertable
112
92
113
93
field :name
114
94
115
95
embeds_one :child , cascade_callbacks : true , class_name : "CbEmbedsOneChild" , inverse_of : :parent
116
-
117
- def initialize ( callback_registry )
118
- @callback_registry = callback_registry
119
- super ( )
120
- end
121
-
122
- attr_accessor :callback_registry
123
-
124
- def insert_as_root
125
- @callback_registry &.record_call ( self . class , :insert_into_database )
126
- super
127
- end
128
-
129
- include CallbackTracking
130
96
end
131
97
132
98
class CbEmbedsOneChild
133
99
include Mongoid ::Document
100
+ include CallbackTracking
134
101
135
102
field :age
136
103
137
104
embedded_in :parent , class_name : "CbEmbedsOneParent" , inverse_of : :child
138
-
139
- def initialize ( callback_registry )
140
- @callback_registry = callback_registry
141
- super ( )
142
- end
143
-
144
- attr_accessor :callback_registry
145
-
146
- include CallbackTracking
147
105
end
148
106
149
107
class CbEmbedsManyParent
150
108
include Mongoid ::Document
109
+ include CallbackTracking
110
+ include RootInsertable
151
111
152
112
embeds_many :children , cascade_callbacks : true , class_name : "CbEmbedsManyChild" , inverse_of : :parent
153
-
154
- def initialize ( callback_registry )
155
- @callback_registry = callback_registry
156
- super ( )
157
- end
158
-
159
- attr_accessor :callback_registry
160
-
161
- def insert_as_root
162
- @callback_registry &.record_call ( self . class , :insert_into_database )
163
- super
164
- end
165
-
166
- include CallbackTracking
167
113
end
168
114
169
115
class CbEmbedsManyChild
170
116
include Mongoid ::Document
117
+ include CallbackTracking
171
118
172
119
embedded_in :parent , class_name : "CbEmbedsManyParent" , inverse_of : :children
173
-
174
- def initialize ( callback_registry )
175
- @callback_registry = callback_registry
176
- super ( )
177
- end
178
-
179
- attr_accessor :callback_registry
180
-
181
- include CallbackTracking
182
120
end
183
121
184
122
class CbParent
185
123
include Mongoid ::Document
186
-
187
- def initialize ( callback_registry )
188
- @callback_registry = callback_registry
189
- super ( )
190
- end
191
-
192
- attr_accessor :callback_registry
124
+ include CallbackTracking
193
125
194
126
embeds_many :cb_children
195
127
embeds_many :cb_cascaded_children , cascade_callbacks : true
196
-
197
- include CallbackTracking
128
+ embeds_many :cb_cascaded_nodes , cascade_callbacks : true , as : :parent
198
129
end
199
130
200
131
class CbChild
201
132
include Mongoid ::Document
133
+ include CallbackTracking
202
134
203
135
embedded_in :cb_parent
204
-
205
- def initialize ( callback_registry , options )
206
- @callback_registry = callback_registry
207
- super ( options )
208
- end
209
-
210
- attr_accessor :callback_registry
211
-
212
- include CallbackTracking
213
136
end
214
137
215
138
class CbCascadedChild
216
139
include Mongoid ::Document
140
+ include CallbackTracking
217
141
218
142
embedded_in :cb_parent
219
143
220
- def initialize ( callback_registry , options )
221
- @callback_registry = callback_registry
222
- super ( options )
223
- end
224
-
225
- attr_accessor :callback_registry
226
-
227
144
before_save :test_mongoid_state
228
145
229
- include CallbackTracking
230
-
231
146
private
232
147
233
148
# Helps test that cascading child callbacks have access to the Mongoid
@@ -238,6 +153,15 @@ def test_mongoid_state
238
153
Mongoid ::Threaded . stack ( 'interceptable' ) . push ( self )
239
154
end
240
155
end
156
+
157
+ class CbCascadedNode
158
+ include Mongoid ::Document
159
+ include CallbackTracking
160
+
161
+ embedded_in :parent , polymorphic : true
162
+
163
+ embeds_many :cb_cascaded_nodes , cascade_callbacks : true , as : :parent
164
+ end
241
165
end
242
166
243
167
class InterceptableBand
0 commit comments