From 1b5c431053ce9f58851ffa81163b9b97025d0e65 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Wed, 8 Jun 2011 22:31:57 +0200 Subject: [PATCH] Should be able to set contextual validations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contextual validations are active __only__ within an appropriate context: either ”create" or ”update”. validates(:name, :presence => true, :on => :create) validates(:name, :presence => {:on => :update}) See http://edgeguides.rubyonrails.org/active_record_validations_callbacks.html#on Should fix https://github.com/couchrest/couchrest_model/pull/90 --- lib/couchrest/model/base.rb | 2 +- lib/couchrest/model/callbacks.rb | 6 ++++-- lib/couchrest/model/casted_model.rb | 3 ++- spec/couchrest/persistence_spec.rb | 21 +++++++++++++++++++++ spec/fixtures/base.rb | 11 +++++++++++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index 5f3a6cd0..508f5689 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -7,7 +7,6 @@ class Base < Document include CouchRest::Model::Configuration include CouchRest::Model::Connection include CouchRest::Model::Persistence - include CouchRest::Model::Callbacks include CouchRest::Model::DocumentQueries include CouchRest::Model::Views include CouchRest::Model::DesignDoc @@ -21,6 +20,7 @@ class Base < Document include CouchRest::Model::Designs include CouchRest::Model::CastedBy include CouchRest::Model::Dirty + include CouchRest::Model::Callbacks def self.subclasses @subclasses ||= [] diff --git a/lib/couchrest/model/callbacks.rb b/lib/couchrest/model/callbacks.rb index 30378104..dd6a67fa 100644 --- a/lib/couchrest/model/callbacks.rb +++ b/lib/couchrest/model/callbacks.rb @@ -16,8 +16,10 @@ module Callbacks end - def valid?(*) #nodoc - _run_validation_callbacks { super } + def valid?(context = nil) + context ||= (new_record? ? :create : :update) + output = super(context) + errors.empty? && output end end diff --git a/lib/couchrest/model/casted_model.rb b/lib/couchrest/model/casted_model.rb index f20de16a..e8c6a209 100644 --- a/lib/couchrest/model/casted_model.rb +++ b/lib/couchrest/model/casted_model.rb @@ -5,13 +5,14 @@ module CastedModel included do include CouchRest::Model::Configuration - include CouchRest::Model::Callbacks include CouchRest::Model::Properties include CouchRest::Model::PropertyProtection include CouchRest::Model::Associations include CouchRest::Model::Validations include CouchRest::Model::CastedBy include CouchRest::Model::Dirty + include CouchRest::Model::Callbacks + class_eval do # Override CastedBy's base_doc? def base_doc? diff --git a/spec/couchrest/persistence_spec.rb b/spec/couchrest/persistence_spec.rb index 8def9593..b9e10379 100644 --- a/spec/couchrest/persistence_spec.rb +++ b/spec/couchrest/persistence_spec.rb @@ -362,6 +362,27 @@ end end + describe "with contextual validation on ”create”" do + it "should validate only within ”create” context" do + doc = WithContextualValidationOnCreate.new + doc.save.should be_false + doc.name = "Alice" + doc.save.should be_true + + doc.update_attributes(:name => nil).should be_true + end + end + + describe "with contextual validation on ”update”" do + it "should validate only within ”update” context" do + doc = WithContextualValidationOnUpdate.new + doc.save.should be_true + + doc.update_attributes(:name => nil).should be_false + doc.update_attributes(:name => "Bob").should be_true + end + end + describe "save" do it "should run the after filter after saving" do @doc.run_after_save.should be_nil diff --git a/spec/fixtures/base.rb b/spec/fixtures/base.rb index d395b4a3..aaf6d395 100644 --- a/spec/fixtures/base.rb +++ b/spec/fixtures/base.rb @@ -83,6 +83,17 @@ def conditional_two_method end end +# Following two fixture classes have __intentionally__ diffent syntax for setting the validation context +class WithContextualValidationOnCreate < CouchRest::Model::Base + property(:name, String) + validates(:name, :presence => {:on => :create}) +end + +class WithContextualValidationOnUpdate < CouchRest::Model::Base + property(:name, String) + validates(:name, :presence => true, :on => :update) +end + class WithTemplateAndUniqueID < CouchRest::Model::Base use_database TEST_SERVER.default_database unique_id do |model|