From a4a2b202ae79072acdabd58e48a056de95553a7e Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Mon, 2 Feb 2009 00:35:37 -0800 Subject: [PATCH] Added attachment methods to CocuhRest::Document: #put_attachment, #fetch_attachment and #delete_attachment. Note you can overwrite exisitng attachments with #put_attachment. --- lib/couchrest/core/document.rb | 23 +++++++++- spec/couchrest/core/document_spec.rb | 65 ++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/core/document.rb b/lib/couchrest/core/document.rb index dcd352a..7c4e29b 100644 --- a/lib/couchrest/core/document.rb +++ b/lib/couchrest/core/document.rb @@ -74,7 +74,28 @@ module CouchRest result = database.move(self, dest) result['ok'] end - + + # saves an attachment directly to couchdb + def put_attachment(name, file, options={}) + raise ArgumentError, "doc.database required to put_attachment" unless database + result = database.put_attachment(self, name, file, options) + self['_rev'] = result['rev'] + result['ok'] + end + + # returns an attachment's data + def fetch_attachment(name) + raise ArgumentError, "doc.database required to put_attachment" unless database + database.fetch_attachment(self, name) + end + + # deletes an attachment directly from couchdb + def delete_attachment(name) + raise ArgumentError, "doc.database required to delete_attachment" unless database + result = database.delete_attachment(self, name) + self['_rev'] = result['rev'] + result['ok'] + end end diff --git a/spec/couchrest/core/document_spec.rb b/spec/couchrest/core/document_spec.rb index a714cd6..301bd47 100644 --- a/spec/couchrest/core/document_spec.rb +++ b/spec/couchrest/core/document_spec.rb @@ -211,3 +211,68 @@ describe "MOVE existing document" do end end end + +describe "dealing with attachments" do + before do + @db = reset_test_db! + @attach = "My Doc

Has words.

" + response = @db.save({'key' => 'value'}) + @doc = @db.get(response['id']) + end + + def append_attachment(name='test.html', attach=@attach) + @doc['_attachments'] ||= {} + @doc['_attachments'][name] = { + 'type' => 'text/html', + 'data' => attach + } + @doc.save + @rev = @doc['_rev'] + end + + describe "PUTing an attachment directly to the doc" do + before do + @doc.put_attachment('test.html', @attach) + end + + it "is there" do + @db.fetch_attachment(@doc, 'test.html').should == @attach + end + + it "updates the revision" do + @doc['_rev'].should_not == @rev + end + + it "updates attachments" do + @attach2 = "My Doc

Is Different.

" + @doc.put_attachment('test.html', @attach2) + @db.fetch_attachment(@doc, 'test.html').should == @attach2 + end + end + + describe "fetching an attachment from a doc directly" do + before do + append_attachment + end + + it "pulls the attachment" do + @doc.fetch_attachment('test.html').should == @attach + end + end + + describe "deleting an attachment from a doc directly" do + before do + append_attachment + @doc.delete_attachment('test.html') + end + + it "removes it" do + lambda { @db.fetch_attachment(@doc, 'test.html').should }.should raise_error(RestClient::ResourceNotFound) + end + + it "updates the revision" do + @doc['_rev'].should_not == @rev + end + end + +end \ No newline at end of file