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