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:
commit
8dceb655db
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue