This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/asset.rb

41 lines
758 B
Ruby

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