make sure we also delete optimized images

This commit is contained in:
Régis Hanol 2013-06-21 09:34:02 +02:00
parent 3775a9c11d
commit 8a751e6e44
3 changed files with 15 additions and 3 deletions

View File

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

View File

@ -11,7 +11,7 @@ class Upload < ActiveRecord::Base
has_many :post_uploads has_many :post_uploads
has_many :posts, through: :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 :filesize
validates_presence_of :original_filename validates_presence_of :original_filename
@ -36,7 +36,7 @@ class Upload < ActiveRecord::Base
optimized_images << thumbnail if thumbnail optimized_images << thumbnail if thumbnail
end end
def delete def destroy
Upload.transaction do Upload.transaction do
Upload.remove_file url Upload.remove_file url
super 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") Upload.joins("LEFT OUTER JOIN post_uploads ON uploads.id = post_uploads.upload_id")
.where("post_uploads.upload_id IS NULL") .where("post_uploads.upload_id IS NULL")
.find_each do |u| .find_each do |u|
u.delete u.destroy
putc "." putc "."
end end
end end