Merge pull request #90 from kostia/validates_on

Should be able to set contextual validations
This commit is contained in:
Sam Lown 2011-06-08 16:06:04 -07:00
commit ca932df5ba
5 changed files with 39 additions and 4 deletions

View file

@ -7,7 +7,6 @@ module CouchRest
include CouchRest::Model::Configuration include CouchRest::Model::Configuration
include CouchRest::Model::Connection include CouchRest::Model::Connection
include CouchRest::Model::Persistence include CouchRest::Model::Persistence
include CouchRest::Model::Callbacks
include CouchRest::Model::DocumentQueries include CouchRest::Model::DocumentQueries
include CouchRest::Model::Views include CouchRest::Model::Views
include CouchRest::Model::DesignDoc include CouchRest::Model::DesignDoc
@ -21,6 +20,7 @@ module CouchRest
include CouchRest::Model::Designs include CouchRest::Model::Designs
include CouchRest::Model::CastedBy include CouchRest::Model::CastedBy
include CouchRest::Model::Dirty include CouchRest::Model::Dirty
include CouchRest::Model::Callbacks
def self.subclasses def self.subclasses
@subclasses ||= [] @subclasses ||= []

View file

@ -16,8 +16,10 @@ module CouchRest #:nodoc:
end end
def valid?(*) #nodoc def valid?(context = nil)
_run_validation_callbacks { super } context ||= (new_record? ? :create : :update)
output = super(context)
errors.empty? && output
end end
end end

View file

@ -5,13 +5,14 @@ module CouchRest::Model
included do included do
include CouchRest::Model::Configuration include CouchRest::Model::Configuration
include CouchRest::Model::Callbacks
include CouchRest::Model::Properties include CouchRest::Model::Properties
include CouchRest::Model::PropertyProtection include CouchRest::Model::PropertyProtection
include CouchRest::Model::Associations include CouchRest::Model::Associations
include CouchRest::Model::Validations include CouchRest::Model::Validations
include CouchRest::Model::CastedBy include CouchRest::Model::CastedBy
include CouchRest::Model::Dirty include CouchRest::Model::Dirty
include CouchRest::Model::Callbacks
class_eval do class_eval do
# Override CastedBy's base_doc? # Override CastedBy's base_doc?
def base_doc? def base_doc?

View file

@ -362,6 +362,27 @@ describe "Model Persistence" do
end end
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 describe "save" do
it "should run the after filter after saving" do it "should run the after filter after saving" do
@doc.run_after_save.should be_nil @doc.run_after_save.should be_nil

11
spec/fixtures/base.rb vendored
View file

@ -83,6 +83,17 @@ class WithCallBacks < CouchRest::Model::Base
end end
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 class WithTemplateAndUniqueID < CouchRest::Model::Base
use_database TEST_SERVER.default_database use_database TEST_SERVER.default_database
unique_id do |model| unique_id do |model|