2016-05-24 15:06:59 +03:00
|
|
|
import ApplicationSerializer from 'ghost-admin/serializers/application';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {pluralize} from 'ember-inflector';
|
2016-06-11 19:52:36 +03:00
|
|
|
|
2022-02-02 21:27:03 +03:00
|
|
|
export default class Setting extends ApplicationSerializer {
|
2015-10-28 14:36:45 +03:00
|
|
|
serializeIntoHash(hash, type, record, options) {
|
2014-06-20 06:29:49 +04:00
|
|
|
// Settings API does not want ids
|
|
|
|
options = options || {};
|
|
|
|
options.includeId = false;
|
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
let root = pluralize(type.modelName);
|
2022-10-12 16:03:54 +03:00
|
|
|
let data = Object.keys(record.record.changedAttributes()).length > 0 ?
|
|
|
|
this.serialize(record, options) : [];
|
2015-10-28 14:36:45 +03:00
|
|
|
let payload = [];
|
2014-06-20 06:29:49 +04:00
|
|
|
|
|
|
|
delete data.id;
|
2022-07-15 15:44:26 +03:00
|
|
|
delete data._meta;
|
2014-06-20 06:29:49 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
Object.keys(data).forEach((k) => {
|
2014-10-25 01:09:50 +04:00
|
|
|
payload.push({key: k, value: data[k]});
|
2014-06-20 06:29:49 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
hash[root] = payload;
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|
2014-06-20 06:29:49 +04:00
|
|
|
|
2022-10-12 16:03:54 +03:00
|
|
|
serializeAttribute(snapshot, json, key, attributes) {
|
2023-09-25 19:29:09 +03:00
|
|
|
// Only serialize attributes that have changed and
|
2022-10-12 16:03:54 +03:00
|
|
|
// send a partial update to the API to avoid conflicts
|
|
|
|
// with different screens using the same model
|
|
|
|
// See https://github.com/TryGhost/Ghost/issues/15470
|
|
|
|
if (
|
|
|
|
!snapshot.record.get('isNew') &&
|
|
|
|
!snapshot.record.changedAttributes()[key]
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.serializeAttribute(snapshot, json, key, attributes);
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
normalizeArrayResponse(store, primaryModelClass, _payload, id, requestType) {
|
|
|
|
let payload = {settings: [this._extractObjectFromArrayPayload(_payload)]};
|
2022-02-02 21:27:03 +03:00
|
|
|
return super.normalizeArrayResponse(store, primaryModelClass, payload, id, requestType);
|
|
|
|
}
|
2015-09-03 14:06:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
normalizeSingleResponse(store, primaryModelClass, _payload, id, requestType) {
|
|
|
|
let payload = {setting: this._extractObjectFromArrayPayload(_payload)};
|
2022-02-02 21:27:03 +03:00
|
|
|
return super.normalizeSingleResponse(store, primaryModelClass, payload, id, requestType);
|
|
|
|
}
|
2015-09-03 14:06:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_extractObjectFromArrayPayload(_payload) {
|
|
|
|
let payload = {id: '0'};
|
2014-06-20 06:29:49 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_payload.settings.forEach((setting) => {
|
2014-06-20 06:29:49 +04:00
|
|
|
payload[setting.key] = setting.value;
|
|
|
|
});
|
|
|
|
|
2022-07-15 15:44:26 +03:00
|
|
|
// HACK: Ember Data doesn't expose `meta` properties consistently
|
|
|
|
// - https://github.com/emberjs/data/issues/2905
|
|
|
|
//
|
|
|
|
// We need the `meta` data returned when saving so we extract it and dump
|
|
|
|
// it onto the model as an attribute then delete it again when serializing.
|
|
|
|
if (_payload.meta) {
|
|
|
|
payload._meta = _payload.meta;
|
|
|
|
}
|
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
return payload;
|
2014-06-20 06:29:49 +04:00
|
|
|
}
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|