Skip to content

Commit

Permalink
no need modelName as an option since it can be inferred from this.con…
Browse files Browse the repository at this point in the history
…structor.modelName
  • Loading branch information
luccastera committed Oct 25, 2012
1 parent 335644e commit e788fef
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions lib/nested_set.js
Expand Up @@ -9,8 +9,6 @@ var mongoose = require('mongoose'),
var NestedSetPlugin = function(schema, options) {
options = options || {};

var modelName = options.modelName || 'User';

schema.add({ lft: {type: Number, min: 0} });
schema.add({ rgt: {type: Number, min: 0} });
schema.add({ parentId: {type: Schema.ObjectId} });
Expand All @@ -21,7 +19,7 @@ var NestedSetPlugin = function(schema, options) {

schema.pre('save', function(next) {
var self = this;
var model = mongoose.model(modelName);
var model = mongoose.model(this.constructor.modelName);
if (self.parentId) {
self.parent(function(err, parentNode) {
if (!err && parentNode && parentNode.lft && parentNode.rgt) {
Expand Down Expand Up @@ -68,7 +66,7 @@ var NestedSetPlugin = function(schema, options) {

schema.pre('remove', function(next) {
var self = this;
var model = mongoose.model(modelName);
var model = mongoose.model(this.constructor.modelName);
if (self.parentId) {
self.parent(function(err, parentNode) {
if (!err && parentNode && parentNode.lft && parentNode.rgt) {
Expand Down Expand Up @@ -119,7 +117,7 @@ var NestedSetPlugin = function(schema, options) {

self.find({parentId: parent._id}, function(err, children) {
if (err) return callback(err);
if (!children) return callback(new Error(modelName + ' not found'));
if (!children) return callback(new Error(self.constructor.modelName + ' not found'));

if (children.length > 0) {
async.forEachSeries(children, function(item, cb) {
Expand Down Expand Up @@ -161,15 +159,13 @@ var NestedSetPlugin = function(schema, options) {
// returns the parent node
schema.method('parent', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.findOne({_id: self.parentId}, callback);
mongoose.model(self.constructor.modelName).findOne({_id: self.parentId}, callback);
});

// Returns the list of ancestors + current node
schema.method('selfAndAncestors', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.where('lft').lte(self.lft).where('rgt').gte(self.rgt).run(callback);
mongoose.model(self.constructor.modelName).where('lft').lte(self.lft).where('rgt').gte(self.rgt).run(callback);
});

// Returns the list of ancestors
Expand All @@ -188,8 +184,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of children
schema.method('children', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.find({parentId: self._id}, callback);
mongoose.model(self.constructor.modelName).find({parentId: self._id}, callback);
});

// Returns the list of children + current node
Expand All @@ -207,8 +202,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of descendants + current node
schema.method('selfAndDescendants', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.where('lft').gte(self.lft).where('rgt').lte(self.rgt).run(callback);
mongoose.model(self.constructor.modelName).where('lft').gte(self.lft).where('rgt').lte(self.rgt).run(callback);
});

// Returns the list of descendants
Expand All @@ -227,8 +221,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of all nodes with the same parent + current node
schema.method('selfAndSiblings', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.find({parentId: self.parentId}, callback);
mongoose.model(self.constructor.modelName).find({parentId: self.parentId}, callback);
});

// Returns the list of all nodes with the same parent
Expand Down

0 comments on commit e788fef

Please sign in to comment.