diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-11-27 09:42:27 +0000 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-11-27 09:42:27 +0000 |
commit | 939dc36052039e65a0af2936ffb1135281397eb2 (patch) | |
tree | 804e0745f2e3d94301c4df1f62ef8e9f68c3acb5 /lib/dm-validations-with-empty-errors-hack.rb | |
parent | df1a036268e52396eca349b2803a5e05f891733c (diff) |
Monkey patch to cause validations to save anyway, if no error has been
given.
Diffstat (limited to 'lib/dm-validations-with-empty-errors-hack.rb')
-rw-r--r-- | lib/dm-validations-with-empty-errors-hack.rb | 30 |
1 files changed, 30 insertions, 0 deletions
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 + + + |