carrierwave-mongoid-media/app/models/carrierwave_mongoid_media.rb
2013-02-22 16:58:41 -05:00

41 lines
663 B
Ruby

require 'delegate'
class CarrierwaveMongoidMedia < SimpleDelegator
def self.find(path)
obj = Mongoid::GridFS[path]
raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :path => path) if !obj
new(obj)
end
def self.force_downloads_on(&block)
if block
@block = block
else
@block
end
end
def attachment_filename
File.basename(self.filename)
end
def initialize(gridfs_object)
@gridfs_object = gridfs_object
end
def __getobj__
@gridfs_object
end
def force_download?
if block = self.class.force_downloads_on
block.call(self.filename)
else
false
end
end
end