diff options
author | Dennis Eriksen <dennis.se@gmail.com> | 2013-05-09 01:12:39 +0200 |
---|---|---|
committer | Dennis Eriksen <dennis.se@gmail.com> | 2013-05-09 01:12:39 +0200 |
commit | dd0867054a4a2c7800b72f0d21dec3ca327e958a (patch) | |
tree | 3fd2ce2929edcb9160b59d3f5547693e76676895 /models/pPost.js | |
parent | been working on the post-design (diff) | |
download | Divid-dd0867054a4a2c7800b72f0d21dec3ca327e958a.tar.gz |
added project post scheme
Diffstat (limited to 'models/pPost.js')
-rw-r--r-- | models/pPost.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/models/pPost.js b/models/pPost.js new file mode 100644 index 0000000..f29c7f8 --- /dev/null +++ b/models/pPost.js @@ -0,0 +1,67 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , Schema = mongoose.Schema; + +var ProjectSchema = new Schema({ + user: { type: Schema.ObjectId, ref: 'User' }, + name: { type: String, default: '', trim: true }, + description: {type: String, default: '', trim: true }, + currency: { type: String, default: 'kr', trim: true }, + public: { type: String, default: 'invite-only' }, + created: { type: Date, default: Date.now }, + updated: { type: Date, default: Date.now }, + shortURL: { type: String, unique: true } +}); + +// the four validations below only apply if you are signing up traditionally + +ProjectSchema.path('name').validate(function(name) { + // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate + return name.length; +}, 'Project name cannot be blank'); + + +ProjectSchema.pre('save', function(next) { + if (this.shortURL !== undefined) return next(); + var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + this.shortURL = ''; + for (var i = 0; i < 6; i++) { + var x = Math.floor(Math.random() * chars.length); + this.shortURL += chars.charAt(x); + } + console.log('SHORT: ' + this.shortURL); + next(); +}); + + + +ProjectSchema.statics = { + + log: function() { + console.log('wat. wat logged this'); + }, + + + + /** + * Find project by id + * + * @param {ObjectId} id + * @param {Function} callback + * @api private + */ + + load: function(id, callback) { + this.findOne({ _id: id }) + .populate('user') + .exec(callback); + } + + +} + +mongoose.model('Project', ProjectSchema); |