Skip to content

Commit

Permalink
Merge pull request #6631 from dbalders/draft-slug
Browse files Browse the repository at this point in the history
Update Post Slug while Draft on Title Changes
  • Loading branch information
ErisDS committed Mar 29, 2016
2 parents 5bd30ef + 81c215d commit 48b846b
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions core/server/models/post.js
Expand Up @@ -96,7 +96,13 @@ Post = ghostBookshelf.Model.extend({
var self = this,
tagsToCheck,
title,
i;
i,
// Variables to make the slug checking more readable
newTitle = this.get('title'),
prevTitle = this._previousAttributes.title,
prevSlug = this._previousAttributes.slug,
postStatus = this.get('status'),
publishedAt = this.get('published_at');

options = options || {};
// keep tags for 'saved' event and deduplicate upper/lowercase tags
Expand Down Expand Up @@ -141,13 +147,31 @@ Post = ghostBookshelf.Model.extend({
}
}

if (this.hasChanged('slug') || !this.get('slug')) {
// If a title is set, not the same as the old title, a draft post, and has never been published
if (prevTitle !== undefined && newTitle !== prevTitle && postStatus === 'draft' && !publishedAt) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
return ghostBookshelf.Model.generateSlug(Post, this.get('slug') || this.get('title'),
return ghostBookshelf.Model.generateSlug(Post, this.get('title'),
{status: 'all', transacting: options.transacting, importing: options.importing})
.then(function then(slug) {
self.set({slug: slug});
// After the new slug is found, do another generate for the old title to compare it to the old slug
return ghostBookshelf.Model.generateSlug(Post, prevTitle).then(function then(prevTitleSlug) {
// If the old slug is the same as the slug that was generated from the old title
// then set a new slug. If it is not the same, means was set by the user
if (prevTitleSlug === prevSlug) {
self.set({slug: slug});
}
});
});
} else {
// If any of the attributes above were false, set initial slug and check to see if slug was changed by the user
if (this.hasChanged('slug') || !this.get('slug')) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
return ghostBookshelf.Model.generateSlug(Post, this.get('slug') || this.get('title'),
{status: 'all', transacting: options.transacting, importing: options.importing})
.then(function then(slug) {
self.set({slug: slug});
});
}
}
},

Expand Down

0 comments on commit 48b846b

Please sign in to comment.