Permalink
Browse files

extract directory cleaning logic to AttachmentFu.cleanup_directory

  • Loading branch information...
1 parent 317c4e8 commit e90425d2b2a953432a787723163c1b9dfaa526d4 @technoweenie committed Mar 22, 2010
Showing with 16 additions and 13 deletions.
  1. +16 −13 lib/attachment_fu.rb
View
@@ -150,6 +150,21 @@ def self.reset
create_task :get_image_size, "attachment_fu/tasks/resize"
end
+ def self.cleanup_directory(root, file)
+ FileUtils.rm file if File.exist?(file)
+ dir_name = File.dirname(file)
+ default = %w(. ..)
+ while dir_name != root
+ dir_exists = File.exists?(dir_name)
+ if !dir_exists || (Dir.entries(dir_name) - default).empty?
+ FileUtils.rm_rf(dir_name) if dir_exists
+ dir_name.sub! /\/\w+$/, ''
+ else
+ dir_name = root
+ end
+ end
+ end
+
# This mixin is included in attachment classes by AttachmentFu::SetupMethods.is_attachment.
module InstanceMethods
# Strips filename of any funny characters.
@@ -342,20 +357,8 @@ def check_task_progress(stack)
# the empty asset paths.
def delete_attachment
fp = full_path
- fa = full_attachment_path
return if fp.blank?
- FileUtils.rm fp if File.exist?(fp)
- dir_name = File.dirname(fp)
- default = %w(. ..)
- while dir_name != fa
- dir_exists = File.exists?(dir_name)
- if !dir_exists || (Dir.entries(dir_name) - default).empty?
- FileUtils.rm_rf(dir_name) if dir_exists
- dir_name.sub! /\/\w+$/, ''
- else
- dir_name = fa
- end
- end
+ AttachmentFu.cleanup_directory(full_attachment_path, fp)
end
def rename_attachment

0 comments on commit e90425d

Please sign in to comment.