From 8d25cbc8b0d598bde978f5e1d0b0374f6948ef3e Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Mon, 12 Jan 2009 20:49:57 -0800 Subject: [PATCH] merge mime types in model --- couchrest.gemspec | 1 + lib/couchrest/core/model.rb | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/couchrest.gemspec b/couchrest.gemspec index 016d2ae..7143668 100644 --- a/couchrest.gemspec +++ b/couchrest.gemspec @@ -129,5 +129,6 @@ Gem::Specification.new do |s| s.add_dependency "json", [">= 1.1.2"] s.add_dependency "rest-client", [">= 0.5"] s.add_dependency "extlib", [">= 0.9.6"] + s.add_dependency "mime-types", [">= 1.15"] s.require_paths = ["lib"] end \ No newline at end of file diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 1a0d8d4..6aaca0c 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -1,7 +1,12 @@ require 'rubygems' require 'extlib' require 'digest/md5' +<<<<<<< HEAD:lib/couchrest/core/model.rb require File.dirname(__FILE__) + '/document' +======= +require 'mime/types' + +>>>>>>> CRUD for attachments in CouchRest::Model instance:lib/couchrest/core/model.rb # = CouchRest::Model - ORM, the CouchDB way module CouchRest # = CouchRest::Model - ORM, the CouchDB way @@ -500,6 +505,45 @@ module CouchRest result['ok'] end + # 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? + self['_attachments'] ||= {} + set_attachment_attr(file, attachment_name) + end + + # reads the data from an attachment + def read_attachment(attachment_name) + Base64.decode64(database.fetch_attachment(self.id, attachment_name)) + end + + # modifies a file attachment on the current doc + def update_attachment(file, attachment_name) + return unless self['_attachments'] && self['_attachments'][attachment_name] + delete_attachment(attachment_name) + set_attachment_attr(file, attachment_name) + end + + # deletes a file attachment from the current doc + def delete_attachment(attachment_name) + return unless self['_attachments'] + self['_attachments'].delete attachment_name + end + + protected + + # Saves a document for the first time, after running the before(:create) + # callbacks, and applying the unique_id. + def create + set_unique_id if respond_to?(:set_unique_id) # hack + save_doc + end + + # Saves the document and runs the :update callbacks. + def update + save_doc + end + private def apply_defaults @@ -538,6 +582,22 @@ module CouchRest end end + def encode_attachment(data) + Base64.encode64(data).gsub(/\r|\n/,'') + end + + def get_mime_type(file) + MIME::Types.type_for(file.path).empty? ? + 'text\/plain' : MIME::Types.type_for(file.path).content_type.gsub(/\//,'\/') + end + + def set_attachment_attr(file, attachment_name) + self['_attachments'][attachment_name] = { + 'content-type' => get_mime_type(file), + 'data' => encode_attachment(file.read) + } + end + include ::Extlib::Hook register_instance_hooks :save, :destroy