Improved auto_update_design_doc handling.

This commit is contained in:
Peter Williams 2011-12-01 09:19:15 -07:00
parent 88bb413ec2
commit 447b11a397
2 changed files with 22 additions and 10 deletions

View file

@ -10,7 +10,7 @@ module CouchRest
@design_doc ||= if auto_update_design_doc @design_doc ||= if auto_update_design_doc
::CouchRest::Design.new(default_design_doc) ::CouchRest::Design.new(default_design_doc)
else else
stored_design_doc stored_design_doc || ::CouchRest::Design.new(default_design_doc)
end end
end end
@ -79,16 +79,25 @@ module CouchRest
# If auto updates enabled, check checksum cache # If auto updates enabled, check checksum cache
return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum
retries = 1
begin
# Load up the stored doc (if present), update, and save # Load up the stored doc (if present), update, and save
saved = stored_design_doc(db) saved = stored_design_doc(db)
if saved if saved
if force || saved['couchrest-hash'] != checksum if force || saved['couchrest-hash'] != checksum
saved.merge!(design_doc) saved.merge!(design_doc)
db.save_doc(saved) db.save_doc(saved)
@design_doc = saved # update memo to point to the document we actually saved
end end
else else
design_doc.delete('_rev') # This is a new document and so doesn't have a revision yet
db.save_doc(design_doc) db.save_doc(design_doc)
design_doc.delete('_rev') # Prevent conflicts, never store rev as DB specific end
rescue RestClient::Conflict
# if we get a conflict retry the operation...
raise if retries < 1
retries -= 1
retry
end end
# Ensure checksum cached for next attempt if using auto updates # Ensure checksum cached for next attempt if using auto updates

View file

@ -72,9 +72,12 @@ module CouchRest
# <tt>spec/couchrest/more/extended_doc_spec.rb</tt>. # <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
def view_by(*keys) def view_by(*keys)
return unless auto_update_design_doc
opts = keys.pop if keys.last.is_a?(Hash) opts = keys.pop if keys.last.is_a?(Hash)
opts ||= {} opts ||= {}
ducktype = opts.delete(:ducktype) ducktype = opts.delete(:ducktype)
unless ducktype || opts[:map] unless ducktype || opts[:map]
opts[:guards] ||= [] opts[:guards] ||= []
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')" opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"