aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/callbacks_test.rb
diff options
context:
space:
mode:
authorJohn Foley <john@thefol.io>2012-07-02 10:18:12 -0600
committerJohn Foley <john@thefol.io>2012-09-23 12:53:50 -0600
commitd66d6076b2a723c72d97fb8e8f90d83249f9bfe5 (patch)
treee5136d7466ff78671f6d999cb5a8ca0c01b1faf2 /activerecord/test/cases/callbacks_test.rb
parent09d298eeccd46857dd018aea7ba0a880640cc289 (diff)
downloadrails-d66d6076b2a723c72d97fb8e8f90d83249f9bfe5.tar.gz
rails-d66d6076b2a723c72d97fb8e8f90d83249f9bfe5.tar.bz2
rails-d66d6076b2a723c72d97fb8e8f90d83249f9bfe5.zip
Fix collisions with before and after validation callbacks.
This commit allows a user to do something like: before_validation :do_stuff, :on => [ :create, :update ] after_validation :do_more, :on => [ :create, :update ]
Diffstat (limited to 'activerecord/test/cases/callbacks_test.rb')
-rw-r--r--activerecord/test/cases/callbacks_test.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index deeef3a3fd..7457bafd4e 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -137,6 +137,32 @@ class OnCallbacksDeveloper < ActiveRecord::Base
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_#{self.validation_context}".to_sym
+ end
+
+ def after_validation_on_create_and_update
+ history << "after_validation_on_#{self.validation_context}".to_sym
+ end
+
+ def history
+ @history ||= []
+ end
+end
+
class CallbackCancellationDeveloper < ActiveRecord::Base
self.table_name = 'developers'
@@ -285,6 +311,17 @@ class CallbacksTest < ActiveRecord::TestCase
], 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
@@ -344,6 +381,18 @@ class CallbacksTest < ActiveRecord::TestCase
], 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