2020-01-16 20:01:12 +03:00
|
|
|
import RESTSerializer from '@ember-data/serializer/rest';
|
2018-10-18 20:23:19 +03:00
|
|
|
import {camelize, decamelize, underscore} from '@ember/string';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {pluralize} from 'ember-inflector';
|
2016-01-23 21:12:22 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default RESTSerializer.extend({
|
2019-12-17 23:42:42 +03:00
|
|
|
// hacky method for getting access to meta data for single-resource responses
|
|
|
|
// https://github.com/emberjs/data/pull/4077#issuecomment-200780097
|
|
|
|
// TODO: review once the record links and meta RFC lands
|
|
|
|
// https://github.com/emberjs/rfcs/blob/master/text/0332-ember-data-record-links-and-meta.md
|
|
|
|
extractMeta(store, typeClass) {
|
|
|
|
let meta = this._super(...arguments);
|
|
|
|
typeClass.___meta = meta;
|
|
|
|
return meta;
|
|
|
|
},
|
|
|
|
|
2018-04-05 13:54:36 +03:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
serializeIntoHash(hash, type, record, options) {
|
2014-05-09 09:00:10 +04:00
|
|
|
// Our API expects an id on the posted object
|
|
|
|
options = options || {};
|
|
|
|
options.includeId = true;
|
|
|
|
|
|
|
|
// 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-05-09 09:00:10 +04:00
|
|
|
|
|
|
|
hash[root] = [data];
|
2016-01-23 21:12:22 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
keyForAttribute(attr) {
|
|
|
|
return decamelize(attr);
|
2018-10-18 20:23:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
keyForRelationship(key, typeClass, method) {
|
|
|
|
let transform = method === 'serialize' ? underscore : camelize;
|
|
|
|
|
|
|
|
if (typeClass === 'belongsTo' && !key.match(/(Id|By)$/)) {
|
|
|
|
let transformed = `${transform(key)}_id`;
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return transform(key);
|
2014-05-09 09:00:10 +04:00
|
|
|
}
|
2014-06-09 15:02:51 +04:00
|
|
|
});
|