Skip to content
Browse files

Added pre.remove middleware

  • Loading branch information...
1 parent 31f0b78 commit faf40f8bc684d06ad48c02318acdbfee0a534dc1 @luccastera luccastera committed Dec 30, 2011
View
BIN docs/test_tree_after_node_removal.dia
Binary file not shown.
View
BIN docs/test_tree_after_node_removal.dia~
Binary file not shown.
View
BIN docs/test_tree_after_node_removal.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
48 lib/nested_set.js
@@ -59,8 +59,52 @@ var NestedSetPlugin = function(schema, options) {
next();
}
});
-
-
+
+ schema.pre('remove', function(next) {
+ var self = this;
+ var model = mongoose.model(modelName);
+ if (self.parentId) {
+ self.parent(function(err, parentNode) {
+ if (!err && parentNode && parentNode.lft && parentNode.rgt) {
+
+ // find siblings and check if they have lft and rgt values set
+ self.siblings(function(err, nodes) {
+ if (nodes.every(function(node) { return node.lft && node.rgt;})) {
+ var maxRgt = 0;
+ nodes.forEach(function(node) {
+ if (node.rgt > maxRgt) {
+ maxRgt = node.rgt;
+ }
+ });
+ if (nodes.length === 0) {
+ // if it is a leaf node, the maxRgt should be the lft value of the parent
+ maxRgt = parentNode.lft;
+ }
+ model.update({lft: { $gt: maxRgt}}, {$inc: {lft: -2}}, {multi: true}, function(err, updatedCount) {
+ model.update({rgt: { $gt: maxRgt}}, {$inc: {rgt: -2}}, {multi: true}, function(err, updatedCount2) {
+ next();
+ });
+ });
+ } else {
+ // the siblings do not have lft and rgt set. This means tree was not build.
+ // warn on console and move on.
+// console.log('WARNING: tree is not built for ' + modelName + ' nodes. Siblings does not have lft/rgt');
+ next();
+ }
+ });
+ } else {
+ // parent node does not have lft and rgt set. This means tree was not built.
+ // warn on console and move on.
+// console.log('WARNING: tree is not built for ' + modelName + ' nodes. Parent does not have lft/rgt');
+ next();
+ }
+ });
+ } else {
+ // no parentId is set, so ignore
+ next();
+ }
+ });
+
// Builds the tree by populating lft and rgt using the parentIds
schema.static('rebuildTree', function(parent, left, callback) {
parent.lft = left;
View
48 tests/nested_set_test.js
@@ -374,7 +374,53 @@ var tests = testCase({
test.done();
});
},
- 'adding a new leaf node the a built tree should re-arrange the tree correctly': function(test) {
+ 'adding a new node to a built tree should re-arrange the tree correctly': function(test) {
+ test.expect(18)
+ User.findOne({username: 'michael'}, function(err, michael) {
+ User.rebuildTree(michael, 1, function() {
+ User.findOne({username: 'creed'}, function(err, creed) {
+ creed.remove(function() {
+ User.find(function(err, users) {
+ // see docs/test_tree_after_leaf_insertion.png for the graphical representation of this tree
+ // with lft/rgt values after the insertion
+ users.forEach(function(person) {
+ if (person.username === 'michael') {
+ test.equal(1, person.lft);
+ test.equal(18, person.rgt);
+ } else if (person.username === 'meredith') {
+ test.equal(2, person.lft);
+ test.equal(5, person.rgt);
+ } else if (person.username === 'jim') {
+ test.equal(6, person.lft);
+ test.equal(13, person.rgt);
+ } else if (person.username === 'angela') {
+ test.equal(14, person.lft);
+ test.equal(17, person.rgt);
+ } else if (person.username === 'kelly') {
+ test.equal(3, person.lft);
+ test.equal(4, person.rgt);
+ } else if (person.username === 'phyllis') {
+ test.equal(7, person.lft);
+ test.equal(8, person.rgt);
+ } else if (person.username === 'stanley') {
+ test.equal(9, person.lft);
+ test.equal(10, person.rgt);
+ } else if (person.username === 'dwight') {
+ test.equal(11, person.lft);
+ test.equal(12, person.rgt);
+ } else if (person.username === 'oscar') {
+ test.equal(15, person.lft);
+ test.equal(16, person.rgt);
+ }
+ });
+ test.done();
+ });
+ });
+ });
+ });
+ });
+ },
+ 'removing a node to a built tree should re-arrange the tree correctly': function(test) {
test.expect(22)
User.findOne({username: 'michael'}, function(err, michael) {
User.rebuildTree(michael, 1, function() {

0 comments on commit faf40f8

Please sign in to comment.
Something went wrong with that request. Please try again.