Skip to content

Commit

Permalink
🐛 fix hasDateChanged (#8291)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kirrg001 authored and kevinansfield committed Apr 6, 2017
1 parent 791f1b5 commit 59a8911
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
13 changes: 11 additions & 2 deletions core/server/models/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,17 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
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;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Post = ghostBookshelf.Model.extend({
prevSlug = this._previousAttributes.slug,
tagsToCheck = this.get('tags'),
publishedAt = this.get('published_at'),
publishedAtHasChanged = this.hasDateChanged('published_at'),
publishedAtHasChanged = this.hasDateChanged('published_at', {beforeWrite: true}),
mobiledoc = this.get('mobiledoc'),
tags = [];

Expand Down
43 changes: 40 additions & 3 deletions core/test/integration/model/model_posts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,21 @@ describe('Post Model', function () {
});

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);
post = results.toJSON();
post.status.should.equal('draft');

results.set('published_at', null);
return results.save();
}).then(function () {
return PostModel.edit({
status: 'scheduled'
}, _.extend({}, context, {id: post.id}));
}).then(function () {
done(new Error('expected error'));
}).catch(function (err) {
should.exist(err);
(err instanceof errors.ValidationError).should.eql(true);
Expand Down Expand Up @@ -570,7 +575,7 @@ describe('Post Model', function () {

return PostModel.edit({
status: 'scheduled',
published_at: moment().add(20, 'days')
published_at: moment().add(20, 'days').toDate()
}, _.extend({}, context, {id: post.id}));
}).then(function (edited) {
should.exist(edited);
Expand Down Expand Up @@ -606,6 +611,38 @@ describe('Post Model', function () {
}).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) {
var postId = testUtils.DataGenerator.Content.posts[0].id;

Expand Down

0 comments on commit 59a8911

Please sign in to comment.