added CouchRest::Model#attachment_url for someone to fetch the attachment using other means
This commit is contained in:
parent
a79d9b7f90
commit
3bff23e7c5
|
@ -536,10 +536,17 @@ module CouchRest
|
||||||
self['_attachments'].delete attachment_name
|
self['_attachments'].delete attachment_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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?)
|
!!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns URL to fetch the attachment from
|
||||||
|
def attachment_url(attachment_name)
|
||||||
|
return unless has_attachment?(attachment_name)
|
||||||
|
"#{database.root}/#{self.id}/#{attachment_name}"
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Saves a document for the first time, after running the before(:create)
|
# Saves a document for the first time, after running the before(:create)
|
||||||
|
|
|
@ -834,4 +834,22 @@ describe CouchRest::Model do
|
||||||
lambda{Basic.get(@obj.id).read_attachment(@attachment_name)}.should raise_error
|
lambda{Basic.get(@obj.id).read_attachment(@attachment_name)}.should raise_error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#attachment_url" 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 == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return nil if attachment does not exist' do
|
||||||
|
@obj.attachment_url('bogus').should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return the attachment URL as specified by CouchDB HttpDocumentApi' do
|
||||||
|
@obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue