2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2016-05-24 15:06:59 +03:00
|
|
|
import ApplicationSerializer from 'ghost-admin/serializers/application';
|
2016-03-21 17:44:55 +03:00
|
|
|
import EmbeddedRecordsMixin from 'ember-data/serializers/embedded-records-mixin';
|
2017-05-29 21:50:03 +03:00
|
|
|
import Ember from 'ember';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
const {String: {pluralize}} = Ember;
|
2016-06-11 19:52:36 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
|
2014-06-27 06:35:25 +04:00
|
|
|
// settings for the EmbeddedRecordsMixin.
|
|
|
|
attrs: {
|
2016-06-14 11:11:59 +03:00
|
|
|
tags: {embedded: 'always'},
|
|
|
|
publishedAtUTC: {key: 'published_at'},
|
|
|
|
createdAtUTC: {key: 'created_at'},
|
|
|
|
updatedAtUTC: {key: 'updated_at'}
|
2014-06-27 06:35:25 +04:00
|
|
|
},
|
|
|
|
|
2016-04-13 17:19:44 +03:00
|
|
|
normalize(model, hash, prop) {
|
2016-01-23 21:12:22 +03:00
|
|
|
// this is to enable us to still access the raw authorId
|
2014-07-31 06:44:51 +04:00
|
|
|
// without requiring an extra get request (since it is an
|
|
|
|
// async relationship).
|
2016-04-13 17:19:44 +03:00
|
|
|
if ((prop === 'post' || prop === 'posts') && hash.author !== undefined) {
|
2016-02-16 18:37:15 +03:00
|
|
|
hash.author_id = hash.author;
|
|
|
|
}
|
2016-04-13 17:19:44 +03:00
|
|
|
|
|
|
|
return this._super(...arguments);
|
2014-07-31 06:44:51 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
normalizeSingleResponse(store, primaryModelClass, payload) {
|
|
|
|
let root = this.keyForAttribute(primaryModelClass.modelName);
|
2016-06-11 19:52:36 +03:00
|
|
|
let pluralizedRoot = pluralize(primaryModelClass.modelName);
|
2014-06-27 06:35:25 +04:00
|
|
|
|
2016-11-03 16:39:41 +03:00
|
|
|
if (payload[pluralizedRoot]) {
|
|
|
|
payload[root] = payload[pluralizedRoot][0];
|
|
|
|
delete payload[pluralizedRoot];
|
|
|
|
}
|
2014-06-27 06:35:25 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this._super(...arguments);
|
2014-06-27 06:35:25 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
normalizeArrayResponse() {
|
|
|
|
return this._super(...arguments);
|
2015-09-03 14:06:50 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
serializeIntoHash(hash, type, record, options) {
|
2014-06-27 06:35:25 +04:00
|
|
|
options = options || {};
|
2014-12-18 21:39:11 +03:00
|
|
|
options.includeId = true;
|
2014-06-27 06:35:25 +04:00
|
|
|
|
|
|
|
// We have a plural root in the API
|
2016-06-11 19:52:36 +03:00
|
|
|
let root = pluralize(type.modelName);
|
2015-10-28 14:36:45 +03:00
|
|
|
let data = this.serialize(record, options);
|
2014-06-27 06:35:25 +04:00
|
|
|
|
2014-12-15 18:01:30 +03:00
|
|
|
// Properties that exist on the model but we don't want sent in the payload
|
|
|
|
|
2014-06-27 06:35:25 +04:00
|
|
|
delete data.uuid;
|
2014-09-04 00:51:52 +04:00
|
|
|
delete data.html;
|
2014-12-15 18:01:30 +03:00
|
|
|
// Inserted locally as a convenience.
|
2016-02-16 18:37:15 +03:00
|
|
|
delete data.author_id;
|
2014-12-15 18:01:30 +03:00
|
|
|
// Read-only virtual property.
|
|
|
|
delete data.url;
|
2014-06-27 06:35:25 +04:00
|
|
|
|
|
|
|
hash[root] = [data];
|
2014-06-13 04:48:15 +04:00
|
|
|
}
|
|
|
|
});
|