lib/bulldog/attachment/maybe.rb in bulldog-0.0.5 vs lib/bulldog/attachment/maybe.rb in bulldog-0.0.6
- old
+ new
@@ -64,29 +64,62 @@
def type
self.class.attachment_type
end
#
- # Set the stored attributes in the record.
+ # Load the attachment.
#
- def set_stored_attributes
+ # Called just after initializing the attachment, or after a
+ # reload on the new attachment.
+ #
+ def load
storable_attributes.each do |name, storable_attribute|
if (column_name = reflection.column_name_for_stored_attribute(name))
value = storable_attribute.value_for(self, :original)
record.send("#{column_name}=", value)
end
end
end
#
- # Clear the stored attributes in the record.
+ # Unload the attachment.
#
- def clear_stored_attributes
- storable_attributes.each do |name, callback|
+ # Called before a reload on the old attachment, or before a
+ # destroy.
+ #
+ def unload
+ storable_attributes.each do |name, storable_attribute|
if (column_name = reflection.column_name_for_stored_attribute(name))
record.send("#{column_name}=", nil)
end
+ if storable_attribute.memoize?
+ send("memoized_#{name}").clear
+ end
end
+ end
+
+ #
+ # Refresh the attachment. This includes anything read from the
+ # file, such as stored attributes, and dimensions.
+ #
+ # Use this to update the record if the underlying file has been
+ # modified outside of Bulldog. Note that the file has to be
+ # initialized from a saved file for this to work.
+ #
+ # Example:
+ #
+ # article = Article.create(:photo => photo_file)
+ # article = Article.find(article.id)
+ #
+ # # ... file is modified by an outside force ...
+ #
+ # article.photo.reload
+ # article.photo.dimensions # now reflects the new dimensions
+ #
+ def reload
+ unload
+ stream.reload
+ load
end
#
# Set the stored attributes in the attachment from the values in
# the record.