public
Description: Conditional checks on Rails filters
Clone URL: git://github.com/thoughtbot/when.git
added tests for block callbacks for callbacks and filters

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@346 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jcarroll (author)
Thu Feb 14 17:58:55 -0800 2008
commit  06047c40968b6befca5871d3942ed1aae4ffd7ac
tree    0d56c6158126350681893fa9d5e6bd2cea2de95e
parent  432aa3e537c75fa52a742877a6623b92383ebade
...
11
12
13
14
15
16
 
 
 
17
18
19
...
22
23
24
25
26
 
 
27
28
29
...
11
12
13
 
 
 
14
15
16
17
18
19
...
22
23
24
 
 
25
26
27
28
29
0
@@ -11,9 +11,9 @@ module When
0
           src = <<-END
0
             def #{callback}_with_conditions(*callbacks, &block)
0
               options = callbacks.extract_options!
0
-# if block_given?
0
-# callbacks << block
0
-# end
0
+ if block_given?
0
+ callbacks << block
0
+ end
0
               callbacks.each do |callback|
0
                 #{callback}_without_conditions do |record|
0
                   unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
0
@@ -22,8 +22,8 @@ module When
0
                       record.send callback
0
                     elsif callback.class == String
0
                       eval(callback, binding)
0
-# elsif callback.class == Proc || callback.class == Method
0
-# callback.call(record)
0
+ elsif callback.class == Proc || callback.class == Method
0
+ callback.call(record)
0
                     else
0
                       raise ActiveRecord::ActiveRecordError,
0
                         "Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."
...
9
10
11
12
13
14
 
 
 
15
16
17
...
20
21
22
23
24
 
 
25
26
27
...
9
10
11
 
 
 
12
13
14
15
16
17
...
20
21
22
 
 
23
24
25
26
27
0
@@ -9,9 +9,9 @@ module When
0
           src = <<-END
0
             def #{filter}_with_conditions(*filters, &block)
0
               options = filters.extract_options!
0
-# if block_given?
0
-# options << block
0
-# end
0
+ if block_given?
0
+ filters << block
0
+ end
0
               filters.each do |filter|
0
                 #{filter}_without_conditions(options) do |controller|
0
                   unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
0
@@ -20,8 +20,8 @@ module When
0
                       controller.send filter
0
                     elsif filter.class == String
0
                       eval(filter, binding)
0
-# elsif filter.class == Proc || filter.class == Method
0
-# filter.call(controller)
0
+ elsif filter.class == Proc || filter.class == Method
0
+ filter.call(controller)
0
                     else
0
                       raise ActionController::ActionControllerError,
0
                         "Filters must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."
...
300
301
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
304
305
...
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
0
@@ -300,6 +300,138 @@ class CallbacksTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ conditions.each do |condition|
0
+ basic_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+ end
0
+
0
+ update_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+ end
0
+
0
+ destroy_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_toggle_flag" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.toggle_flag
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert company.destroy
0
+ assert ! company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_toggle_flag" do
0
+ Company.send callback.to_sym, :if => condition do |record|
0
+ record.toggle_flag
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert company.destroy
0
+ assert ! company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_toggle_flag" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.toggle_flag
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert company.destroy
0
+ assert company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_toggle_flag" do
0
+ Company.send callback.to_sym, :unless => condition do |record|
0
+ record.toggle_flag
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert company.destroy
0
+ assert company.flag
0
+ end
0
+ end
0
+ end
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const Company.to_s if const_defined? Company.to_s
...
343
344
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
347
348
...
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
0
@@ -343,6 +343,179 @@ class FiltersTest < ActionController::TestCase
0
     end
0
   end
0
 
0
+ conditions.each do |condition|
0
+ basic_filters.each do |filter|
0
+ define_method "test_#{filter}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :only => :show, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :only => :show, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :only => :show, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :only => :show, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :except => :show, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :except => :show, :if => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :except => :show, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :except => :show, :unless => condition do |controller|
0
+ controller.change_name
0
+ end
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+ end
0
+ end
0
+
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const CompaniesController.to_s if const_defined? CompaniesController.to_s
...
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
199
200
...
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
0
@@ -195,6 +195,92 @@ class ValidationsTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ conditions.each do |condition|
0
+ basic_validations.each do |validation|
0
+ define_method "test_#{validation}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send validation.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send validation.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send validation.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send validation.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+ end
0
+
0
+ update_validations.each do |validation|
0
+ define_method "test_#{validation}_with_block_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send validation.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_calback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send validation.to_sym, :if => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send validation.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', company.name
0
+ end
0
+
0
+ define_method "test_#{validation}_with_block_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send validation.to_sym, :unless => condition do |record|
0
+ record.change_name
0
+ end
0
+
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
+ assert company.save
0
+ assert_equal 'new name', company.name
0
+ end
0
+ end
0
+ end
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const Company.to_s if const_defined? Company.to_s

Comments

    No one has commented yet.