From c70b1d1b30fa05e1a888a2083655b6065f75914b Mon Sep 17 00:00:00 2001 From: VvanGemert Date: Thu, 5 Jan 2012 16:27:05 +0100 Subject: [PATCH] Added support for keeping a limited amount of documents in GridFS and removed the delete_old option. The delete_old removed all the old files while having a versioning system you might want to keep a limited set of files. This small little patch does that. You can specify a number of versions to keep by using the new versions option. --- lib/mongo/gridfs/grid_file_system.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 3addfaa..41a18ca 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -97,7 +97,7 @@ module Mongo def open(filename, mode, opts={}) opts = opts.dup opts.merge!(default_grid_io_opts(filename)) - del = opts.delete(:delete_old) && mode == 'w' + versions = opts.delete(:versions) && mode == 'w' file = GridIO.new(@files, @chunks, filename, mode, opts) return file unless block_given? result = nil @@ -105,9 +105,9 @@ module Mongo result = yield file ensure id = file.close - if del + if versions self.delete do - @files.find({'filename' => filename, '_id' => {'$ne' => id}}, :fields => ['_id']) + @files.find({'filename' => filename, '_id' => {'$ne' => id}}, :fields => ['_id'], :sort => ['uploadDate', -1], :skip => (versions -1)) end end end