From 48284d842acc64d154648623a5d6a995d8e33422 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Tue, 24 May 2011 21:55:46 -0400 Subject: [PATCH] Move validation code into the Validation plugin --- lib/mongo_mapper/plugins/callbacks.rb | 5 ----- .../plugins/embedded_callbacks.rb | 10 --------- lib/mongo_mapper/plugins/querying.rb | 3 +-- lib/mongo_mapper/plugins/validations.rb | 22 +++++++++++++++++++ 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/mongo_mapper/plugins/callbacks.rb b/lib/mongo_mapper/plugins/callbacks.rb index e677fe392..0983125b9 100644 --- a/lib/mongo_mapper/plugins/callbacks.rb +++ b/lib/mongo_mapper/plugins/callbacks.rb @@ -5,11 +5,6 @@ module Callbacks extend ActiveSupport::Concern module InstanceMethods - def valid?(context = nil) - context ||= (new_record? ? :create : :update) - super(context) && errors.empty? - end - def destroy run_callbacks(:destroy) { super } end diff --git a/lib/mongo_mapper/plugins/embedded_callbacks.rb b/lib/mongo_mapper/plugins/embedded_callbacks.rb index f357148e8..2d9ca5fd9 100644 --- a/lib/mongo_mapper/plugins/embedded_callbacks.rb +++ b/lib/mongo_mapper/plugins/embedded_callbacks.rb @@ -6,7 +6,6 @@ module EmbeddedCallbacks included do extend ::ActiveModel::Callbacks - include ::ActiveModel::Validations::Callbacks define_model_callbacks :save, :create, :update, :destroy, :only => [:before, :after] end @@ -32,12 +31,3 @@ def run_callbacks(callback, &block) end end end - -# Need to monkey patch ActiveModel for now since it uses the internal -# _run_validation_callbacks, which is impossible to override due to the -# way ActiveSupport::Callbacks is implemented. -ActiveModel::Validations::Callbacks.class_eval do - def run_validations! - run_callbacks(:validation) { super } - end -end diff --git a/lib/mongo_mapper/plugins/querying.rb b/lib/mongo_mapper/plugins/querying.rb index 1a65baa3d..f5e64f340 100644 --- a/lib/mongo_mapper/plugins/querying.rb +++ b/lib/mongo_mapper/plugins/querying.rb @@ -140,8 +140,7 @@ def update_multiple(docs) module InstanceMethods def save(options={}) options.assert_valid_keys(:validate, :safe) - options.reverse_merge!(:validate => true) - !options[:validate] || valid? ? create_or_update(options) : false + create_or_update(options) end def save!(options={}) diff --git a/lib/mongo_mapper/plugins/validations.rb b/lib/mongo_mapper/plugins/validations.rb index 07010aa14..d7c4b9a2a 100644 --- a/lib/mongo_mapper/plugins/validations.rb +++ b/lib/mongo_mapper/plugins/validations.rb @@ -5,6 +5,7 @@ module Validations extend ActiveSupport::Concern include ::ActiveModel::Validations + include ::ActiveModel::Validations::Callbacks module ClassMethods def validates_uniqueness_of(*attr_names) @@ -16,6 +17,18 @@ def validates_associated(*attr_names) end end + module InstanceMethods + def save(options = {}) + options.reverse_merge!(:validate => true) + !options[:validate] || valid? ? super : false + end + + def valid?(context = nil) + context ||= (new_record? ? :create : :update) + super(context) + end + end + class UniquenessValidator < ::ActiveModel::EachValidator def initialize(options) super(options.reverse_merge(:case_sensitive => true)) @@ -64,3 +77,12 @@ def validate_each(record, attribute, value) end end end + +# Need to monkey patch ActiveModel for now since it uses the internal +# _run_validation_callbacks, which is impossible to override due to the +# way ActiveSupport::Callbacks is implemented. +ActiveModel::Validations::Callbacks.class_eval do + def run_validations! + run_callbacks(:validation) { super } + end +end