Include repositoryId as an attribute on build

For some reason (probably some problem with one of the serializers) we
sometimes lack an id attribute for a promise that we get for a repo
relationship on build. Because of that doing `build.get('repo.id')` may
sometimes return undefined. A temporary workaround is to make sure that
we always can access the `repository_id` property.
This commit is contained in:
Piotr Sarnacki 2015-11-10 17:52:18 +01:00
parent 3946077c96
commit d59e402314
3 changed files with 22 additions and 2 deletions

View File

@ -19,6 +19,7 @@ Build = Model.extend DurationCalculations,
pullRequestTitle: DS.attr()
pullRequestNumber: DS.attr('number')
eventType: DS.attr('string')
repositoryId: DS.attr('number')
repo: DS.belongsTo('repo', async: true)
commit: DS.belongsTo('commit')

View File

@ -35,7 +35,7 @@ Repo = Model.extend
builds: (->
id = @get('id')
builds = @store.filter('build', event_type: ['push', 'api'], repository_id: id, (b) ->
b.get('repo.id') == id && (b.get('eventType') == 'push' || b.get('eventType') == 'api')
b.get('repositoryId')+'' == id+'' && (b.get('eventType') == 'push' || b.get('eventType') == 'api')
)
# TODO: move to controller
@ -52,7 +52,7 @@ Repo = Model.extend
pullRequests: (->
id = @get('id')
builds = @store.filter('build', event_type: 'pull_request', repository_id: id, (b) ->
b.get('repo.id') == id && b.get('eventType') == 'pull_request'
b.get('repositoryId')+'' == id+'' && b.get('eventType') == 'pull_request'
)
# TODO: move to controller

View File

@ -16,4 +16,23 @@ Serializer = V2FallbackSerializer.extend
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`