travis-web/app/serializers/v2_fallback.js
Piotr Sarnacki d9cff6e8b4 Create adapters and serializers working with v3 and v2 APIs
This commit adds adapters and serializers for v3, but also a fallback
serializer for v2, which allows to handle v2 and v3 payloads at the same
time. This is needed, because when we use v3 endpoint for one of the
models (in this case repo), we can also get embedded records of other
types (like branch or build).
2015-12-08 10:18:02 +01:00

42 lines
1.2 KiB
JavaScript

import Ember from 'ember';
import V3Serializer from 'travis/serializers/v3';
export default V3Serializer.extend({
isNewSerializerAPI: true,
extractRelationships(modelClass, resourceHash) {
if(resourceHash['@type']) {
return this._super(...arguments);
} else {
let relationships = {};
modelClass.eachRelationship((key, relationshipMeta) => {
// V2 API payload
let relationship = null;
let relationshipKey = this.keyForV2Relationship(key, relationshipMeta.kind, 'deserialize');
if (resourceHash.hasOwnProperty(relationshipKey)) {
let data = null;
let relationshipHash = resourceHash[relationshipKey];
if (relationshipMeta.kind === 'belongsTo') {
data = this.extractRelationship(relationshipMeta.type, relationshipHash);
} else if (relationshipMeta.kind === 'hasMany') {
data = relationshipHash.map((item) => this.extractRelationship(relationshipMeta.type, item));
}
relationship = { data };
}
if (relationship) {
relationships[key] = relationship;
}
});
return relationships;
}
},
keyForV2Relationship(key, typeClass, method) {
return key.underscore() + '_id';
}
});