diff options
author | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-30 13:28:45 +0200 |
---|---|---|
committer | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-30 13:28:45 +0200 |
commit | c0630c6db730ba50ab247435bff14a36a4303e93 (patch) | |
tree | b046ea65ae857d0893da73f2ac8a14b29cc05ce5 | |
parent | made the dashboard work somewhat better (diff) | |
download | Divid-c0630c6db730ba50ab247435bff14a36a4303e93.tar.gz |
added shorturl for projects
-rw-r--r-- | models/project.js | 18 | ||||
-rw-r--r-- | views/dashboard.ejs | 2 |
2 files changed, 18 insertions, 2 deletions
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() { diff --git a/views/dashboard.ejs b/views/dashboard.ejs index 66dfa80..dfd76f6 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -16,7 +16,7 @@ <section class="project"> <div class="row-fluid"> <div class="span11"> - <h1><a href="/project"><%= projects.project.user.username %>/<strong><%= projects.project.name %></strong></a></h1> + <h1><a href="/project/<%= projects.project.shortURL %>"><%= projects.project.user.username %>/<strong><%= projects.project.name %></strong></a></h1> <div class="row-fluid"> <div class="span8"> <small>Meg, Robert, Turid-Laila, Kurt, Stian, Aleksander, Frank, Olav, Heidi, Anette, Kristine</small> |