Browse files

Making changes to accept empty values for parent.

  • Loading branch information...
1 parent 811f288 commit 4f1daacd54685996ea1155d101a1a27a8e39acb8 @mariuskubilius committed Jan 7, 2013
Showing with 5 additions and 3 deletions.
  1. +4 −2 lib/ancestorTree.js
  2. +1 −1 test/ancestorTree.test.js
View
6 lib/ancestorTree.js
@@ -11,12 +11,14 @@ ancestorTree = function(schema){
var fields = {};
if (!schema.paths.parent) {
fields.parent = {
- type: ObjectId,
+ type: ObjectId,
set : function(val) {
if(typeof(val) === "object" && val._id) {
return val._id;
}
+ if(val === '') { return undefined}
return val;
+
},
index: true
}
@@ -35,7 +37,7 @@ ancestorTree = function(schema){
schema.pre('save', function (next) {
var parentModified = this.isDirectModified('parent');
var self = this;
-
+
if (self.isNew || parentModified) {
if(!self.parent && self.isNew){
return next();
View
2 test/ancestorTree.test.js
@@ -14,7 +14,7 @@ describe('ancestorTree', function () {
describe('#default()', function() {
MockSchema.plugin(ancestorTree);
var MockModel = db.model('ancestorMock', MockSchema);
- var parent = new MockModel({});
+ var parent = new MockModel({parent:''});
var child = new MockModel({parent: parent._id});
var subChild = new MockModel({parent: child._id});

0 comments on commit 4f1daac

Please sign in to comment.