Skip to content
This repository
Browse code

Returning false in before_destroy should cancel action. Closes #1829.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2381 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 83b390bcfcded8bd5263883ddf741ef35bccc801 1 parent 8e78e93
Scott Barron authored September 28, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Returning false from before_destroy should cancel the action.  #1829 [Jeremy Huffman]
  4
+
3 5
 * Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type.  #2256 [mat <mat@absolight.fr>]
4 6
 
5 7
 * Extensive documentation for the abstract database adapter.  #2250 [François Beausoleil <fbeausoleil@ftml.net>]
4  activerecord/lib/active_record/callbacks.rb
@@ -345,9 +345,9 @@ def callback(method)
345 345
           return false if result == false
346 346
         end
347 347
 
348  
-        send(method) if respond_to_without_attributes?(method)
  348
+        result = send(method) if respond_to_without_attributes?(method)
349 349
 
350  
-        return true
  350
+        return result
351 351
       end
352 352
 
353 353
       def callbacks_for(method)
37  activerecord/test/callbacks_test.rb
@@ -70,6 +70,25 @@ def on_after_save
70 70
   end
71 71
 end
72 72
 
  73
+class ImmutableDeveloper < ActiveRecord::Base
  74
+  set_table_name 'developers'
  75
+
  76
+  before_destroy :cancel_destroy
  77
+  
  78
+  private
  79
+  
  80
+  def cancel_destroy
  81
+    return false
  82
+  end
  83
+end
  84
+
  85
+class ImmutableMethodDeveloper < ActiveRecord::Base
  86
+  set_table_name 'developers'
  87
+
  88
+  def before_destroy    
  89
+    return false
  90
+  end
  91
+end
73 92
 
74 93
 class CallbacksTest < Test::Unit::TestCase
75 94
   fixtures :developers
@@ -282,6 +301,24 @@ def test_delete
282 301
       [ :after_initialize,            :block  ],
283 302
     ], david.history
284 303
   end
  304
+  
  305
+  def test_before_destroy_returning_false
  306
+    david = ImmutableDeveloper.find(1)
  307
+    devs = ImmutableDeveloper.find(:all).size
  308
+    assert !david.destroy
  309
+    # cancel_destroy returns false so the destruction should
  310
+    # be cancelled
  311
+    assert_equal ImmutableDeveloper.find(:all).size, devs
  312
+    
  313
+    david = ImmutableMethodDeveloper.find(1)
  314
+    devs = ImmutableMethodDeveloper.find(:all).size
  315
+    assert !david.destroy
  316
+    # before_destroy returns false so the destruction should
  317
+    # be cancelled
  318
+    assert_equal ImmutableMethodDeveloper.find(:all).size, devs
  319
+  end
  320
+  
  321
+  
285 322
 
286 323
   def test_zzz_callback_returning_false # must be run last since we modify CallbackDeveloper
287 324
     david = CallbackDeveloper.find(1)

0 notes on commit 83b390b

Please sign in to comment.
Something went wrong with that request. Please try again.