mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
🐛 fix hasDateChanged (#8291)
no issue - i don't know if this never worked or has worked and something changed in bookshelf - but this fixes: saving the content (no change for published_at) of a scheduled post within the 2minutes window - add `beforeWrite` option to hasDateChanged helper, see comment - use previous for `beforeWrite` operations - add a test and fix some other small issues in the scheduler tests
This commit is contained in:
parent
791f1b55df
commit
59a8911830
3 changed files with 52 additions and 6 deletions
|
@ -249,8 +249,17 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
return this.updatedAttributes()[attr];
|
return this.updatedAttributes()[attr];
|
||||||
},
|
},
|
||||||
|
|
||||||
hasDateChanged: function (attr) {
|
/**
|
||||||
return moment(this.get(attr)).diff(moment(this.updated(attr))) !== 0;
|
* There is difference between `updated` and `previous`:
|
||||||
|
* Depending on the hook (before or after writing into the db), both fields have a different meaning.
|
||||||
|
* e.g. onSaving -> before db write (has to use previous)
|
||||||
|
* onUpdated -> after db write (has to use updated)
|
||||||
|
*
|
||||||
|
* hasDateChanged('attr', {beforeWrite: true})
|
||||||
|
*/
|
||||||
|
hasDateChanged: function (attr, options) {
|
||||||
|
options = options || {};
|
||||||
|
return moment(this.get(attr)).diff(moment(options.beforeWrite ? this.previous(attr) : this.updated(attr))) !== 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -149,7 +149,7 @@ Post = ghostBookshelf.Model.extend({
|
||||||
prevSlug = this._previousAttributes.slug,
|
prevSlug = this._previousAttributes.slug,
|
||||||
tagsToCheck = this.get('tags'),
|
tagsToCheck = this.get('tags'),
|
||||||
publishedAt = this.get('published_at'),
|
publishedAt = this.get('published_at'),
|
||||||
publishedAtHasChanged = this.hasDateChanged('published_at'),
|
publishedAtHasChanged = this.hasDateChanged('published_at', {beforeWrite: true}),
|
||||||
mobiledoc = this.get('mobiledoc'),
|
mobiledoc = this.get('mobiledoc'),
|
||||||
tags = [];
|
tags = [];
|
||||||
|
|
||||||
|
|
|
@ -472,16 +472,21 @@ describe('Post Model', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('draft -> scheduled without published_at update', function (done) {
|
it('draft -> scheduled without published_at update', function (done) {
|
||||||
PostModel.findOne({status: 'draft'}).then(function (results) {
|
var post;
|
||||||
var post;
|
|
||||||
|
|
||||||
|
PostModel.findOne({status: 'draft'}).then(function (results) {
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
post = results.toJSON();
|
post = results.toJSON();
|
||||||
post.status.should.equal('draft');
|
post.status.should.equal('draft');
|
||||||
|
|
||||||
|
results.set('published_at', null);
|
||||||
|
return results.save();
|
||||||
|
}).then(function () {
|
||||||
return PostModel.edit({
|
return PostModel.edit({
|
||||||
status: 'scheduled'
|
status: 'scheduled'
|
||||||
}, _.extend({}, context, {id: post.id}));
|
}, _.extend({}, context, {id: post.id}));
|
||||||
|
}).then(function () {
|
||||||
|
done(new Error('expected error'));
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
should.exist(err);
|
should.exist(err);
|
||||||
(err instanceof errors.ValidationError).should.eql(true);
|
(err instanceof errors.ValidationError).should.eql(true);
|
||||||
|
@ -570,7 +575,7 @@ describe('Post Model', function () {
|
||||||
|
|
||||||
return PostModel.edit({
|
return PostModel.edit({
|
||||||
status: 'scheduled',
|
status: 'scheduled',
|
||||||
published_at: moment().add(20, 'days')
|
published_at: moment().add(20, 'days').toDate()
|
||||||
}, _.extend({}, context, {id: post.id}));
|
}, _.extend({}, context, {id: post.id}));
|
||||||
}).then(function (edited) {
|
}).then(function (edited) {
|
||||||
should.exist(edited);
|
should.exist(edited);
|
||||||
|
@ -606,6 +611,38 @@ describe('Post Model', function () {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('scheduled -> scheduled with unchanged published_at (within the 2 minutes window)', function (done) {
|
||||||
|
var post;
|
||||||
|
|
||||||
|
PostModel.findOne({status: 'scheduled'}).then(function (results) {
|
||||||
|
should.exist(results);
|
||||||
|
post = results.toJSON();
|
||||||
|
post.status.should.equal('scheduled');
|
||||||
|
|
||||||
|
results.set('published_at', moment().add(2, 'minutes').add(2, 'seconds').toDate());
|
||||||
|
return results.save();
|
||||||
|
}).then(function () {
|
||||||
|
Object.keys(eventsTriggered).length.should.eql(2);
|
||||||
|
should.exist(eventsTriggered['post.edited']);
|
||||||
|
should.exist(eventsTriggered['post.rescheduled']);
|
||||||
|
eventsTriggered = {};
|
||||||
|
|
||||||
|
return Promise.delay(1000 * 3);
|
||||||
|
}).then(function () {
|
||||||
|
return PostModel.edit({
|
||||||
|
status: 'scheduled'
|
||||||
|
}, _.extend({}, context, {id: post.id}));
|
||||||
|
}).then(function (edited) {
|
||||||
|
should.exist(edited);
|
||||||
|
edited.attributes.status.should.equal('scheduled');
|
||||||
|
|
||||||
|
Object.keys(eventsTriggered).length.should.eql(1);
|
||||||
|
should.exist(eventsTriggered['post.edited']);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('published -> scheduled and expect update of published_at', function (done) {
|
it('published -> scheduled and expect update of published_at', function (done) {
|
||||||
var postId = testUtils.DataGenerator.Content.posts[0].id;
|
var postId = testUtils.DataGenerator.Content.posts[0].id;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue