Created
April 30, 2014 06:56
-
-
Save tdantas/9bab48899c9123dca0e5 to your computer and use it in GitHub Desktop.
mongoose test post middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Page = new Schema({ | |
title: { type: String, default: '', trim: true }, | |
url: { type: String, default: '', trim: true }, | |
thumbnails: {type: Schema.ObjectId, ref: 'Thumbnails'}, | |
}); | |
Page.post('save', function(doc) { | |
if(doc.thumbnails) return; | |
Thumbnail.generate(doc.url, function(err, thumb){ | |
doc.thumbnails = thumb; | |
doc.save() | |
}); | |
}); | |
var Thumbnail = new Schema({ | |
domain: {type: String, trim: true}, | |
url: { | |
small: { type: String, trim: true }, | |
original: {type: String, trim: true } | |
} | |
}); | |
Thumbnail.statics.generate = function(doc, cb) { | |
// generate site preview , save the thumbnail and call cb | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Page = mongoose.model('Page'); | |
var Thumbnail = mongoose.model('Thumbnail'); | |
// How to verify post middleware ? | |
it("generates thumbnail if page does not have one", function(done){ | |
var pageAttr = { title: 'Gist web page', url: 'http://www.gist.github.com' } | |
var gist = new Page(pageAttr), function(err, _page){ | |
if(err) throw err; | |
done(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment