aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations_repair_helper.rb
blob: 40741e6dbe1d571eb20a7932cda307680723f1dd (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 ActiveModel
  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