Orbit/app/models/asset.rb

32 lines
604 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
def url
"/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("#{RAILS_ROOT}/public/assets/#{self.filename}", "wb")
f.write(self.data.file.read)
f.close
end
2010-02-01 09:24:28 +00:00
end