aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/callbacks_test.rb
blob: b4026078f185f72d76b89f92eb3f94978195b1b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# frozen_string_literal: true

require "cases/helper"
require "models/developer"
require "models/computer"

class CallbackDeveloper < ActiveRecord::Base
  self.table_name = "developers"

  class << self
    def callback_proc(callback_method)
      Proc.new { |model| model.history << [callback_method, :proc] }
    end

    def define_callback_method(callback_method)
      define_method(callback_method) do
        history << [callback_method, :method]
      end
      send(callback_method, :"#{callback_method}")
    end

    def callback_object(callback_method)
      klass = Class.new
      klass.define_method(callback_method) do |model|
        model.history << [callback_method, :object]
      end
      klass.new
    end
  end

  ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
    next if callback_method.to_s.start_with?("around_")
    define_callback_method(callback_method)
    send(callback_method, callback_proc(callback_method))
    send(callback_method, callback_object(callback_method))
    send(callback_method) { |model| model.history << [callback_method, :block] }
  end

  def history
    @history ||= []
  end
end

class CallbackDeveloperWithHaltedValidation < CallbackDeveloper
  before_validation proc { |model| model.history << [:before_validation, :throwing_abort]; throw(:abort) }
  before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
end

class ParentDeveloper < ActiveRecord::Base
  self.table_name = "developers"
  attr_accessor :after_save_called
  before_validation { |record| record.after_save_called = true }
end

class ChildDeveloper < ParentDeveloper
end

class ImmutableDeveloper < ActiveRecord::Base
  self.table_name = "developers"

  validates_inclusion_of :salary, in: 50000..200000

  before_save :cancel
  before_destroy :cancel

  private
    def cancel
      false
    end
end

class DeveloperWithCanceledCallbacks < ActiveRecord::Base
  self.table_name = "developers"

  validates_inclusion_of :salary, in: 50000..200000

  before_save :cancel
  before_destroy :cancel

  private
    def cancel
      throw(:abort)
    end
end

class OnCallbacksDeveloper < ActiveRecord::Base
  self.table_name = "developers"

  before_validation { history << :before_validation }
  before_validation(on: :create) { history << :before_validation_on_create }
  before_validation(on: :update) { history << :before_validation_on_update }

  validate do
    history << :validate
  end

  after_validation { history << :after_validation }
  after_validation(on: :create) { history << :after_validation_on_create }
  after_validation(on: :update) { history << :after_validation_on_update }

  def history
    @history ||= []
  end
end

class ContextualCallbacksDeveloper < ActiveRecord::Base
  self.table_name = "developers"

  before_validation { history << :before_validation }
  before_validation :before_validation_on_create_and_update, on: [ :create, :update ]

  validate do
    history << :validate
  end

  after_validation { history << :after_validation }
  after_validation :after_validation_on_create_and_update, on: [ :create, :update ]

  def before_validation_on_create_and_update
    history << "before_validation_on_#{validation_context}".to_sym
  end

  def after_validation_on_create_and_update
    history << "after_validation_on_#{validation_context}".to_sym
  end

  def history
    @history ||= []
  end
end

class CallbackHaltedDeveloper < ActiveRecord::Base
  self.table_name = "developers"

  attr_reader   :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
  attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy

  before_save    { throw(:abort) if defined?(@cancel_before_save) }
  before_create  { throw(:abort) if @cancel_before_create  }
  before_update  { throw(:abort) if @cancel_before_update  }
  before_destroy { throw(:abort) if @cancel_before_destroy }

  after_save    { @after_save_called    = true }
  after_update  { @after_update_called  = true }
  after_create  { @after_create_called  = true }
  after_destroy { @after_destroy_called = true }
end

class CallbacksTest < ActiveRecord::TestCase
  fixtures :developers

  def test_initialize
    david = CallbackDeveloper.new
    assert_equal [
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
    ], david.history
  end

  def test_find
    david = CallbackDeveloper.find(1)
    assert_equal [
      [ :after_find,            :method ],
      [ :after_find,            :proc   ],
      [ :after_find,            :object ],
      [ :after_find,            :block  ],
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
    ], david.history
  end

  def test_new_valid?
    david = CallbackDeveloper.new
    david.valid?
    assert_equal [
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
      [ :before_validation,           :method ],
      [ :before_validation,           :proc   ],
      [ :before_validation,           :object ],
      [ :before_validation,           :block  ],
      [ :after_validation,            :method ],
      [ :after_validation,            :proc   ],
      [ :after_validation,            :object ],
      [ :after_validation,            :block  ],
    ], david.history
  end

  def test_existing_valid?
    david = CallbackDeveloper.find(1)
    david.valid?
    assert_equal [
      [ :after_find,            :method ],
      [ :after_find,            :proc   ],
      [ :after_find,            :object ],
      [ :after_find,            :block  ],
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
      [ :before_validation,           :method ],
      [ :before_validation,           :proc   ],
      [ :before_validation,           :object ],
      [ :before_validation,           :block  ],
      [ :after_validation,            :method ],
      [ :after_validation,            :proc   ],
      [ :after_validation,            :object ],
      [ :after_validation,            :block  ],
    ], david.history
  end

  def test_create
    david = CallbackDeveloper.create("name" => "David", "salary" => 1000000)
    assert_equal [
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
      [ :before_validation,           :method ],
      [ :before_validation,           :proc   ],
      [ :before_validation,           :object ],
      [ :before_validation,           :block  ],
      [ :after_validation,            :method ],
      [ :after_validation,            :proc   ],
      [ :after_validation,            :object ],
      [ :after_validation,            :block  ],
      [ :before_save,                 :method ],
      [ :before_save,                 :proc   ],
      [ :before_save,                 :object ],
      [ :before_save,                 :block  ],
      [ :before_create,               :method ],
      [ :before_create,               :proc   ],
      [ :before_create,               :object ],
      [ :before_create,               :block  ],
      [ :after_create,                :method ],
      [ :after_create,                :proc   ],
      [ :after_create,                :object ],
      [ :after_create,                :block  ],
      [ :after_save,                  :method ],
      [ :after_save,                  :proc   ],
      [ :after_save,                  :object ],
      [ :after_save,                  :block  ],
      [ :after_commit,                :block  ],
      [ :after_commit,                :object ],
      [ :after_commit,                :proc   ],
      [ :after_commit,                :method ]
    ], david.history
  end

  def test_validate_on_create
    david = OnCallbacksDeveloper.create("name" => "David", "salary" => 1000000)
    assert_equal [
      :before_validation,
      :before_validation_on_create,
      :validate,
      :after_validation,
      :after_validation_on_create
    ], david.history
  end

  def test_validate_on_contextual_create
    david = ContextualCallbacksDeveloper.create("name" => "David", "salary" => 1000000)
    assert_equal [
      :before_validation,
      :before_validation_on_create,
      :validate,
      :after_validation,
      :after_validation_on_create
    ], david.history
  end

  def test_update
    david = CallbackDeveloper.find(1)
    david.save
    assert_equal [
      [ :after_find,            :method ],
      [ :after_find,            :proc   ],
      [ :after_find,            :object ],
      [ :after_find,            :block  ],
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
      [ :before_validation,           :method ],
      [ :before_validation,           :proc   ],
      [ :before_validation,           :object ],
      [ :before_validation,           :block  ],
      [ :after_validation,            :method ],
      [ :after_validation,            :proc   ],
      [ :after_validation,            :object ],
      [ :after_validation,            :block  ],
      [ :before_save,                 :method ],
      [ :before_save,                 :proc   ],
      [ :before_save,                 :object ],
      [ :before_save,                 :block  ],
      [ :before_update,               :method ],
      [ :before_update,               :proc   ],
      [ :before_update,               :object ],
      [ :before_update,               :block  ],
      [ :after_update,                :method ],
      [ :after_update,                :proc   ],
      [ :after_update,                :object ],
      [ :after_update,                :block  ],
      [ :after_save,                  :method ],
      [ :after_save,                  :proc   ],
      [ :after_save,                  :object ],
      [ :after_save,                  :block  ],
      [ :after_commit,                :block  ],
      [ :after_commit,                :object ],
      [ :after_commit,                :proc   ],
      [ :after_commit,                :method ]
    ], david.history
  end

  def test_validate_on_update
    david = OnCallbacksDeveloper.find(1)
    david.save
    assert_equal [
      :before_validation,
      :before_validation_on_update,
      :validate,
      :after_validation,
      :after_validation_on_update
    ], david.history
  end

  def test_validate_on_contextual_update
    david = ContextualCallbacksDeveloper.find(1)
    david.save
    assert_equal [
      :before_validation,
      :before_validation_on_update,
      :validate,
      :after_validation,
      :after_validation_on_update
    ], david.history
  end

  def test_destroy
    david = CallbackDeveloper.find(1)
    david.destroy
    assert_equal [
      [ :after_find,            :method ],
      [ :after_find,            :proc   ],
      [ :after_find,            :object ],
      [ :after_find,            :block  ],
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
      [ :before_destroy,              :method ],
      [ :before_destroy,              :proc   ],
      [ :before_destroy,              :object ],
      [ :before_destroy,              :block  ],
      [ :after_destroy,               :method ],
      [ :after_destroy,               :proc   ],
      [ :after_destroy,               :object ],
      [ :after_destroy,               :block  ],
      [ :after_commit,                :block  ],
      [ :after_commit,                :object ],
      [ :after_commit,                :proc   ],
      [ :after_commit,                :method ]
    ], david.history
  end

  def test_delete
    david = CallbackDeveloper.find(1)
    CallbackDeveloper.delete(david.id)
    assert_equal [
      [ :after_find,            :method ],
      [ :after_find,            :proc   ],
      [ :after_find,            :object ],
      [ :after_find,            :block  ],
      [ :after_initialize,            :method ],
      [ :after_initialize,            :proc   ],
      [ :after_initialize,            :object ],
      [ :after_initialize,            :block  ],
    ], david.history
  end

  def assert_save_callbacks_not_called(someone)
    assert_not someone.after_save_called
    assert_not someone.after_create_called
    assert_not someone.after_update_called
  end
  private :assert_save_callbacks_not_called

  def test_before_create_throwing_abort
    someone = CallbackHaltedDeveloper.new
    someone.cancel_before_create = true
    assert_predicate someone, :valid?
    assert_not someone.save
    assert_save_callbacks_not_called(someone)
  end

  def test_before_save_throwing_abort
    david = DeveloperWithCanceledCallbacks.find(1)
    assert_predicate david, :valid?
    assert_not david.save
    exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
    assert_equal david, exc.record

    david = DeveloperWithCanceledCallbacks.find(1)
    david.salary = 10_000_000
    assert_not_predicate david, :valid?
    assert_not david.save
    assert_raise(ActiveRecord::RecordInvalid) { david.save! }

    someone = CallbackHaltedDeveloper.find(1)
    someone.cancel_before_save = true
    assert_predicate someone, :valid?
    assert_not someone.save
    assert_save_callbacks_not_called(someone)
  end

  def test_before_update_throwing_abort
    someone = CallbackHaltedDeveloper.find(1)
    someone.cancel_before_update = true
    assert_predicate someone, :valid?
    assert_not someone.save
    assert_save_callbacks_not_called(someone)
  end

  def test_before_destroy_throwing_abort
    david = DeveloperWithCanceledCallbacks.find(1)
    assert_not david.destroy
    exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
    assert_equal david, exc.record
    assert_not_nil ImmutableDeveloper.find_by_id(1)

    someone = CallbackHaltedDeveloper.find(1)
    someone.cancel_before_destroy = true
    assert_not someone.destroy
    assert_raise(ActiveRecord::RecordNotDestroyed) { someone.destroy! }
    assert_not someone.after_destroy_called
  end

  def test_callback_throwing_abort
    david = CallbackDeveloperWithHaltedValidation.find(1)
    david.save
    assert_equal [
      [ :after_find,        :method ],
      [ :after_find,        :proc   ],
      [ :after_find,        :object ],
      [ :after_find,        :block  ],
      [ :after_initialize,  :method ],
      [ :after_initialize,  :proc   ],
      [ :after_initialize,  :object ],
      [ :after_initialize,  :block  ],
      [ :before_validation, :method ],
      [ :before_validation, :proc   ],
      [ :before_validation, :object ],
      [ :before_validation, :block  ],
      [ :before_validation, :throwing_abort ],
    ], david.history
  end

  def test_inheritance_of_callbacks
    parent = ParentDeveloper.new
    assert_not parent.after_save_called
    parent.save
    assert parent.after_save_called

    child = ChildDeveloper.new
    assert_not child.after_save_called
    child.save
    assert child.after_save_called
  end

  def test_before_save_doesnt_allow_on_option
    exception = assert_raises ArgumentError do
      Class.new(ActiveRecord::Base) do
        before_save(on: :create) { }
      end
    end
    assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
  end

  def test_around_save_doesnt_allow_on_option
    exception = assert_raises ArgumentError do
      Class.new(ActiveRecord::Base) do
        around_save(on: :create) { }
      end
    end
    assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
  end

  def test_after_save_doesnt_allow_on_option
    exception = assert_raises ArgumentError do
      Class.new(ActiveRecord::Base) do
        after_save(on: :create) { }
      end
    end
    assert_equal "Unknown key: :on. Valid keys are: :if, :unless, :prepend", exception.message
  end
end