Skip to content

Commit

Permalink
Merge branch 'master' of github.com:couchrest/couchrest_model
Browse files Browse the repository at this point in the history
Conflicts:
	lib/couchrest/model/callbacks.rb
  • Loading branch information
samlown committed Jun 8, 2011
2 parents 3579e0e + ca932df commit 7875113
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/couchrest/model/base.rb
Expand Up @@ -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 ||= []
Expand Down
2 changes: 2 additions & 0 deletions lib/couchrest/model/casted_model.rb
Expand Up @@ -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?
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/models/base.rb
Expand Up @@ -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|
Expand Down
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
@@ -1,16 +1,16 @@
$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"

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')

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/persistence_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 7875113

Please sign in to comment.