Merge pull request #1058 from ZogStriP/delete-optimized-images-when-cleaning-up-orphans

make sure we also delete optimized images
This commit is contained in:
Robin Ward 2013-06-21 07:10:28 -07:00
commit 8dceb655db
3 changed files with 15 additions and 3 deletions

View File

@ -36,6 +36,18 @@ class OptimizedImage < ActiveRecord::Base
thumbnail
end
def destroy
OptimizedImage.transaction do
remove_file
super
end
end
def remove_file
File.delete path
rescue Errno::ENOENT
end
def url
"#{Upload.base_url}/#{optimized_path}/#{filename}"
end

View File

@ -11,7 +11,7 @@ class Upload < ActiveRecord::Base
has_many :post_uploads
has_many :posts, through: :post_uploads
has_many :optimized_images
has_many :optimized_images, dependent: :destroy
validates_presence_of :filesize
validates_presence_of :original_filename
@ -36,7 +36,7 @@ class Upload < ActiveRecord::Base
optimized_images << thumbnail if thumbnail
end
def delete
def destroy
Upload.transaction do
Upload.remove_file url
super

View File

@ -43,7 +43,7 @@ task "images:clean_orphans" => :environment do
Upload.joins("LEFT OUTER JOIN post_uploads ON uploads.id = post_uploads.upload_id")
.where("post_uploads.upload_id IS NULL")
.find_each do |u|
u.delete
u.destroy
putc "."
end
end