aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations_repair_helper.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-20 10:32:24 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-20 10:32:24 +0000
commit5b1a1bf5bfc520248285b036672146122dd2a815 (patch)
tree31d82a9155db6c9e553888da6375bd89fdb122cf /activemodel/lib/active_model/validations_repair_helper.rb
parent93e2d378df5f599e3e48e74932f37e1c679f633e (diff)
downloadrails-5b1a1bf5bfc520248285b036672146122dd2a815.tar.gz
rails-5b1a1bf5bfc520248285b036672146122dd2a815.tar.bz2
rails-5b1a1bf5bfc520248285b036672146122dd2a815.zip
Make Active Model test suite similar to Active Record
Diffstat (limited to 'activemodel/lib/active_model/validations_repair_helper.rb')
-rw-r--r--activemodel/lib/active_model/validations_repair_helper.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations_repair_helper.rb b/activemodel/lib/active_model/validations_repair_helper.rb
new file mode 100644
index 0000000000..99e3b2f631
--- /dev/null
+++ b/activemodel/lib/active_model/validations_repair_helper.rb
@@ -0,0 +1,48 @@
+module ActiveModel
+ module ValidationsRepairHelper
+ 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 = Toolbox.record_validations(*model_classes)
+ end
+ teardown do
+ Toolbox.reset_validations(@validation_repairs)
+ end
+ end
+ end
+
+ def repair_validations(*model_classes, &block)
+ validation_repairs = Toolbox.record_validations(*model_classes)
+ return block.call
+ ensure
+ Toolbox.reset_validations(validation_repairs)
+ end
+ end
+end