1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/lib/active_model/validations_repair_helper.rb
2009-05-30 09:35:24 -05:00

44 lines
1.3 KiB
Ruby

module ActiveModel
module ValidationsRepairHelper
extend ActiveSupport::Concern
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