Method: CouchClient::Document#attach

Defined in:
lib/couch-client/document.rb

#attach(name, content, content_type) ⇒ Object

Tries to attach a file to the document. If it us unable to, it will save the error and make it available via #error.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/couch-client/document.rb', line 82

def attach(name, content, content_type)
  # The document must already be saved to the server before a file can be attached.
  if self.rev
    @code, body = @connection.hookup.put([self.id, name], {"rev" => self.rev}, content, content_type)
    
    # If the document was saved
    if body["ok"]
      # Update rev and return `true`
      self.rev = body["rev"]
      true
    else
      # Else save error message and return `false`.
      @error = {body["error"] => body["reason"]}
      false
    end
  else
    # Raise an error if the document is new before trying to attach a file.
    raise AttachmentError.new("a document must exist before an attachment can be uploaded to it")
  end
end