diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb
index 463eed5..8ce8569 100644
--- a/lib/couchrest/core/model.rb
+++ b/lib/couchrest/core/model.rb
@@ -93,6 +93,7 @@ module CouchRest
class_inheritable_accessor :generated_design_doc
class_inheritable_accessor :design_doc_slug_cache
class_inheritable_accessor :design_doc_fresh
+ class_inheritable_accessor :validation_fields
class << self
# override the CouchRest::Model-wide default_database
@@ -171,6 +172,10 @@ module CouchRest
self.default_obj = hash
end
+ def validate *fields
+ self.validation_fields = fields.map {|f| f.to_s}
+ end
+
# Automatically set updated_at and created_at fields
# on the document whenever saving occurs. CouchRest uses a pretty
# decent time format by default. See Time#to_json
@@ -444,12 +449,25 @@ module CouchRest
!rev
end
+ def valid?
+ self.invalid_fields.blank?
+ end
+
+ def invalid_fields
+ return [] unless self.validation_fields
+ check = self.validation_fields.clone
+ self.validation_fields.each do |c|
+ check.delete_if {|x| x == c } if !self[c].blank?
+ end
+ return check
+ end
+
# Saves the document to the db using create or update. Also runs the :save
# callbacks. Sets the _id and _rev fields based on
# CouchDB's response.
def save
if new_record?
- create
+ if self.valid? then create else return false end
else
update
end