From a2bd6d827d3451e8f143f0d07ae57df573cbb56b Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 11 Nov 2015 11:52:25 +0100 Subject: [PATCH] Convert build serializer to javascript --- app/serializers/build.coffee | 65 ---------------------------- app/serializers/build.js | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 65 deletions(-) delete mode 100644 app/serializers/build.coffee create mode 100644 app/serializers/build.js diff --git a/app/serializers/build.coffee b/app/serializers/build.coffee deleted file mode 100644 index 18f17076..00000000 --- a/app/serializers/build.coffee +++ /dev/null @@ -1,65 +0,0 @@ -`import Ember from 'ember'` -`import V2FallbackSerializer from 'travis/serializers/v2_fallback'` - -Serializer = V2FallbackSerializer.extend - isNewSerializerAPI: true - attrs: { - _config: { key: 'config' } - _finishedAt: { key: 'finished_at' } - _startedAt: { key: 'started_at' } - _duration: { key: 'duration' } - } - - extractRelationships: (modelClass, resourceHash) -> - result = @_super(modelClass, resourceHash) - result - - normalizeArrayResponse: (store, primaryModelClass, payload, id, requestType) -> - if payload.commits - payload.builds.forEach (build) -> - commit_id = build.commit_id - if commit = payload.commits.findBy('id', commit_id) - build.commit = commit - delete build.commit_id - - result = @_super.apply(this, arguments) - store = this.store - # TODO: probably it should be done for all of the relationships, not - # only commit - result.data.forEach (item) -> - if item.relationships && item.relationships.commit - serializer = store.serializerFor 'commit' - modelClass = store.modelFor 'commit' - normalized = serializer.normalize(modelClass, item.relationships.commit.data) - result.included.push normalized.data - - result - - keyForV2Relationship: (key, typeClass, method) -> - if key == 'repo' - 'repository_id' - else if key == 'commit' - key - else - @_super.apply(this, arguments) - - normalize: (modelClass, resourceHash) -> - result = @_super(modelClass, resourceHash) - - data = result.data - - # for some reasone repo's promise not always has its id set - # so we can't always do build.get('repo.id') - # this ensures that repositoryId is always reachable on build - # TODO: look into the real cause of the problem (maybe it will be gone - # after fully switching to V3 and/or updating ember-data) - if repoId = resourceHash.repository_id - data.attributes.repositoryId = repoId - else if resourceHash.repository - if href = resourceHash.repository['@href'] - id = href.match(/\d+/)[0] - data.attributes.repositoryId = id - - return result - -`export default Serializer` diff --git a/app/serializers/build.js b/app/serializers/build.js new file mode 100644 index 00000000..9296836e --- /dev/null +++ b/app/serializers/build.js @@ -0,0 +1,82 @@ +import Ember from 'ember'; +import V2FallbackSerializer from 'travis/serializers/v2_fallback'; + +var Serializer = V2FallbackSerializer.extend({ + isNewSerializerAPI: true, + attrs: { + _config: { + key: 'config' + }, + _finishedAt: { + key: 'finished_at' + }, + _startedAt: { + key: 'started_at' + }, + _duration: { + key: 'duration' + } + }, + + extractRelationships: function(modelClass, resourceHash) { + var result; + result = this._super(modelClass, resourceHash); + return result; + }, + + normalizeArrayResponse: function(store, primaryModelClass, payload, id, requestType) { + var result; + if (payload.commits) { + payload.builds.forEach(function(build) { + var commit, commit_id; + commit_id = build.commit_id; + if (commit = payload.commits.findBy('id', commit_id)) { + build.commit = commit; + return delete build.commit_id; + } + }); + } + result = this._super.apply(this, arguments); + store = this.store; + result.data.forEach(function(item) { + var modelClass, normalized, serializer; + if (item.relationships && item.relationships.commit) { + serializer = store.serializerFor('commit'); + modelClass = store.modelFor('commit'); + normalized = serializer.normalize(modelClass, item.relationships.commit.data); + return result.included.push(normalized.data); + } + }); + return result; + }, + + keyForV2Relationship: function(key, typeClass, method) { + if (key === 'repo') { + return 'repository_id'; + } else if (key === 'commit') { + return key; + } else { + return this._super.apply(this, arguments); + } + }, + + normalize: function(modelClass, resourceHash) { + var data, href, id, repoId, result; + + result = this._super(modelClass, resourceHash); + + data = result.data; + + if (repoId = resourceHash.repository_id) { + data.attributes.repositoryId = repoId; + } else if (resourceHash.repository) { + if (href = resourceHash.repository['@href']) { + id = href.match(/\d+/)[0]; + data.attributes.repositoryId = id; + } + } + return result; + } +}); + +export default Serializer;