aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-12-19 13:52:21 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-12-19 13:52:21 +0000
commit89b75814045e811d52b19278ae27b5f45c6d9dd6 (patch)
tree6b796d32bf3282e47e7c68e2d4b29cb987fba066 /activerecord
parent8a92cdc8638d1f91aaa55fc47743a6210ad2181b (diff)
downloadrails-89b75814045e811d52b19278ae27b5f45c6d9dd6.tar.gz
rails-89b75814045e811d52b19278ae27b5f45c6d9dd6.tar.bz2
rails-89b75814045e811d52b19278ae27b5f45c6d9dd6.zip
Add repair_helper.rb file I forgot in previous commit 8a92cd
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/cases/repair_helper.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/activerecord/test/cases/repair_helper.rb b/activerecord/test/cases/repair_helper.rb
new file mode 100644
index 0000000000..0155668811
--- /dev/null
+++ b/activerecord/test/cases/repair_helper.rb
@@ -0,0 +1,50 @@
+module ActiveRecord
+ module Testing
+ module RepairHelper
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ end
+ end
+
+ module Toolbox
+ def self.record_validations(*model_classes)
+ model_classes.inject({}) do |repair, klass|
+ repair[klass] ||= {}
+ [:validate, :validate_on_create, :validate_on_update].each do |callback|
+ the_callback = klass.instance_variable_get("@#{callback.to_s}_callbacks")
+ repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup)
+ end
+ repair
+ end
+ end
+
+ def self.reset_validations(recorded)
+ recorded.each do |klass, repairs|
+ [:validate, :validate_on_create, :validate_on_update].each do |callback|
+ klass.instance_variable_set("@#{callback.to_s}_callbacks", repairs[callback])
+ end
+ end
+ end
+ end
+
+ module ClassMethods
+ def repair_validations(*model_classes)
+ setup do
+ @validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes)
+ end
+ teardown do
+ ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(@validation_repairs)
+ end
+ end
+ end
+
+ def repair_validations(*model_classes, &block)
+ validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes)
+ return block.call
+ ensure
+ ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(validation_repairs)
+ end
+ end
+ end
+end