lib/couchrest/model/extended_attachments.rb in couchrest_model-1.1.0.beta2 vs lib/couchrest/model/extended_attachments.rb in couchrest_model-1.1.0.beta3
- old
+ new
@@ -1,8 +1,9 @@
module CouchRest
module Model
module ExtendedAttachments
+ extend ActiveSupport::Concern
# Add a file attachment to the current document. Expects
# :file and :name to be included in the arguments.
def create_attachment(args={})
raise ArgumentError unless args[:file] && args[:name]
@@ -33,11 +34,14 @@
end
# deletes a file attachment from the current doc
def delete_attachment(attachment_name)
return unless attachments
- attachments.delete attachment_name
+ if attachments.include?(attachment_name)
+ attribute_will_change!("_attachments")
+ attachments.delete attachment_name
+ end
end
# returns true if attachment_name exists
def has_attachment?(attachment_name)
!!(attachments && attachments[attachment_name] && !attachments[attachment_name].empty?)
@@ -64,9 +68,11 @@
end
def set_attachment_attr(args)
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file].path)
content_type ||= (get_mime_type(args[:name]) || 'text/plain')
+
+ attribute_will_change!("_attachments")
attachments[args[:name]] = {
'content_type' => content_type,
'data' => args[:file].read
}
end