Use clearer urls fro documents

This commit is contained in:
Jori Lallo
2016-08-15 21:41:51 +02:00
parent 537341c01c
commit 3089ac7bc8
14 changed files with 88 additions and 56 deletions

View File

@@ -1,3 +1,5 @@
import slug from 'slug';
import randomstring from 'randomstring';
import {
DataTypes,
sequelize,
@@ -5,10 +7,13 @@ import {
import _ from 'lodash';
import Document from './Document';
slug.defaults.mode = 'rfc3986';
const allowedAtlasTypes = [['atlas', 'journal']];
const Atlas = sequelize.define('atlas', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
urlId: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
description: DataTypes.STRING,
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes } },
@@ -20,6 +25,9 @@ const Atlas = sequelize.define('atlas', {
tableName: 'atlases',
paranoid: true,
hooks: {
beforeValidate: (collection) => {
collection.urlId = collection.urlId || randomstring.generate(10);
},
afterCreate: async (collection) => {
if (collection.type !== 'atlas') return;
@@ -38,8 +46,12 @@ const Atlas = sequelize.define('atlas', {
},
},
instanceMethods: {
getUrl() {
// const slugifiedName = slug(this.name);
// return `/${slugifiedName}-c${this.urlId}`;
return `/collections/${this.id}`;
},
async buildStructure() {
console.log('start');
if (this.navigationTree) return this.navigationTree;
const getNodeForDocument = async (document) => {

View File

@@ -16,11 +16,6 @@ import Revision from './Revision';
slug.defaults.mode = 'rfc3986';
const generateSlug = (title, urlId) => {
const slugifiedTitle = slug(title);
return `${slugifiedTitle}-${urlId}`;
};
const createRevision = async (doc) => {
// Create revision of the current (latest)
await Revision.create({
@@ -80,7 +75,7 @@ const Document = sequelize.define('document', {
paranoid: true,
hooks: {
beforeValidate: (doc) => {
doc.urlId = randomstring.generate(15);
doc.urlId = doc.urlId || randomstring.generate(10);
},
beforeCreate: documentBeforeSave,
beforeUpdate: documentBeforeSave,
@@ -88,12 +83,9 @@ const Document = sequelize.define('document', {
afterUpdate: async (doc) => await createRevision(doc),
},
instanceMethods: {
buildUrl() {
const slugifiedTitle = slug(this.title);
return `${slugifiedTitle}-${this.urlId}`;
},
getUrl() {
return `/documents/${this.id}`;
const slugifiedTitle = slug(this.title);
return `/d/${slugifiedTitle}-${this.urlId}`;
},
},
});