Permalink
Browse files

no need modelName as an option since it can be inferred from this.con…

…structor.modelName
  • Loading branch information...
1 parent 335644e commit e788fef5a140c6fafb62c98f3b8e27307ce3b967 @luccastera luccastera committed Oct 25, 2012
Showing with 8 additions and 15 deletions.
  1. +8 −15 lib/nested_set.js
View
@@ -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} });
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit e788fef

Please sign in to comment.