Add character names to the end of their URL's

Closes #21, makes links to characters human readable
This commit is contained in:
Stefan Zermatten
2017-07-13 13:14:04 +02:00
parent 089feae26f
commit 3227cd0934
9 changed files with 59 additions and 9 deletions

View File

@@ -48,3 +48,4 @@ es5-shim@4.6.15
differential:vulcanize
reactive-dict
percolate:synced-cron
ongoworks:speakingurl

View File

@@ -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

View File

@@ -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({

View File

@@ -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() {

View File

@@ -11,7 +11,7 @@
{{#if characters.count}}
<div class="character-list layout horizontal wrap">
{{# each characters}}
<a class="character-card flex layout vertical end-justified" href="/character/{{_id}}">
<a class="character-card flex layout vertical end-justified" href="{{pathFor route="characterSheet" data=this}}">
<iron-image class="fit {{colorClass}}"
sizing="cover" preload fade src={{picture}}>
</iron-image>

View File

@@ -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},
}
);

View File

@@ -14,7 +14,7 @@ Template.characterSideList.helpers({
]
},
{
fields: {name: 1},
fields: {name: 1, urlName: 1},
sort: {name: 1},
}
);

View File

@@ -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;
},
});

View File

@@ -15,6 +15,7 @@ Meteor.publish("characterList", function(){
{
fields: {
name: 1,
urlName: 1,
race: 1,
alignment: 1,
gender: 1,