- 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
|
@ -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
|
||||
|
|
|
@ -230,7 +230,31 @@ describe CouchRest::Database do
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
describe "fetch_attachment" do
|
||||
before do
|
||||
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
||||
@doc = {
|
||||
"_id" => "mydocwithattachment",
|
||||
"field" => ["some value"],
|
||||
"_attachments" => {
|
||||
"test.html" => {
|
||||
"type" => "text/html",
|
||||
"data" => @attach
|
||||
}
|
||||
}
|
||||
}
|
||||
@db.save(@doc)
|
||||
end
|
||||
|
||||
it "should get the attachment with the doc's _id" do
|
||||
@db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
|
||||
end
|
||||
it "should get the attachment with the doc itself" do
|
||||
@db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT attachment from file" do
|
||||
before(:each) do
|
||||
filename = FIXTURE_PATH + '/attachments/couchdb.png'
|
||||
|
@ -330,6 +354,27 @@ describe CouchRest::Database do
|
|||
attachment.should == @attach2
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE an attachment directly from the database" do
|
||||
before(:each) do
|
||||
doc = {
|
||||
'_id' => 'mydocwithattachment',
|
||||
'_attachments' => {
|
||||
'test.html' => {
|
||||
'type' => 'text/html',
|
||||
'data' => "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
||||
}
|
||||
}
|
||||
}
|
||||
@db.save(doc)
|
||||
@doc = @db.get('mydocwithattachment')
|
||||
end
|
||||
it "should delete the attachment" do
|
||||
lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should_not raise_error
|
||||
@db.delete_attachment(@doc, "test.html")
|
||||
lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should raise_error(RestClient::ResourceNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST document with attachment (with funky name)" do
|
||||
before(:each) do
|
||||
|
|
Loading…
Reference in a new issue