lib/attached/attachment.rb in attached-0.2.4 vs lib/attached/attachment.rb in attached-0.2.5
- old
+ new
@@ -13,10 +13,11 @@
attr_reader :file
attr_reader :name
attr_reader :instance
attr_reader :queue
+ attr_reader :purge
attr_reader :errors
attr_reader :path
attr_reader :styles
attr_reader :default
attr_reader :medium
@@ -56,22 +57,27 @@
# * instance - The instance the attachment is attached to
#
# Options:
#
# * :path - The location where the attachment is stored
- # * :storage - The storage medium represented as a symbol such as ':s3'
- # * :credentials - A file, hash, or path used to authenticate with the specified storage medium
# * :styles - A hash containing optional parameters including extension and identifier
+ # * :credentials - A file, hash, or path used to authenticate with the specified storage medium
+ # * :medium - A symbol or subclass of 'Attached::Storage::Base' to be used
+ # * :processor - A symbol or subclass of 'Attached::Processor::Base' to be used
+ # * :alias - A string representing a fully qualified host alias
+ # * :processors - An array of processors
+ # * :aliases - An array of aliases
def initialize(name, instance, options = {})
options = self.class.options.merge(options)
@name = name
@instance = instance
- @queue = {}
- @errors = []
+ @queue = {}
+ @purge = []
+ @errors = []
@path = options[:path]
@styles = options[:styles]
@default = options[:default]
@medium = options[:medium]
@@ -109,16 +115,18 @@
#
# Usage:
#
# @object.avatar.assign(...)
- def assign(file, identifier = Guid.new)
+ def assign(file, identifier = "#{Guid.new}")
@file = file.respond_to?(:tempfile) ? file.tempfile : file
extension ||= File.extname(file.original_filename) if file.respond_to?(:original_filename)
extension ||= File.extname(file.path) if file.respond_to?(:path)
-
+
+ @purge = [self.path, *self.styles.map { |style, options| self.path(style) }] if file?
+
instance_set :size, file.size
instance_set :extension, extension
instance_set :identifier, identifier
process
@@ -130,14 +138,20 @@
# Usage:
#
# @object.avatar.save
def save
- @queue.each do |style, file|
- @storage.save(file, self.path(style)) if file and self.path(style)
+ self.purge.each do |path|
+ self.storage.destroy(path)
end
+ self.queue.each do |style, file|
+ path = self.path(style)
+ self.storage.save(file, path) if file and path
+ end
+
+ @purge = []
@queue = {}
end
# Destroy an attachment.
@@ -145,11 +159,19 @@
# Usage:
#
# @object.avatar.destroy
def destroy
- @storage.destroy(self.path) if self.path
+ if file?
+ self.storage.destroy(self.path)
+ self.styles.each do |style, options|
+ self.storage.destroy(self.path(style))
+ end
+ end
+
+ @purge = []
+ @queue = {}
end
# Acesss the URL for an attachment.
#
@@ -241,10 +263,11 @@
def extension=(extension)
instance_set(:extension, extension)
end
+
# Set the identifier for an attachment. It will act independently of the
# defined style.
#
# Usage:
#
@@ -260,9 +283,10 @@
def reprocess!
end
private
+
# Helper function for calling processors (will queue default).
#
# Usage:
#
\ No newline at end of file