class Asset include MongoMapper::Document include Grip::HasAttachment key :filename, String, :required => true key :description, String has_grid_attachment :data, :required => true 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 if self.filename.blank? && self.data self.filename = self.data.file_name end end def save_static_file 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