
/*
 * @list dependencies
 */

var async = require('async');

/*
 * @method paginate
 * @param {Object} query Mongoose Query Object
 * @param {Number} pageNumber
 * @param {Number} resultsPerPage
 * Extend Mongoose Models to paginate queries
 */

function paginate(q, pageNumber, resultsPerPage, callback, options) {
  var model = this;
  options = options || {};
  columns = options.columns || null;
  sortBy = options.sortBy || {
    _id : 1
  };
  populate = options.populate || null;
  callback = callback || function() {};
  skipFrom = (pageNumber * resultsPerPage) - resultsPerPage;
  query = model.find(q);
  if (columns !== null) {
    query = query.select(options.columns);
  }
  query = query.skip(skipFrom).limit(resultsPerPage).sort(sortBy);
  if (populate) {
    query = query.populate(populate);
  }
  async.parallel({
    results: function(callback) {
      query.exec(callback);
    },
    count: function(callback) {
      model.count(q, function(err, count) {
        callback(err, count);
      })
    }
  }, function(error, data) {
    if (error) {
      return callback(error);
    }
    callback(null, Math.ceil(data.count / resultsPerPage) || 1, data.results, data.count);
  });
}

module.exports = function(schema) {
  schema.statics.paginate = paginate;
}

@@ -315,16 +315,60 @@
  %7B%0A  var
+ query, skipFrom, sortBy, columns, populate,
  model =

format shit