diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index f9bcec9..8ae8cac 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -21,6 +21,7 @@ module CouchRest 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/casted_model.rb b/lib/couchrest/model/casted_model.rb index aa27e0a..0074e18 100644 --- a/lib/couchrest/model/casted_model.rb +++ b/lib/couchrest/model/casted_model.rb @@ -12,6 +12,8 @@ module CouchRest::Model include CouchRest::Model::Callbacks 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/fixtures/models/base.rb b/spec/fixtures/models/base.rb index 65fabfa..b64df11 100644 --- a/spec/fixtures/models/base.rb +++ b/spec/fixtures/models/base.rb @@ -83,6 +83,17 @@ class WithCallBacks < CouchRest::Model::Base 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| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 284d0d1..d04b99f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,6 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) -MODELS = File.join(File.dirname(__FILE__), "fixtures", "models") -$LOAD_PATH.unshift(MODELS) - require "bundler/setup" require "rubygems" require "rspec" @@ -11,6 +8,9 @@ require "rspec" require 'couchrest_model' unless defined?(FIXTURE_PATH) + MODEL_PATH = File.join(File.dirname(__FILE__), "fixtures", "models") + $LOAD_PATH.unshift(MODEL_PATH) + FIXTURE_PATH = File.join(File.dirname(__FILE__), '/fixtures') SCRATCH_PATH = File.join(File.dirname(__FILE__), '/tmp') @@ -34,7 +34,7 @@ RSpec.configure do |config| end # Require each of the fixture models -Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) } +Dir[ File.join(MODEL_PATH, "*.rb") ].sort.each { |file| require File.basename(file) } class Basic < CouchRest::Model::Base use_database TEST_SERVER.default_database diff --git a/spec/unit/persistence_spec.rb b/spec/unit/persistence_spec.rb index 3a8a042..e8f78ac 100644 --- a/spec/unit/persistence_spec.rb +++ b/spec/unit/persistence_spec.rb @@ -356,6 +356,27 @@ describe CouchRest::Model::Persistence do 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