Permalink
Browse files

ClassMethods and InstanceMethods: converted @nil_attachments to Hash …

…so file id can be stored for deletion, eliminating the need for call to set() on after_save, allowing to properly work with EmbeddedDocument. Added nullify_nil_attachments_attributes() which is called before_save. Attachments are still removed from the db in destroy_nil_attachments after_save.
  • Loading branch information...
1 parent c2e7132 commit 18e3dcb1301c65c3aef19327992a7d0f905e4928 Jamie Orchard-Hays committed Nov 23, 2011
Showing with 13 additions and 14 deletions.
  1. +3 −2 lib/joint/class_methods.rb
  2. +10 −12 lib/joint/instance_methods.rb
@@ -11,6 +11,7 @@ def attachment(name, options = {})
self.attachment_names = attachment_names.dup.add(name)
after_save :save_attachments
+ before_save :nullify_nil_attachments_attributes
after_save :destroy_nil_attachments
before_destroy :destroy_all_attachments
@@ -27,12 +28,12 @@ def #{name}
end
def #{name}?
- !nil_attachments.include?(:#{name}) && send(:#{name}_id?)
+ !nil_attachments.has_key?(:#{name}) && send(:#{name}_id?)
end
def #{name}=(file)
if file.nil?
- nil_attachments << :#{name}
+ nil_attachments[:#{name}] = send("#{name}_id")
assigned_attachments.delete(:#{name})
else
send("#{name}_id=", BSON::ObjectId.new) if send("#{name}_id").nil?
@@ -10,7 +10,7 @@ def assigned_attachments
end
def nil_attachments
- @nil_attachments ||= Set.new
+ @nil_attachments ||= {}
end
# IO must respond to read and rewind
@@ -27,21 +27,19 @@ def save_attachments
end
assigned_attachments.clear
end
-
- def destroy_nil_attachments
- # currently MM does not send sets to instance as well
- nil_attachments.each do |name|
- grid.delete(send(name).id)
+
+ def nullify_nil_attachments_attributes
+ nil_attachments.each_key do |name|
send(:"#{name}_id=", nil)
send(:"#{name}_size=", nil)
send(:"#{name}_type=", nil)
send(:"#{name}_name=", nil)
- set({
- :"#{name}_id" => nil,
- :"#{name}_size" => nil,
- :"#{name}_type" => nil,
- :"#{name}_name" => nil,
- })
+ end
+ end
+
+ def destroy_nil_attachments
+ nil_attachments.each_value do |id|
+ grid.delete(id)
end
nil_attachments.clear

0 comments on commit 18e3dcb

Please sign in to comment.