Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:couchrest/couchrest_model

Conflicts:
	lib/couchrest/model/callbacks.rb
  • Loading branch information...
commit 7875113d951cd81a500145b1492865d37e2aa675 2 parents 3579e0e + ca932df
@samlown samlown authored
View
1  lib/couchrest/model/base.rb
@@ -21,6 +21,7 @@ class Base < CouchRest::Document
include CouchRest::Model::Designs
include CouchRest::Model::CastedBy
include CouchRest::Model::Dirty
+ include CouchRest::Model::Callbacks
def self.subclasses
@subclasses ||= []
View
2  lib/couchrest/model/casted_model.rb
@@ -12,6 +12,8 @@ module CastedModel
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?
View
11 spec/fixtures/models/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|
View
8 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 '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 @@
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
View
21 spec/unit/persistence_spec.rb
@@ -356,6 +356,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
Please sign in to comment.
Something went wrong with that request. Please try again.