destroy static file after destroy asset

This commit is contained in:
Wen-Tien Chang 2010-02-05 16:18:07 +08:00
parent 0f7dce8aa6
commit 8f5c6d1a56
1 changed files with 10 additions and 1 deletions

View File

@ -10,11 +10,16 @@ class Asset
before_validation :setup_filename before_validation :setup_filename
after_save :save_static_file after_save :save_static_file
after_destroy :delete_static_file
def url def url
"/assets/#{self.filename}" "/assets/#{self.filename}"
end end
def location
"#{RAILS_ROOT}/public/assets/#{self.filename}"
end
protected protected
def setup_filename def setup_filename
@ -24,9 +29,13 @@ class Asset
end end
def save_static_file 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.write(self.data.file.read)
f.close f.close
end end
def delete_static_file
File.delete(self.location) rescue nil
end
end end