aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/validations_repair_helper.rb
blob: e04738d209a943ffd1da69e7dad95b922662ad75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module ActiveRecord
  module ValidationsRepairHelper
    extend ActiveSupport::Concern

    module ClassMethods
      def repair_validations(*model_classes)
        setup do
          @_stored_callbacks = {}
          model_classes.each do |k|
            @_stored_callbacks[k] = k._validate_callbacks.dup
          end
        end
        teardown do
          model_classes.each do |k|
            k._validate_callbacks = @_stored_callbacks[k]
            k.__update_callbacks(:validate)
          end
        end
      end
    end

    def repair_validations(*model_classes, &block)
      @__stored_callbacks = {}
      model_classes.each do |k|
        @__stored_callbacks[k] = k._validate_callbacks.dup
      end
      return block.call
    ensure
      model_classes.each do |k|
        k._validate_callbacks = @__stored_callbacks[k]
        k.__update_callbacks(:validate)
      end
    end
  end
end