creating attachments accessor
This commit is contained in:
parent
08541c2d1a
commit
822d0bad18
|
@ -7,11 +7,15 @@ module CouchRest
|
||||||
def create_attachment(args={})
|
def create_attachment(args={})
|
||||||
raise ArgumentError unless args[:file] && args[:name]
|
raise ArgumentError unless args[:file] && args[:name]
|
||||||
return if has_attachment?(args[:name])
|
return if has_attachment?(args[:name])
|
||||||
self['_attachments'] ||= {}
|
|
||||||
set_attachment_attr(args)
|
set_attachment_attr(args)
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
raise ArgumentError, 'You must specify :file and :name'
|
raise ArgumentError, 'You must specify :file and :name'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# return all attachments
|
||||||
|
def attachments
|
||||||
|
self['_attachments'] ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
# reads the data from an attachment
|
# reads the data from an attachment
|
||||||
def read_attachment(attachment_name)
|
def read_attachment(attachment_name)
|
||||||
|
@ -30,13 +34,13 @@ module CouchRest
|
||||||
|
|
||||||
# deletes a file attachment from the current doc
|
# deletes a file attachment from the current doc
|
||||||
def delete_attachment(attachment_name)
|
def delete_attachment(attachment_name)
|
||||||
return unless self['_attachments']
|
return unless attachments
|
||||||
self['_attachments'].delete attachment_name
|
attachments.delete attachment_name
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns true if attachment_name exists
|
# returns true if attachment_name exists
|
||||||
def has_attachment?(attachment_name)
|
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
|
end
|
||||||
|
|
||||||
# returns URL to fetch the attachment from
|
# returns URL to fetch the attachment from
|
||||||
|
@ -62,7 +66,7 @@ module CouchRest
|
||||||
def set_attachment_attr(args)
|
def set_attachment_attr(args)
|
||||||
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file].path)
|
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file].path)
|
||||||
content_type ||= (get_mime_type(args[:name]) || 'text/plain')
|
content_type ||= (get_mime_type(args[:name]) || 'text/plain')
|
||||||
self['_attachments'][args[:name]] = {
|
attachments[args[:name]] = {
|
||||||
'content_type' => content_type,
|
'content_type' => content_type,
|
||||||
'data' => args[:file].read
|
'data' => args[:file].read
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,14 +53,14 @@ describe "Model attachments" do
|
||||||
@obj.create_attachment(:file => @file_ext, :name => @attachment_name)
|
@obj.create_attachment(:file => @file_ext, :name => @attachment_name)
|
||||||
@obj.save.should be_true
|
@obj.save.should be_true
|
||||||
reloaded_obj = Basic.get(@obj.id)
|
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
|
end
|
||||||
|
|
||||||
it "should create an attachment from file without an extension" do
|
it "should create an attachment from file without an extension" do
|
||||||
@obj.create_attachment(:file => @file_no_ext, :name => @attachment_name)
|
@obj.create_attachment(:file => @file_no_ext, :name => @attachment_name)
|
||||||
@obj.save.should be_true
|
@obj.save.should be_true
|
||||||
reloaded_obj = Basic.get(@obj.id)
|
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
|
end
|
||||||
|
|
||||||
it 'should raise ArgumentError if :file is missing' do
|
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
|
it 'should set the content-type if passed' do
|
||||||
@obj.create_attachment(:file => @file_ext, :name => @attachment_name, :content_type => @content_type)
|
@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
|
end
|
||||||
|
|
||||||
it "should detect the content-type automatically" do
|
it "should detect the content-type automatically" do
|
||||||
@obj.create_attachment(:file => File.open(FIXTURE_PATH + '/attachments/couchdb.png'), :name => "couchdb.png")
|
@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
|
||||||
|
|
||||||
it "should use name to detect the content-type automatically if no file" do
|
it "should use name to detect the content-type automatically if no file" do
|
||||||
file = File.open(FIXTURE_PATH + '/attachments/couchdb.png')
|
file = File.open(FIXTURE_PATH + '/attachments/couchdb.png')
|
||||||
file.stub!(:path).and_return("badfilname")
|
file.stub!(:path).and_return("badfilname")
|
||||||
@obj.create_attachment(:file => File.open(FIXTURE_PATH + '/attachments/couchdb.png'), :name => "couchdb.png")
|
@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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -120,7 +120,7 @@ describe "Model attachments" do
|
||||||
file = File.open(FIXTURE_PATH + '/attachments/README')
|
file = File.open(FIXTURE_PATH + '/attachments/README')
|
||||||
@file.should_not == file
|
@file.should_not == file
|
||||||
@obj.update_attachment(:file => file, :name => @attachment_name, :content_type => @content_type)
|
@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
|
end
|
||||||
|
|
||||||
it 'should delete an attachment that exists' do
|
it 'should delete an attachment that exists' do
|
||||||
|
@ -150,6 +150,27 @@ describe "Model attachments" do
|
||||||
it 'should return the attachment URI' do
|
it 'should return the attachment URI' do
|
||||||
@obj.attachment_uri(@attachment_name).should == "#{Basic.database.uri}/#{@obj.id}/#{@attachment_name}"
|
@obj.attachment_uri(@attachment_name).should == "#{Basic.database.uri}/#{@obj.id}/#{@attachment_name}"
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue