aboutsummaryrefslogtreecommitdiff
path: root/lib/dm-validations-with-empty-errors-hack.rb
blob: db06d421ef2ef2bac1b0a0f591b9b384748e5d40 (plain)
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
require 'dm-validations'

module DataMapper
  module Validations
    #
    # Rewrite save method to save without validations, if the validations failed, but give no reason.
    #
    # @api private
    def save_self(*)
      if Validations::Context.any? && !valid?(model.validators.current_context)
        #
        # Don't do anything unusual if there is no logger available.
        #
        return false unless self.respond_to?("logger")

        if self.errors.empty?
          logger.warn "Forced to save #{self.inspect} without validations due to #{self.errors.inspect}."
          super
        else
          false
        end
      else
        super
      end
    end
  end
end