orbit-basic/app/models/asset.rb

41 lines
758 B
Ruby
Raw Normal View History

2010-02-01 09:24:28 +00:00
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
2010-02-05 08:15:16 +00:00
after_save :save_static_file
after_destroy :delete_static_file
2010-02-05 08:15:16 +00:00
def url
"/assets/#{self.filename}"
end
2010-02-01 09:24:28 +00:00
def location
"#{RAILS_ROOT}/public/assets/#{self.filename}"
end
2010-02-01 09:24:28 +00:00
protected
def setup_filename
if self.filename.blank? && self.data
self.filename = self.data.file_name
end
end
2010-02-05 08:15:16 +00:00
def save_static_file
f = File.new(self.location, "wb")
2010-02-05 08:15:16 +00:00
f.write(self.data.file.read)
f.close
end
def delete_static_file
File.delete(self.location) rescue nil
end
2010-02-01 09:24:28 +00:00
end