Ghost/ghost/admin/mirage/serializers/application.js
Kevin Ansfield 983110d931 Switched from ember-cli-shims to new module imports (#779)
no issue

- add eslint-plugin-ember, configure no-old-shims rule
- run `eslint --fix` on `app`, `lib`, `mirage`, and `tests` to move imports to the new module imports
- further cleanup of Ember globals usage
- remove event-dispatcher initializer now that `canDispatchToEventManager` is deprecated
2017-08-22 14:53:26 +07:00

37 lines
1.3 KiB
JavaScript

import {Collection, RestSerializer} from 'ember-cli-mirage';
import {pluralize} from 'ember-cli-mirage/utils/inflector';
import {underscore} from '@ember/string';
export default RestSerializer.extend({
keyForAttribute(attr) {
return underscore(attr);
},
serialize(object, request) {
// Ember expects pluralized responses for the post, user, and invite models,
// and this shortcut will ensure that those models are pluralized
if (this.isModel(object) && ['post', 'user', 'invite'].includes(object.modelName)) {
object = new Collection(object.modelName, [object]);
}
let json = RestSerializer.prototype.serialize.call(this, object, request);
if (this.isCollection(object) && object.meta) {
json.meta = object.meta;
}
return json;
},
// POST and PUT request send data in pluralized attributes for all models,
// so we extract it here - this allows #normalizedRequestAttrs to work
// in route functions
normalize(body, modelName) {
// sometimes mirage doesn't include a modelName, so we extrapolate it from
// the first element of Object.keys
modelName = pluralize(modelName) || Object.keys(body)[0];
let [attributes] = body[modelName] || [{}];
return {data: {attributes}};
}
});