Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Don't send created_by and updated_by attrs in API requests
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#9548
- refactor serialisers to use `serialize` rather than `serializeToHash` to avoid code duplication
- strip `created_by` and `updated_by` attrs when serializing - Ghost will set these automatically based on the currently logged in user
  • Loading branch information
kevinansfield committed Apr 5, 2018
1 parent e7c9406 commit 7fa52be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
10 changes: 10 additions & 0 deletions app/serializers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import {decamelize} from '@ember/string';
import {pluralize} from 'ember-inflector';

export default RESTSerializer.extend({
serialize(/*snapshot, options*/) {
let json = this._super(...arguments);

// don't send attributes that are updated automatically on the server
delete json.created_by;
delete json.updated_by;

return json;
},

serializeIntoHash(hash, type, record, options) {
// Our API expects an id on the posted object
options = options || {};
Expand Down
29 changes: 8 additions & 21 deletions app/serializers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,19 @@ export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
return this._super(...arguments);
},

serializeIntoHash(hash, type, record, options) {
options = options || {};
options.includeId = true;

// We have a plural root in the API
let root = pluralize(type.modelName);

// TODO: this is throwing a warning when saving a new post:
// The embedded relationship 'tags' is undefined for 'post' with id 'null'.
// Please include it in your original payload.
//
// This appears to be an issue in Ember Data - needs further investigation
// and possibly an issue raised
let data = this.serialize(record, options);
serialize(/*snapshot, options*/) {
let json = this._super(...arguments);

// Properties that exist on the model but we don't want sent in the payload

delete data.uuid;
delete data.html;
delete json.uuid;
delete json.html;
// Inserted locally as a convenience.
delete data.author_id;
delete json.author_id;
// Read-only virtual property.
delete data.url;
delete json.url;
// Deprecated property (replaced with data.authors)
delete data.author;
delete json.author;

hash[root] = [data];
return json;
}
});
13 changes: 4 additions & 9 deletions app/serializers/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ export default ApplicationSerializer.extend({
updatedAtUTC: {key: 'updated_at'}
},

serializeIntoHash(hash, type, record, options) {
options = options || {};
options.includeId = true;

let root = pluralize(type.modelName);
let data = this.serialize(record, options);
serialize(/*snapshot, options*/) {
let json = this._super(...arguments);

// Properties that exist on the model but we don't want sent in the payload
delete json.count;

delete data.count;

hash[root] = [data];
return json;
},

// if we use `queryRecord` ensure we grab the first record to avoid
Expand Down

0 comments on commit 7fa52be

Please sign in to comment.