From 8f5c6d1a56e28888497403679c639243a6d674c3 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 5 Feb 2010 16:18:07 +0800 Subject: [PATCH] destroy static file after destroy asset --- app/models/asset.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/asset.rb b/app/models/asset.rb index 09d6dbd4..ca2608fe 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -10,11 +10,16 @@ class Asset before_validation :setup_filename after_save :save_static_file + after_destroy :delete_static_file def url "/assets/#{self.filename}" end + def location + "#{RAILS_ROOT}/public/assets/#{self.filename}" + end + protected def setup_filename @@ -24,9 +29,13 @@ class Asset end def save_static_file - f = File.new("#{RAILS_ROOT}/public/assets/#{self.filename}", "wb") + f = File.new(self.location, "wb") f.write(self.data.file.read) f.close end + def delete_static_file + File.delete(self.location) rescue nil + end + end \ No newline at end of file