'use strict';

var
  hookPlugin = require('mongoose-hook'),
  revPlugin = {};

// pre-hook
revPlugin.pre = function(p, callback) {
  switch (p.method) {
    case 'insert':
      if (!p.insert[this.revPathName])
        p.insert[this.revPathName] = 0;
      break;

    case 'findAndModify':
    case 'update':
      if (p.update) {
        if (!p.update.$inc) p.update.$inc = {};
        p.update.$inc[this.revPathName] = 1;
      }
      break;
  }

  callback();
};

// Mongoose plugin

// opts = {
//   mongoose: Mongoose,
//   path: String // optional; default: 'rev' 
// }
module.exports = function(schema, opts) {
  var
    hookOpts,
    schemaPaths = {};

  hookOpts = {
    name: 'mongoose-revision-all',
    mongoose: opts.mongoose,
    pre: revPlugin.pre,
    revPathName: opts.path || 'rev'
  };

  // disable built-in versioning
  schema.set('versionKey', false);

  // add revision path to schema
  schemaPaths[hookOpts.revPathName] = Number;
  schema.add(schemaPaths);

  // enable initial revisioning on model level
  schema.pre('save', function(next) {
    if (this.isNew)
      this[hookOpts.revPathName] = 0;

    next();
  });

  // register mongoose-hook'ing plugin
  schema.plugin(hookPlugin, hookOpts);
};

@@ -130,49 +130,24 @@
 %7B%0A  
-switch (p.method) %7B%0A    case '
+if (p.
 insert
-':%0A  
+) %7B%0A
     
@@ -185,18 +185,16 @@
 )%0A      
-  
 p.insert
@@ -223,69 +223,11 @@
 ;%0A  
-    break;%0A%0A    case 'findAndModify':%0A    case 'update':%0A    
+%7D%0A%0A
   if
@@ -240,20 +240,16 @@
 date) %7B%0A
-    
     if (
@@ -288,20 +288,16 @@
 %7B%7D;%0A    
-  
-  
 p.update
@@ -329,29 +329,8 @@
  1;%0A
-      %7D%0A      break;%0A
   %7D%0A

Update index.js