From 939dc36052039e65a0af2936ffb1135281397eb2 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 27 Nov 2012 09:42:27 +0000 Subject: Monkey patch to cause validations to save anyway, if no error has been given. --- lib/dm-validations-with-empty-errors-hack.rb | 30 ++++++++++++++++++++++++++++ lib/mauve/datamapper.rb | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/dm-validations-with-empty-errors-hack.rb diff --git a/lib/dm-validations-with-empty-errors-hack.rb b/lib/dm-validations-with-empty-errors-hack.rb new file mode 100644 index 0000000..db06d42 --- /dev/null +++ b/lib/dm-validations-with-empty-errors-hack.rb @@ -0,0 +1,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 + + + diff --git a/lib/mauve/datamapper.rb b/lib/mauve/datamapper.rb index 2a1f509..1814302 100644 --- a/lib/mauve/datamapper.rb +++ b/lib/mauve/datamapper.rb @@ -14,7 +14,7 @@ require 'dm-aggregates' end end require 'dm-types' -require 'dm-validations' +require 'dm-validations-with-empty-errors-hack' # DataMapper::Model.raise_on_save_failure = true -- cgit v1.2.1