Skip to content

Commit

Permalink
🎨 fix previos/current date comparison (isEqual in bookshelf) (TryGhos…
Browse files Browse the repository at this point in the history
…t#8357)

no issue

- client dates are sent as ISO format (moment(..).format())
- server dates are in JS Date format
  >> when bookshelf fetches data from the database, all dates are transformed into JS dates
  >> see `parse` helper function
- Bookshelf updates the model with the client data via Bookshelf's `set` function
- therefor Bookshelf uses a simple `isEqual` function from lodash to detect changes
- .previous(attr) and .get(attr) return false
- that has the concequence that dates are always marked as "changed"
- internally we use our `hasDateChanged` if we have to compare previous/updated dates
- but Bookshelf is not in our control for this case
  • Loading branch information
kirrg001 committed Apr 19, 2017
1 parent f739368 commit f2fd075
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
41 changes: 39 additions & 2 deletions core/server/models/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,51 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({

/**
* Filters potentially unsafe model attributes, so you can pass them to Bookshelf / Knex.
* This filter should be called before each insert/update operation.
*
* @param {Object} data Has keys representing the model's attributes/fields in the database.
* @return {Object} The filtered results of the passed in data, containing only what's allowed in the schema.
*/
filterData: function filterData(data) {
var permittedAttributes = this.prototype.permittedAttributes(),
filteredData = _.pick(data, permittedAttributes);
filteredData = _.pick(data, permittedAttributes),
sanitizedData = this.sanitizeData(filteredData);

return sanitizedData;
},

/**
* `sanitizeData` ensures that client data is in the correct format for further operations.
*
* Dates:
* - client dates are sent as ISO format (moment(..).format())
* - server dates are in JS Date format
* >> when bookshelf fetches data from the database, all dates are in JS Dates
* >> see `parse`
* - Bookshelf updates the model with the new client data via the `set` function
* - Bookshelf uses a simple `isEqual` function from lodash to detect real changes
* - .previous(attr) and .get(attr) returns false obviously
* - internally we use our `hasDateChanged` if we have to compare previous/updated dates
* - but Bookshelf is not in our control for this case
*
* @IMPORTANT
* Before the new client data get's inserted again, the dates get's retransformed into
* proper strings, see `format`.
*/
sanitizeData: function sanitizeData(data) {
var tableName = _.result(this.prototype, 'tableName');

_.each(data, function (value, key) {
if (value !== null
&& schema.tables[tableName].hasOwnProperty(key)
&& schema.tables[tableName][key].type === 'dateTime'
&& typeof value === 'string'
) {
data[key] = moment(value).toDate();
}
});

return filteredData;
return data;
},

/**
Expand Down
13 changes: 5 additions & 8 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,19 +528,16 @@ Post = ghostBookshelf.Model.extend({
},

/**
* Filters potentially unsafe model attributes, so you can pass them to Bookshelf / Knex.
* Manually add 'tags' attribute since it's not in the schema and call parent.
*
* @param {Object} data Has keys representing the model's attributes/fields in the database.
* @return {Object} The filtered results of the passed in data, containing only what's allowed in the schema.
*/
filterData: function filterData(data) {
var permittedAttributes = this.prototype.permittedAttributes(),
filteredData;

// manually add 'tags' attribute since it's not in the schema
permittedAttributes.push('tags');

filteredData = _.pick(data, permittedAttributes);
var filteredData = ghostBookshelf.Model.filterData.apply(this, arguments),
extraData = _.pick(data, ['tags']);

_.merge(filteredData, extraData);
return filteredData;
},

Expand Down

0 comments on commit f2fd075

Please sign in to comment.