Skip to content

Commit

Permalink
Merge pull request TryGhost#2502 from ErisDS/issue-2460
Browse files Browse the repository at this point in the history
Ensure generateSlug gets transaction for tags
  • Loading branch information
ErisDS committed Mar 25, 2014
2 parents 3ee6987 + 438cbab commit 678d8c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions core/server/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ Tag = ghostBookshelf.Model.extend({

tableName: 'tags',

saving: function () {
saving: function (newPage, attr, options) {
/*jshint unused:false*/

var self = this;

ghostBookshelf.Model.prototype.saving.apply(this, arguments);

if (!this.get('slug')) {
// Generating a slug requires a db call to look for conflicting slugs
return ghostBookshelf.Model.generateSlug(Tag, this.get('name'))
if (this.hasChanged('slug') || !this.get('slug')) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
return ghostBookshelf.Model.generateSlug(Tag, this.get('slug') || this.get('name'),
{transacting: options.transacting})
.then(function (slug) {
self.set({slug: slug});
});
Expand Down
10 changes: 6 additions & 4 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ User = ghostBookshelf.Model.extend({

tableName: 'users',

saving: function () {
saving: function (newPage, attr, options) {
/*jshint unused:false*/

var self = this;
// disabling sanitization until we can implement a better version
// this.set('name', this.sanitize('name'));
Expand All @@ -49,14 +51,14 @@ User = ghostBookshelf.Model.extend({

ghostBookshelf.Model.prototype.saving.apply(this, arguments);

if (!this.get('slug')) {
if (this.hasChanged('slug') || !this.get('slug')) {
// Generating a slug requires a db call to look for conflicting slugs
return ghostBookshelf.Model.generateSlug(User, this.get('name'))
return ghostBookshelf.Model.generateSlug(User, this.get('slug') || this.get('name'),
{transacting: options.transacting})
.then(function (slug) {
self.set({slug: slug});
});
}

},

posts: function () {
Expand Down

0 comments on commit 678d8c8

Please sign in to comment.