Simplify publish date checks

Fixes #814

- Changed to compare instances of `moment` using it's built in
comparison function instead of string compare with the date from the
database.
- Ensure compared date is parsed with the correct formats that we were
supporting before #1107.
- Moved date formats to variables to reduce chance for error in future.
This commit is contained in:
William Dibbern 2013-10-16 11:08:58 -05:00
parent 0e5dc9275c
commit 3e1b37e194

View File

@ -83,25 +83,27 @@
editDate: function (e) {
e.preventDefault();
var self = this,
momentPubDate,
parseDateFormats = ['DD MMM YY', 'DD MMM YYYY', 'DD/MM/YY', 'DD/MM/YYYY', 'DD-MM-YY', 'DD-MM-YYYY'],
displayDateFormat = 'DD MMM YY',
errMessage = '',
pubDate = self.model.get('published_at'),
pubDateMoment = moment(pubDate, parseDateFormats),
pubDateEl = e.currentTarget,
newPubDate = pubDateEl.value;
newPubDate = pubDateEl.value,
newPubDateMoment = moment(newPubDate, parseDateFormats);
// Ensure the published date has changed
if (newPubDate.length === 0 || moment(pubDate).format("DD MMM YY") === newPubDate) {
pubDateEl.value = pubDate === undefined ? 'Not Published' : moment(pubDate).format("DD MMM YY");
if (newPubDate.length === 0 || pubDateMoment.isSame(newPubDateMoment)) {
pubDateEl.value = pubDate === undefined ? 'Not Published' : pubDateMoment.format(displayDateFormat);
return;
}
// Validate new Published date
momentPubDate = moment(newPubDate, ["DD MMM YY", "DD MMM YYYY", "DD/MM/YY", "DD/MM/YYYY", "DD-MM-YY", "DD-MM-YYYY"]);
if (!momentPubDate.isValid()) {
if (!newPubDateMoment.isValid()) {
errMessage = 'Published Date must be a valid date with format: DD MMM YY (e.g. 6 Dec 14)';
}
if (momentPubDate.diff(new Date(), 'h') > 0) {
if (newPubDateMoment.diff(new Date(), 'h') > 0) {
errMessage = 'Published Date cannot currently be in the future.';
}
@ -111,20 +113,20 @@
message: errMessage,
status: 'passive'
});
pubDateEl.value = moment(pubDate).format("DD MMM YY");
pubDateEl.value = pubDateMoment.format(displayDateFormat);
return;
}
// Save new 'Published' date
this.model.save({
// Temp Fix. Set hour to 12 instead of 00 to avoid some TZ issues.
published_at: momentPubDate.hour(12).toDate()
published_at: newPubDateMoment.hour(12).toDate()
}, {
success : function (model, response, options) {
pubDateEl.value = moment(model.get('published_at')).format("DD MMM YY");
pubDateEl.value = moment(model.get('published_at'), parseDateFormats).format(displayDateFormat);
Ghost.notifications.addItem({
type: 'success',
message: "Publish date successfully changed to <strong>" + pubDateEl.value + '</strong>.',
message: 'Publish date successfully changed to <strong>' + pubDateEl.value + '</strong>.',
status: 'passive'
});
},