mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-24 23:48:13 -05:00
Merge pull request #3089 from halfdan/app-slug
Generate slug when adding new app.
This commit is contained in:
commit
30a96f9f30
3 changed files with 46 additions and 11 deletions
|
@ -6,6 +6,22 @@ var ghostBookshelf = require('./base'),
|
||||||
App = ghostBookshelf.Model.extend({
|
App = ghostBookshelf.Model.extend({
|
||||||
tableName: 'apps',
|
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 () {
|
permissions: function () {
|
||||||
// Have to use the require here because of circular dependencies
|
// Have to use the require here because of circular dependencies
|
||||||
return this.belongsToMany(require('./permission').Permission, 'permissions_apps');
|
return this.belongsToMany(require('./permission').Permission, 'permissions_apps');
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
/*globals describe, before, beforeEach, afterEach, it*/
|
/*globals describe, before, beforeEach, afterEach, it*/
|
||||||
var testUtils = require('../../utils'),
|
var testUtils = require('../../utils'),
|
||||||
should = require('should'),
|
sequence = require('when/sequence'),
|
||||||
_ = require("lodash"),
|
should = require('should'),
|
||||||
|
_ = require("lodash"),
|
||||||
|
|
||||||
// Stuff we are testing
|
// Stuff we are testing
|
||||||
Models = require('../../../server/models'),
|
Models = require('../../../server/models'),
|
||||||
|
@ -62,19 +63,19 @@ describe('App Model', function () {
|
||||||
AppModel.findOne({id: 1}).then(function (foundApp) {
|
AppModel.findOne({id: 1}).then(function (foundApp) {
|
||||||
should.exist(foundApp);
|
should.exist(foundApp);
|
||||||
|
|
||||||
return foundApp.set({name: "New App"}).save();
|
return foundApp.set({name: 'New App'}).save();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
return AppModel.findOne({id: 1});
|
return AppModel.findOne({id: 1});
|
||||||
}).then(function (updatedApp) {
|
}).then(function (updatedApp) {
|
||||||
should.exist(updatedApp);
|
should.exist(updatedApp);
|
||||||
|
|
||||||
updatedApp.get("name").should.equal("New App");
|
updatedApp.get('name').should.equal('New App');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can add", function (done) {
|
it('can add', function (done) {
|
||||||
var newApp = testUtils.DataGenerator.forKnex.createApp(testUtils.DataGenerator.Content.apps[1]);
|
var newApp = testUtils.DataGenerator.forKnex.createApp(testUtils.DataGenerator.Content.apps[1]);
|
||||||
|
|
||||||
AppModel.add(newApp).then(function (createdApp) {
|
AppModel.add(newApp).then(function (createdApp) {
|
||||||
|
@ -86,7 +87,7 @@ describe('App Model', function () {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can destroy", function (done) {
|
it('can destroy', function (done) {
|
||||||
var firstApp = {id: 1};
|
var firstApp = {id: 1};
|
||||||
|
|
||||||
AppModel.findOne(firstApp).then(function (foundApp) {
|
AppModel.findOne(firstApp).then(function (foundApp) {
|
||||||
|
@ -104,4 +105,27 @@ describe('App Model', function () {
|
||||||
done();
|
done();
|
||||||
}).catch(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) {
|
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
|
// Create 12 posts with the same title
|
||||||
sequence(_.times(12, function (i) {
|
sequence(_.times(12, function (i) {
|
||||||
return function () {
|
return function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue