Skip to content
Browse files

Remove empty directories when possible.

  • Loading branch information...
1 parent b8506d3 commit 9324ba89f3b0091be2796c6365457a248f889773 @jyurek jyurek committed Dec 29, 2008
Showing with 48 additions and 0 deletions.
  1. +8 −0 lib/paperclip/storage.rb
  2. +40 −0 test/integration_test.rb
View
8 lib/paperclip/storage.rb
@@ -56,6 +56,14 @@ def flush_deletes #:nodoc:
rescue Errno::ENOENT => e
# ignore file-not-found, let everything else pass
end
+ begin
+ while(true)
+ path = File.dirname(path)
+ FileUtils.rmdir(path)
+ end
+ rescue Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL
+ # Stop trying to remove parent directories
+ end
end
@queued_for_delete = []
end
View
40 test/integration_test.rb
@@ -49,6 +49,46 @@ class IntegrationTest < Test::Unit::TestCase
end
end
+ context "A model with attachments scoped under an id" do
+ setup do
+ rebuild_model :styles => { :large => "100x100",
+ :medium => "50x50" },
+ :path => ":rails_root/tmp/:id/:attachments/:style.:extension"
+ @dummy = Dummy.new
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"), 'rb')
+ @dummy.avatar = @file
+ end
+
+ context "when saved" do
+ setup do
+ @dummy.save
+ @saved_path = @dummy.avatar.path(:large)
+ end
+
+ should "have a large file in the right place" do
+ assert File.exists?(@dummy.avatar.path(:large))
+ end
+
+ context "and deleted" do
+ setup do
+ @dummy.avatar = nil
+ @dummy.save
+ end
+
+ should "not have a large file in the right place anymore" do
+ assert ! File.exists?(@saved_path)
+ end
+
+ should "not have its next two parent directories" do
+ assert ! File.exists?(File.dirname(@saved_path))
+ assert ! File.exists?(File.dirname(File.dirname(@saved_path)))
+ end
+ end
+ end
+ end
+
context "A model with no attachment validation" do
setup do
rebuild_model :styles => { :large => "300x300>",

0 comments on commit 9324ba8

Please sign in to comment.
Something went wrong with that request. Please try again.