lib/mongomodel/document/validations.rb in mongomodel-0.4.1 vs lib/mongomodel/document/validations.rb in mongomodel-0.4.2

- old
+ new

@@ -1,15 +1,10 @@ module MongoModel module DocumentExtensions module Validations extend ActiveSupport::Concern - - included do - alias_method_chain :save, :validation - alias_method_chain :save!, :validation - end - + module ClassMethods def property(name, *args, &block) #:nodoc: property = super validates_uniqueness_of(name) if property.options[:unique] @@ -28,16 +23,16 @@ object end end end - # The validation process on save can be skipped by passing false. The regular Document#save method is + # The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Document#save method is # replaced with this when the validations module is mixed in, which it is by default. - def save_with_validation(perform_validation = true) - if perform_validation && valid? || !perform_validation + def save(options={}) + if perform_validation(options) begin - save_without_validation + super rescue DocumentNotSaved valid? false end else @@ -45,19 +40,25 @@ end end # Attempts to save the document just like Document#save but will raise a DocumentInvalid exception # instead of returning false if the document is not valid. - def save_with_validation! - if valid? + def save!(options={}) + if perform_validation(options) begin - save_without_validation! + super rescue DocumentNotSaved => e - raise valid? ? e : DocumentInvalid.new(self) + valid? ? raise : raise(DocumentInvalid.new(self)) end else raise DocumentInvalid.new(self) end + end + + protected + def perform_validation(options={}) + perform_validation = options != false && options[:validate] != false + perform_validation ? valid?(options[:context]) : true end end end end