Commit 87bd86cc by Jon Yurek

Resolved mvanholstyn's open file fixes with current code

parent f79dadce
...@@ -16,7 +16,7 @@ module Paperclip ...@@ -16,7 +16,7 @@ module Paperclip
# Returns representation of the data of the file assigned to the given # Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage. # style, in the format most representative of the current storage.
def to_file style = default_style def to_file style = default_style
File.new(path(style)) if exists?(style) @queued_for_write[style] || (File.new(path(style)) if exists?(style))
end end
alias_method :to_io, :to_file alias_method :to_io, :to_file
...@@ -35,7 +35,7 @@ module Paperclip ...@@ -35,7 +35,7 @@ module Paperclip
begin begin
FileUtils.rm(path) if File.exist?(path) FileUtils.rm(path) if File.exist?(path)
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
# ignore them # ignore file-not-found, let everything else pass
end end
end end
@queued_for_delete = [] @queued_for_delete = []
...@@ -74,7 +74,7 @@ module Paperclip ...@@ -74,7 +74,7 @@ module Paperclip
# Returns representation of the data of the file assigned to the given # Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage. # style, in the format most representative of the current storage.
def to_file style = default_style def to_file style = default_style
@s3_bucket.key(path(style)) @queued_for_write[style] || @s3_bucket.key(path(style))
end end
alias_method :to_io, :to_file alias_method :to_io, :to_file
......
def obtain_class def obtain_class
class_name = ENV['CLASS'] || ENV['class'] class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
@klass = Object.const_get(class_name) @klass = Object.const_get(class_name)
end end
def obtain_attachments def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment'] name = ENV['ATTACHMENT'] || ENV['attachment']
raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_names)
if !name.blank? && @klass.attachment_names.include?(name) if !name.blank? && @klass.attachment_names.include?(name)
[ name ] [ name ]
else else
...@@ -16,11 +18,10 @@ namespace :paperclip do ...@@ -16,11 +18,10 @@ namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)" desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
task :refresh => :environment do task :refresh => :environment do
klass = obtain_class klass = obtain_class
instances = klass.find(:all)
names = obtain_attachments names = obtain_attachments
puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:" puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
instances.each do |instance| klass.find(:all).each do |instance|
names.each do |name| names.each do |name|
result = if instance.send("#{ name }?") result = if instance.send("#{ name }?")
instance.send(name).send("post_process") instance.send(name).send("post_process")
......
...@@ -195,23 +195,6 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -195,23 +195,6 @@ class AttachmentTest < Test::Unit::TestCase
@existing_names.each{|f| assert ! File.exists?(f) } @existing_names.each{|f| assert ! File.exists?(f) }
end end
end end
context "and deleted" do
setup do
@existing_names = @attachment.styles.keys.collect do |style|
@attachment.path(style)
end
@instance.expects(:[]=).with(:test_file_name, nil)
@instance.expects(:[]=).with(:test_content_type, nil)
@instance.expects(:[]=).with(:test_file_size, nil)
@attachment.assign nil
@attachment.save
end
should "delete the files" do
@existing_names.each{|f| assert ! File.exists?(f) }
end
end
end end
end end
end end
......
...@@ -223,7 +223,7 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -223,7 +223,7 @@ class IntegrationTest < Test::Unit::TestCase
expected = @dummy.avatar.to_file expected = @dummy.avatar.to_file
@dummy.avatar = "not a file" @dummy.avatar = "not a file"
assert @dummy.valid? assert @dummy.valid?
assert_equal expected, @dummy.avatar.to_file assert_equal expected.full_name, @dummy.avatar.to_file.full_name
@dummy.avatar = @bad_file @dummy.avatar = @bad_file
assert ! @dummy.valid? assert ! @dummy.valid?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment