mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Generate slug when adding new app.
No issue - Remove duplicate code from model_posts_spec. - Add slug generation to app model - Test slug generation on app model
This commit is contained in:
parent
36fc769733
commit
3d2205195d
3 changed files with 46 additions and 11 deletions
|
@ -6,6 +6,22 @@ var ghostBookshelf = require('./base'),
|
|||
App = ghostBookshelf.Model.extend({
|
||||
tableName: 'apps',
|
||||
|
||||
saving: function (newPage, attr, options) {
|
||||
/*jshint unused:false*/
|
||||
var self = this;
|
||||
|
||||
ghostBookshelf.Model.prototype.saving.apply(this, arguments);
|
||||
|
||||
if (this.hasChanged('slug') || !this.get('slug')) {
|
||||
// Pass the new slug through the generator to strip illegal characters, detect duplicates
|
||||
return ghostBookshelf.Model.generateSlug(App, this.get('slug') || this.get('name'),
|
||||
{transacting: options.transacting})
|
||||
.then(function (slug) {
|
||||
self.set({slug: slug});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
permissions: function () {
|
||||
// Have to use the require here because of circular dependencies
|
||||
return this.belongsToMany(require('./permission').Permission, 'permissions_apps');
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
/*globals describe, before, beforeEach, afterEach, it*/
|
||||
var testUtils = require('../../utils'),
|
||||
should = require('should'),
|
||||
_ = require("lodash"),
|
||||
sequence = require('when/sequence'),
|
||||
should = require('should'),
|
||||
_ = require("lodash"),
|
||||
|
||||
// Stuff we are testing
|
||||
Models = require('../../../server/models'),
|
||||
|
@ -62,19 +63,19 @@ describe('App Model', function () {
|
|||
AppModel.findOne({id: 1}).then(function (foundApp) {
|
||||
should.exist(foundApp);
|
||||
|
||||
return foundApp.set({name: "New App"}).save();
|
||||
return foundApp.set({name: 'New App'}).save();
|
||||
}).then(function () {
|
||||
return AppModel.findOne({id: 1});
|
||||
}).then(function (updatedApp) {
|
||||
should.exist(updatedApp);
|
||||
|
||||
updatedApp.get("name").should.equal("New App");
|
||||
updatedApp.get('name').should.equal('New App');
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it("can add", function (done) {
|
||||
it('can add', function (done) {
|
||||
var newApp = testUtils.DataGenerator.forKnex.createApp(testUtils.DataGenerator.Content.apps[1]);
|
||||
|
||||
AppModel.add(newApp).then(function (createdApp) {
|
||||
|
@ -86,7 +87,7 @@ describe('App Model', function () {
|
|||
}).catch(done);
|
||||
});
|
||||
|
||||
it("can destroy", function (done) {
|
||||
it('can destroy', function (done) {
|
||||
var firstApp = {id: 1};
|
||||
|
||||
AppModel.findOne(firstApp).then(function (foundApp) {
|
||||
|
@ -104,4 +105,27 @@ describe('App Model', function () {
|
|||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('can generate a slug', function (done) {
|
||||
// Create 12 apps
|
||||
sequence(_.times(12, function (i) {
|
||||
return function () {
|
||||
return AppModel.add({
|
||||
name: 'Kudos ' + i,
|
||||
version: '0.0.1',
|
||||
status: 'installed'
|
||||
}, {user: 1});
|
||||
};
|
||||
})).then(function (createdApps) {
|
||||
// Should have created 12 apps
|
||||
createdApps.length.should.equal(12);
|
||||
|
||||
// Should have matching slugs
|
||||
_(createdApps).each(function (app, i) {
|
||||
app.get('slug').should.equal('kudos-' + i);
|
||||
});
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -252,11 +252,6 @@ describe('Post Model', function () {
|
|||
});
|
||||
|
||||
it('can generate a non conflicting slug', function (done) {
|
||||
var newPost = {
|
||||
title: 'Test Title',
|
||||
markdown: 'Test Content 1'
|
||||
};
|
||||
|
||||
// Create 12 posts with the same title
|
||||
sequence(_.times(12, function (i) {
|
||||
return function () {
|
||||
|
|
Loading…
Add table
Reference in a new issue