- Added Database#delete_attachment, for removing them directly
- Modified Database#fetch_attachment to take a doc as its first argument +as well as+ a docid, to be consistent with the other attachment methods. - Refactored the attachment uri generation used by #fetch_attachment, #put_attachment, and #delete_attachment to a common private method, #uri_for_attachment
This commit is contained in:
parent
9b3b56bbf5
commit
b915f7f708
2 changed files with 64 additions and 14 deletions
|
@ -87,25 +87,23 @@ module CouchRest
|
|||
end
|
||||
|
||||
# GET an attachment directly from CouchDB
|
||||
def fetch_attachment docid, name
|
||||
slug = escape_docid(docid)
|
||||
name = CGI.escape(name)
|
||||
RestClient.get "#{@root}/#{slug}/#{name}"
|
||||
def fetch_attachment doc, name
|
||||
docid = doc.respond_to?(:has_key?) ? doc['_id'] : doc
|
||||
RestClient.get uri_for_attachment({'_id' => docid}, name)
|
||||
end
|
||||
|
||||
# PUT an attachment directly to CouchDB
|
||||
def put_attachment doc, name, file, options = {}
|
||||
docid = escape_docid(doc['_id'])
|
||||
name = CGI.escape(name)
|
||||
uri = if doc['_rev']
|
||||
"#{@root}/#{docid}/#{name}?rev=#{doc['_rev']}"
|
||||
else
|
||||
"#{@root}/#{docid}/#{name}"
|
||||
end
|
||||
|
||||
uri = uri_for_attachment(doc, name)
|
||||
JSON.parse(RestClient.put(uri, file, options))
|
||||
end
|
||||
|
||||
# DELETE an attachment directly from CouchDB
|
||||
def delete_attachment doc, name
|
||||
uri = uri_for_attachment(doc, name)
|
||||
JSON.parse(RestClient.delete(uri))
|
||||
end
|
||||
|
||||
# Save a document to CouchDB. This will use the <tt>_id</tt> field from
|
||||
# the document as the id for PUT, or request a new UUID from CouchDB, if
|
||||
# no <tt>_id</tt> is present on the document. IDs are attached to
|
||||
|
@ -233,7 +231,14 @@ module CouchRest
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
def uri_for_attachment doc, name
|
||||
docid = escape_docid(doc['_id'])
|
||||
name = CGI.escape(name)
|
||||
rev = "?rev=#{doc['_rev']}" if doc['_rev']
|
||||
"#{@root}/#{docid}/#{name}#{rev}"
|
||||
end
|
||||
|
||||
def escape_docid id
|
||||
/^_design\/(.*)/ =~ id ? "_design/#{CGI.escape($1)}" : CGI.escape(id)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue