changed parameter passing for model attachment methods to be more flexible

This commit is contained in:
Jonathan S. Katz 2008-10-30 00:41:15 -04:00 committed by Chris Anderson
parent 0cf5fbe311
commit a79d9b7f90
2 changed files with 46 additions and 17 deletions

View file

@ -506,10 +506,13 @@ module CouchRest
end end
# creates a file attachment to the current doc # creates a file attachment to the current doc
def create_attachment(file, attachment_name) def create_attachment(args={})
return if has_attachment?(attachment_name) raise ArgumentError unless args[:file] && args[:name]
return if has_attachment?(args[:name])
self['_attachments'] ||= {} self['_attachments'] ||= {}
set_attachment_attr(file, attachment_name) set_attachment_attr(args)
rescue ArgumentError => e
raise ArgumentError, 'You must specify :file and :name'
end end
# reads the data from an attachment # reads the data from an attachment
@ -518,10 +521,13 @@ module CouchRest
end end
# modifies a file attachment on the current doc # modifies a file attachment on the current doc
def update_attachment(file, attachment_name) def update_attachment(args={})
return unless has_attachment?(attachment_name) raise ArgumentError unless args[:file] && args[:name]
delete_attachment(attachment_name) return unless has_attachment?(args[:name])
set_attachment_attr(file, attachment_name) delete_attachment(args[:name])
set_attachment_attr(args)
rescue ArgumentError => e
raise ArgumentError, 'You must specify :file and :name'
end end
# deletes a file attachment from the current doc # deletes a file attachment from the current doc
@ -595,10 +601,11 @@ module CouchRest
'text\/plain' : MIME::Types.type_for(file.path).first.content_type.gsub(/\//,'\/') 'text\/plain' : MIME::Types.type_for(file.path).first.content_type.gsub(/\//,'\/')
end end
def set_attachment_attr(file, attachment_name) def set_attachment_attr(args)
self['_attachments'][attachment_name] = { content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file])
'content-type' => get_mime_type(file), self['_attachments'][args[:name]] = {
'data' => encode_attachment(file.read) 'content-type' => content_type,
'data' => encode_attachment(args[:file].read)
} }
end end

View file

@ -734,7 +734,7 @@ describe CouchRest::Model do
@obj.save.should == true @obj.save.should == true
@file = File.open(FIXTURE_PATH + '/attachments/test.html') @file = File.open(FIXTURE_PATH + '/attachments/test.html')
@attachment_name = 'my_attachment' @attachment_name = 'my_attachment'
@obj.create_attachment(@file, @attachment_name) @obj.create_attachment(:file => @file, :name => @attachment_name)
end end
it 'should return false if there is no attachment' do it 'should return false if there is no attachment' do
@ -762,23 +762,37 @@ describe CouchRest::Model do
@obj = Basic.new @obj = Basic.new
@obj.save.should == true @obj.save.should == true
@file_ext = File.open(FIXTURE_PATH + '/attachments/test.html') @file_ext = File.open(FIXTURE_PATH + '/attachments/test.html')
@file_no_text = File.open(FIXTURE_PATH + '/attachments/README') @file_no_ext = File.open(FIXTURE_PATH + '/attachments/README')
@attachment_name = 'my_attachment' @attachment_name = 'my_attachment'
@content_type = 'media/mp3'
end end
it "should create an attachment from file with an extension" do it "should create an attachment from file with an extension" do
@obj.create_attachment(@file_ext, @attachment_name) @obj.create_attachment(:file => @file_ext, :name => @attachment_name)
@obj.save.should == true @obj.save.should == 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_no_text, @attachment_name) @obj.create_attachment(:file => @file_no_ext, :name => @attachment_name)
@obj.save.should == true @obj.save.should == 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
lambda{ @obj.create_attachment(:name => @attachment_name) }.should raise_error
end
it 'should raise ArgumentError if :name is missing' do
lambda{ @obj.create_attachment(:file => @file_ext) }.should raise_error
end
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
end
end end
describe 'reading, updating, and deleting an attachment' do describe 'reading, updating, and deleting an attachment' do
@ -786,9 +800,10 @@ describe CouchRest::Model do
@obj = Basic.new @obj = Basic.new
@file = File.open(FIXTURE_PATH + '/attachments/test.html') @file = File.open(FIXTURE_PATH + '/attachments/test.html')
@attachment_name = 'my_attachment' @attachment_name = 'my_attachment'
@obj.create_attachment(@file, @attachment_name) @obj.create_attachment(:file => @file, :name => @attachment_name)
@obj.save.should == true @obj.save.should == true
@file.rewind @file.rewind
@content_type = 'media/mp3'
end end
it 'should read an attachment that exists' do it 'should read an attachment that exists' do
@ -798,7 +813,7 @@ describe CouchRest::Model do
it 'should update an attachment that exists' do it 'should update an attachment that exists' 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, @attachment_name) @obj.update_attachment(:file => file, :name => @attachment_name)
@obj.save @obj.save
reloaded_obj = Basic.get(@obj.id) reloaded_obj = Basic.get(@obj.id)
file.rewind file.rewind
@ -806,6 +821,13 @@ describe CouchRest::Model do
reloaded_obj.read_attachment(@attachment_name).should == file.read reloaded_obj.read_attachment(@attachment_name).should == file.read
end end
it 'should se the content-type if passed' 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
end
it 'should delete an attachment that exists' do it 'should delete an attachment that exists' do
@obj.delete_attachment(@attachment_name) @obj.delete_attachment(@attachment_name)
@obj.save @obj.save