added #has_attachment? method
This commit is contained in:
parent
e497fbbab4
commit
0cf5fbe311
2 changed files with 35 additions and 2 deletions
|
@ -507,7 +507,7 @@ module CouchRest
|
|||
|
||||
# creates a file attachment to the current doc
|
||||
def create_attachment(file, attachment_name)
|
||||
return if self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?
|
||||
return if has_attachment?(attachment_name)
|
||||
self['_attachments'] ||= {}
|
||||
set_attachment_attr(file, attachment_name)
|
||||
end
|
||||
|
@ -519,7 +519,7 @@ module CouchRest
|
|||
|
||||
# modifies a file attachment on the current doc
|
||||
def update_attachment(file, attachment_name)
|
||||
return unless self['_attachments'] && self['_attachments'][attachment_name]
|
||||
return unless has_attachment?(attachment_name)
|
||||
delete_attachment(attachment_name)
|
||||
set_attachment_attr(file, attachment_name)
|
||||
end
|
||||
|
@ -529,6 +529,10 @@ module CouchRest
|
|||
return unless self['_attachments']
|
||||
self['_attachments'].delete attachment_name
|
||||
end
|
||||
|
||||
def has_attachment?(attachment_name)
|
||||
!!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
|
|
|
@ -728,6 +728,35 @@ describe CouchRest::Model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#has_attachment?" do
|
||||
before(:each) do
|
||||
@obj = Basic.new
|
||||
@obj.save.should == true
|
||||
@file = File.open(FIXTURE_PATH + '/attachments/test.html')
|
||||
@attachment_name = 'my_attachment'
|
||||
@obj.create_attachment(@file, @attachment_name)
|
||||
end
|
||||
|
||||
it 'should return false if there is no attachment' do
|
||||
@obj.has_attachment?('bogus').should be_false
|
||||
end
|
||||
|
||||
it 'should return true if there is an attachment' do
|
||||
@obj.has_attachment?(@attachment_name).should be_true
|
||||
end
|
||||
|
||||
it 'should return true if an object with an attachment is reloaded' do
|
||||
@obj.save.should be_true
|
||||
reloaded_obj = Basic.get(@obj.id)
|
||||
reloaded_obj.has_attachment?(@attachment_name).should be_true
|
||||
end
|
||||
|
||||
it 'should return false if an attachment has been removed' do
|
||||
@obj.delete_attachment(@attachment_name)
|
||||
@obj.has_attachment?(@attachment_name).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "creating an attachment" do
|
||||
before(:each) do
|
||||
@obj = Basic.new
|
||||
|
|
Loading…
Reference in a new issue