Skip to content

Instantly share code, notes, and snippets.

@tdantas
Created April 30, 2014 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tdantas/9bab48899c9123dca0e5 to your computer and use it in GitHub Desktop.
Save tdantas/9bab48899c9123dca0e5 to your computer and use it in GitHub Desktop.
mongoose test post middleware
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
}
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