creating attachments accessor

This commit is contained in:
Marcos Tapajós 2010-08-03 23:57:51 -03:00
parent 0d72447e31
commit 33657d9470
2 changed files with 37 additions and 12 deletions

View file

@ -7,12 +7,16 @@ module CouchRest
def create_attachment(args={})
raise ArgumentError unless args[:file] && args[:name]
return if has_attachment?(args[:name])
self['_attachments'] ||= {}
set_attachment_attr(args)
rescue ArgumentError => e
raise ArgumentError, 'You must specify :file and :name'
end
# return all attachments
def attachments
self['_attachments'] ||= {}
end
# reads the data from an attachment
def read_attachment(attachment_name)
database.fetch_attachment(self, attachment_name)
@ -30,13 +34,13 @@ module CouchRest
# deletes a file attachment from the current doc
def delete_attachment(attachment_name)
return unless self['_attachments']
self['_attachments'].delete attachment_name
return unless attachments
attachments.delete attachment_name
end
# returns true if attachment_name exists
def has_attachment?(attachment_name)
!!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
!!(attachments && attachments[attachment_name] && !attachments[attachment_name].empty?)
end
# returns URL to fetch the attachment from
@ -62,7 +66,7 @@ module CouchRest
def set_attachment_attr(args)
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file].path)
content_type ||= (get_mime_type(args[:name]) || 'text/plain')
self['_attachments'][args[:name]] = {
attachments[args[:name]] = {
'content_type' => content_type,
'data' => args[:file].read
}

View file

@ -53,14 +53,14 @@ describe "Model attachments" do
@obj.create_attachment(:file => @file_ext, :name => @attachment_name)
@obj.save.should be_true
reloaded_obj = Basic.get(@obj.id)
reloaded_obj['_attachments'][@attachment_name].should_not be_nil
reloaded_obj.attachments[@attachment_name].should_not be_nil
end
it "should create an attachment from file without an extension" do
@obj.create_attachment(:file => @file_no_ext, :name => @attachment_name)
@obj.save.should be_true
reloaded_obj = Basic.get(@obj.id)
reloaded_obj['_attachments'][@attachment_name].should_not be_nil
reloaded_obj.attachments[@attachment_name].should_not be_nil
end
it 'should raise ArgumentError if :file is missing' do
@ -73,19 +73,19 @@ describe "Model attachments" do
it 'should set the content-type if passed' do
@obj.create_attachment(:file => @file_ext, :name => @attachment_name, :content_type => @content_type)
@obj['_attachments'][@attachment_name]['content_type'].should == @content_type
@obj.attachments[@attachment_name]['content_type'].should == @content_type
end
it "should detect the content-type automatically" do
@obj.create_attachment(:file => File.open(FIXTURE_PATH + '/attachments/couchdb.png'), :name => "couchdb.png")
@obj['_attachments']['couchdb.png']['content_type'].should == "image/png"
@obj.attachments['couchdb.png']['content_type'].should == "image/png"
end
it "should use name to detect the content-type automatically if no file" do
file = File.open(FIXTURE_PATH + '/attachments/couchdb.png')
file.stub!(:path).and_return("badfilname")
@obj.create_attachment(:file => File.open(FIXTURE_PATH + '/attachments/couchdb.png'), :name => "couchdb.png")
@obj['_attachments']['couchdb.png']['content_type'].should == "image/png"
@obj.attachments['couchdb.png']['content_type'].should == "image/png"
end
end
@ -120,7 +120,7 @@ describe "Model attachments" do
file = File.open(FIXTURE_PATH + '/attachments/README')
@file.should_not == file
@obj.update_attachment(:file => file, :name => @attachment_name, :content_type => @content_type)
@obj['_attachments'][@attachment_name]['content_type'].should == @content_type
@obj.attachments[@attachment_name]['content_type'].should == @content_type
end
it 'should delete an attachment that exists' do
@ -150,6 +150,27 @@ describe "Model attachments" do
it 'should return the attachment URI' do
@obj.attachment_uri(@attachment_name).should == "#{Basic.database.uri}/#{@obj.id}/#{@attachment_name}"
end
end
describe "#attachments" do
before(:each) do
@obj = Basic.new
@file = File.open(FIXTURE_PATH + '/attachments/test.html')
@attachment_name = 'my_attachment'
@obj.create_attachment(:file => @file, :name => @attachment_name)
@obj.save.should be_true
end
it 'should return an empty Hash when document does not have any attachment' do
new_obj = Basic.new
new_obj.save.should be_true
new_obj.attachments.should == {}
end
it 'should return a Hash with all attachments' do
@file.rewind
@obj.attachments.should == { @attachment_name =>{ "data" => "PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPHRpdGxlPlRlc3Q8L3RpdGxlPgogIDwvaGVhZD4KICA8Ym9keT4KICAgIDxwPgogICAgICBUZXN0CiAgICA8L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==", "content_type" => "text/html"}}
end
end
end