lib/attached/attachment.rb in attached-0.3.5 vs lib/attached/attachment.rb in attached-0.3.6

- old
+ new

@@ -1,5 +1,7 @@ +require 'open-uri' + require 'guid' require 'attached/storage' require 'attached/storage/error' @@ -109,32 +111,42 @@ # Check if an attachment is present. # # Usage: # - # @object.avatar.file? + # @object.avatar.attached? - def file? + def attached? not identifier.blank? end + # Custom setter for specifying an attachment. + # + # Usage: + # + # @object.avatar.file = File.open(...) + + def file=(file) + @file = file + @file = file.tempfile if file.respond_to?(:tempfile) + end + + # Assign an attachment to a file. # # Usage: # # @object.avatar.assign(...) def assign(file, identifier = "#{Guid.new}") - file = file.file if file.is_a?(Attached::Attachment) + self.file = file - @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? + @purge = [self.path, *self.styles.map { |style, options| self.path(style) }] if attached? instance_set :size, file.size instance_set :extension, extension instance_set :identifier, identifier @@ -168,11 +180,11 @@ # Usage: # # @object.avatar.destroy def destroy - if file? + if attached? self.storage.destroy(self.path) self.styles.each do |style, options| self.storage.destroy(self.path(style)) end end @@ -285,12 +297,19 @@ def identifier=(identifier) instance_set(:identifier, identifier) end - # Todo. + # Access the original file . def reprocess! + uri = URI.parse(self.url) + root = "#{Rails.root}/public" + + self.file ||= File.open("#{root}#{self.url}") unless uri.absolute? + self.file ||= open(uri) + + process end private \ No newline at end of file