From 3227cd093472cc33d23eb5e4cbf5c3f3b4878739 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 13 Jul 2017 13:14:04 +0200 Subject: [PATCH] Add character names to the end of their URL's Closes #21, makes links to characters human readable --- rpg-docs/.meteor/packages | 1 + rpg-docs/.meteor/versions | 1 + rpg-docs/Model/Character/Characters.js | 7 ++++++ rpg-docs/Routes/Routes.js | 24 +++++++++++++++---- .../views/characterList/characterList.html | 2 +- .../views/characterList/characterList.js | 10 +++++++- .../views/characterList/characterSideList.js | 2 +- rpg-docs/server/migrations/migrations.js | 20 ++++++++++++++-- rpg-docs/server/publications/characterList.js | 1 + 9 files changed, 59 insertions(+), 9 deletions(-) diff --git a/rpg-docs/.meteor/packages b/rpg-docs/.meteor/packages index 0928a7e4..89495d83 100644 --- a/rpg-docs/.meteor/packages +++ b/rpg-docs/.meteor/packages @@ -48,3 +48,4 @@ es5-shim@4.6.15 differential:vulcanize reactive-dict percolate:synced-cron +ongoworks:speakingurl diff --git a/rpg-docs/.meteor/versions b/rpg-docs/.meteor/versions index 04f53178..c2254df9 100644 --- a/rpg-docs/.meteor/versions +++ b/rpg-docs/.meteor/versions @@ -85,6 +85,7 @@ npm-mongo@2.2.16_1 oauth@1.1.12 oauth2@1.1.11 observe-sequence@1.0.14 +ongoworks:speakingurl@9.0.0 ordered-dict@1.0.9 percolate:migrations@0.9.8 percolate:synced-cron@1.3.2 diff --git a/rpg-docs/Model/Character/Characters.js b/rpg-docs/Model/Character/Characters.js index 88fbc911..b16e3324 100644 --- a/rpg-docs/Model/Character/Characters.js +++ b/rpg-docs/Model/Character/Characters.js @@ -4,6 +4,7 @@ Characters = new Mongo.Collection("characters"); Schemas.Character = new SimpleSchema({ //strings name: {type: String, defaultValue: "", trim: false, optional: true}, + urlName: {type: String, defaultValue: "", trim: false, optional: true}, alignment: {type: String, defaultValue: "", trim: false, optional: true}, gender: {type: String, defaultValue: "", trim: false, optional: true}, race: {type: String, defaultValue: "", trim: false, optional: true}, @@ -540,6 +541,12 @@ if (Meteor.isServer){ Items .remove({charId: character._id}); Containers .remove({charId: character._id}); }); + Characters.after.update(function(userId, doc, fieldNames, modifier, options) { + if (_.contains(fieldNames, "name")){ + var urlName = getSlug(doc.name, {maintainCase: true}); + Characters.update(doc._id, {$set: {urlName}}); + } + }); } Characters.allow({ diff --git a/rpg-docs/Routes/Routes.js b/rpg-docs/Routes/Routes.js index 5f2906c2..14ac42ff 100644 --- a/rpg-docs/Routes/Routes.js +++ b/rpg-docs/Routes/Routes.js @@ -24,7 +24,7 @@ Router.map(function() { this.route("characterList", { path: "/characterList", waitOn: function(){ - return subsManager.subscribe("characterList", Meteor.userId()); + return subsManager.subscribe("characterList"); }, onAfterAction: function() { document.title = appName + " - Characters"; @@ -32,11 +32,27 @@ Router.map(function() { fastRender: true, }); - this.route("characterSheet", { - path: "/character/:_id", + this.route("characterSheetNaked", { + path: "/character/:_id/", waitOn: function(){ return [ - subsManager.subscribe("singleCharacter", this.params._id, Meteor.userId()), + subsManager.subscribe("singleCharacter", this.params._id), + ]; + }, + action: function(){ + var _id = this.params._id + var character = Characters.findOne(_id); + var urlName = character && character.urlName; + var path = `\/character\/${_id}\/${urlName}`; + Router.go(path,{},{replaceState:true}); + }, + }); + + this.route("characterSheet", { + path: "/character/:_id/:urlName", + waitOn: function(){ + return [ + subsManager.subscribe("singleCharacter", this.params._id), ]; }, data: function() { diff --git a/rpg-docs/client/views/characterList/characterList.html b/rpg-docs/client/views/characterList/characterList.html index 1603eaa5..82e12b92 100644 --- a/rpg-docs/client/views/characterList/characterList.html +++ b/rpg-docs/client/views/characterList/characterList.html @@ -11,7 +11,7 @@ {{#if characters.count}}
{{# each characters}} - + diff --git a/rpg-docs/client/views/characterList/characterList.js b/rpg-docs/client/views/characterList/characterList.js index c5971a03..254b2854 100644 --- a/rpg-docs/client/views/characterList/characterList.js +++ b/rpg-docs/client/views/characterList/characterList.js @@ -10,7 +10,15 @@ Template.characterList.helpers({ ] }, { - fields: {name: 1, picture: 1, color: 1, race: 1, alignment: 1, gender: 1}, + fields: { + name: 1, + urlName: 1, + picture: 1, + color: 1, + race: 1, + alignment: 1, + gender: 1, + }, sort: {name: 1}, } ); diff --git a/rpg-docs/client/views/characterList/characterSideList.js b/rpg-docs/client/views/characterList/characterSideList.js index 915bf87a..010905c2 100644 --- a/rpg-docs/client/views/characterList/characterSideList.js +++ b/rpg-docs/client/views/characterList/characterSideList.js @@ -14,7 +14,7 @@ Template.characterSideList.helpers({ ] }, { - fields: {name: 1}, + fields: {name: 1, urlName: 1}, sort: {name: 1}, } ); diff --git a/rpg-docs/server/migrations/migrations.js b/rpg-docs/server/migrations/migrations.js index 9eee2347..17ca3cf3 100644 --- a/rpg-docs/server/migrations/migrations.js +++ b/rpg-docs/server/migrations/migrations.js @@ -125,7 +125,7 @@ Migrations.add({ } }); var effect = Effects.findOne({ - charId: char._id, name: "Natural Carrying Capacity" + charId: char._id, name: "Natural Carrying Capacity", }); if (effect) return; Effects.insert({ @@ -141,7 +141,7 @@ Migrations.add({ }, }); effect = Effects.findOne({ - charId: char._id, name: "Natural Carrying Capacity" + charId: char._id, name: "Natural Carrying Capacity", }); if (!effect) throw "Carry capacity effect should be set by now." }); @@ -150,3 +150,19 @@ Migrations.add({ return; }, }); + +Migrations.add({ + version: 5, + name: "Gives all characters a URL name", + up: function() { + //update characters + Characters.find({}).forEach(function(char){ + if (char.urlName) return; + var urlName = getSlug(char.name, {maintainCase: true}); + Characters.update(char._id, {$set: {urlName}}); + }); + }, + down: function(){ + return; + }, +}); diff --git a/rpg-docs/server/publications/characterList.js b/rpg-docs/server/publications/characterList.js index b8c24b83..91e8e019 100644 --- a/rpg-docs/server/publications/characterList.js +++ b/rpg-docs/server/publications/characterList.js @@ -15,6 +15,7 @@ Meteor.publish("characterList", function(){ { fields: { name: 1, + urlName: 1, race: 1, alignment: 1, gender: 1,