From d9adc8029baee3eebec8ec809e13ae867132b8f0 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 09:58:35 +0200 Subject: Added accessscema for project access --- models/access.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 models/access.js (limited to 'models') diff --git a/models/access.js b/models/access.js new file mode 100644 index 0000000..486ef5d --- /dev/null +++ b/models/access.js @@ -0,0 +1,28 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , Schema = mongoose.Schema; + +var AccessSchema = new Schema({ + user: { type: String, ref: 'User' }, + creator: { type: String, ref: 'User' }, + project: { type: String, ref: 'Project' }, + permissions: { type: Number, default: '3' }, + created: { type: Date, default: Date.now }, + updated: { type: Date, default: Date.now } +}); + +// the four validations below only apply if you are signing up traditionally + +ProjectSchema.methods = { + + log: function() { + console.log('wat. wat logged this'); + } + +} + +mongoose.model('Access', AccessSchema); -- cgit v1.2.3 From f51fa5f8d2c29e0bccd13bf287f917aeb121ee5c Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 12:25:23 +0200 Subject: made a function for returning all accesses for a user including their projects --- models/access.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'models') diff --git a/models/access.js b/models/access.js index 486ef5d..ab8d257 100644 --- a/models/access.js +++ b/models/access.js @@ -7,9 +7,9 @@ var mongoose = require('mongoose') , Schema = mongoose.Schema; var AccessSchema = new Schema({ - user: { type: String, ref: 'User' }, - creator: { type: String, ref: 'User' }, - project: { type: String, ref: 'Project' }, + user: { type: Schema.ObjectId, ref: 'User' }, + creator: { type: Schema.ObjectId, ref: 'User' }, + project: { type: Schema.ObjectId, ref: 'Project' }, permissions: { type: Number, default: '3' }, created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now } @@ -17,12 +17,27 @@ var AccessSchema = new Schema({ // the four validations below only apply if you are signing up traditionally -ProjectSchema.methods = { +AccessSchema.statics = { log: function() { console.log('wat. wat logged this'); + }, + + /** + * Load ALL accesses for a single user + * + * @param {ObjectId} id + * @param {Function} callback + * @api private + */ + + loadUser: function(id, callback) { + this.find({ user: id }) + .populate('user') + .populate('project') + .sort({ 'created': -1 }) // sort by date + .exec(callback); } - } mongoose.model('Access', AccessSchema); -- cgit v1.2.3 From 334cdccec27afcb1a353df49f7edb77181048843 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 12:25:44 +0200 Subject: made a function for returning project by ID --- models/project.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'models') diff --git a/models/project.js b/models/project.js index 2424694..2641fe5 100644 --- a/models/project.js +++ b/models/project.js @@ -6,12 +6,8 @@ var mongoose = require('mongoose') , Schema = mongoose.Schema; - - - - var ProjectSchema = new Schema({ - user: { type: String, ref: 'User' }, + 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 }, @@ -20,7 +16,6 @@ var ProjectSchema = new Schema({ updated: { type: Date, default: Date.now } }); - // the four validations below only apply if you are signing up traditionally ProjectSchema.path('name').validate(function(name) { @@ -28,12 +23,29 @@ ProjectSchema.path('name').validate(function(name) { return name.length; }, 'Project name cannot be blank'); -ProjectSchema.methods = { +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); -- cgit v1.2.3 From c0630c6db730ba50ab247435bff14a36a4303e93 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 13:28:45 +0200 Subject: added shorturl for projects --- models/project.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'models') diff --git a/models/project.js b/models/project.js index 2641fe5..f29c7f8 100644 --- a/models/project.js +++ b/models/project.js @@ -13,7 +13,8 @@ var ProjectSchema = new Schema({ 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 } + updated: { type: Date, default: Date.now }, + shortURL: { type: String, unique: true } }); // the four validations below only apply if you are signing up traditionally @@ -23,6 +24,21 @@ ProjectSchema.path('name').validate(function(name) { 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() { -- cgit v1.2.3 From 698e59e4426448c2a0c55efb55f8cacb22ce3909 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 13:29:10 +0200 Subject: removed .populate('user') when we already know the user --- models/access.js | 1 - 1 file changed, 1 deletion(-) (limited to 'models') diff --git a/models/access.js b/models/access.js index ab8d257..ce75f8c 100644 --- a/models/access.js +++ b/models/access.js @@ -33,7 +33,6 @@ AccessSchema.statics = { loadUser: function(id, callback) { this.find({ user: id }) - .populate('user') .populate('project') .sort({ 'created': -1 }) // sort by date .exec(callback); -- cgit v1.2.3