Merge pull request #222 from jgable/slugIncrement

Increment slug if duplicate
This commit is contained in:
Hannah Wolfe 2013-06-29 16:56:17 -07:00
commit 9bb4ae62a6
4 changed files with 54 additions and 10 deletions

View File

@ -20,7 +20,7 @@ errors = {
logError: function (err) {
err = err || "Unknown";
// TODO: Logging framework hookup
console.log("Error occurred: ", err.message || err);
console.log("Error occurred: ", err.message || err, err.stack || "");
},
logAndThrowError: function (err) {

View File

@ -45,10 +45,7 @@ Post = GhostBookshelf.Model.extend({
},
creating: function () {
if (!this.get('slug')) {
this.generateSlug();
}
var self = this;
if (!this.get('created_by')) {
this.set('created_by', 1);
}
@ -56,20 +53,46 @@ Post = GhostBookshelf.Model.extend({
if (!this.get('author_id')) {
this.set('author_id', 1);
}
if (!this.get('slug')) {
// Generating a slug requires a db call to look for conflicting slugs
return this.generateSlug(this.get('title'))
.then(function (slug) {
self.set({slug: slug});
});
}
},
// #### generateSlug
// Create a string act as the permalink for a post.
generateSlug: function () {
generateSlug: function (title) {
var slug,
slugTryCount = 1,
// Look for a post with a matching slug, append an incrementing number if so
checkIfSlugExists = function (slugToFind) {
return Post.read({slug: slugToFind}).then(function (found) {
if (!found) {
return when.resolve(slugToFind);
}
slugTryCount += 1;
// TODO: Bug out (when.reject) if over 10 tries or something?
return checkIfSlugExists(slugToFind + slugTryCount);
});
};
// Remove reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\` and convert spaces to hyphens
var slug = this.get('title').replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/\s/g, '-').toLowerCase();
slug = title.replace(/[:\/\?#\[\]@!$&'()*+,;=\\]/g, '').replace(/\s/g, '-').toLowerCase();
// Remove trailing hypen
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 2) : slug;
// Check the filtered slug doesn't match any of the reserved keywords
slug = /^(ghost|ghost\-admin|admin|wp\-admin|dashboard|login|archive|archives|category|categories|tag|tags|page|pages|post|posts)$/g
.test(slug) ? slug + '-post' : slug;
// TODO: test for duplicate slugs and increment
return this.set('slug', slug);
// Test for duplicate slugs.
return checkIfSlugExists(slug);
},
user: function () {

View File

@ -100,6 +100,27 @@ describe('Post Model', function () {
});
it('can generate a non conflicting slug', function (done) {
var newPost = {
title: 'Test Title',
content: 'Test Content 1'
};
PostModel.add(newPost).then(function (createdPost) {
createdPost.get('slug').should.equal('test-title');
newPost.content = 'Test Content 2';
return PostModel.add(newPost);
}).then(function (secondPost) {
secondPost.get('slug').should.equal('test-title2');
secondPost.get('content').should.equal("Test Content 2");
done();
}).otherwise(done);
});
it('can delete', function (done) {
var firstPostId;
PostModel.browse().then(function (results) {

View File

@ -20,7 +20,7 @@ describe('permissions', function () {
return when(helpers.insertDefaultUser());
}).then(function (results) {
done();
});
}).otherwise(errors.logAndThrowError);
});
// beforeEach(function (done) {