Skip to content

Commit 1be7108

Browse files
committed
mirage/version: Add publishedBy relationship
1 parent 20de336 commit 1be7108

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

mirage/models/version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { belongsTo, Model } from 'ember-cli-mirage';
22

33
export default Model.extend({
44
crate: belongsTo(),
5+
publishedBy: belongsTo('user'),
56
});

mirage/serializers/version.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable ember/avoid-leaking-state-in-ember-objects */
2+
13
import BaseSerializer from './application';
24

35
export default BaseSerializer.extend({
@@ -15,6 +17,8 @@ export default BaseSerializer.extend({
1517
'crate_size',
1618
],
1719

20+
include: ['publishedBy'],
21+
1822
links(version) {
1923
return {
2024
authors: `/api/v1/crates/${version.crateId}/${version.num}/authors`,
@@ -28,18 +32,29 @@ export default BaseSerializer.extend({
2832

2933
if (Array.isArray(hash)) {
3034
for (let resource of hash) {
31-
this._adjust(resource);
35+
this._adjust(resource, addToIncludes);
3236
}
3337
} else {
34-
this._adjust(hash);
38+
this._adjust(hash, addToIncludes);
3539
}
3640

41+
addToIncludes = addToIncludes.filter(it => it.modelName !== 'user');
42+
3743
return [hash, addToIncludes];
3844
},
3945

40-
_adjust(hash) {
46+
_adjust(hash, includes) {
4147
hash.dl_path = `/api/v1/crates/${hash.crate_id}/${hash.num}/download`;
4248
hash.crate = hash.crate_id;
49+
50+
if (hash.published_by_id) {
51+
let user = includes.find(it => it.modelName === 'user' && it.id === hash.published_by_id);
52+
hash.published_by = this.getHashForIncludedResource(user)[0].users[0];
53+
} else {
54+
hash.published_by = null;
55+
}
56+
4357
delete hash.crate_id;
58+
delete hash.published_by_id;
4459
},
4560
});

tests/mirage/crates-test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ module('Mirage | Crates', function (hooks) {
270270
version_downloads: '/api/v1/crates/rand/1.0.0-beta.1/downloads',
271271
},
272272
num: '1.0.0-beta.1',
273+
published_by: null,
273274
updated_at: '2017-02-24T12:34:56Z',
274275
yanked: false,
275276
},
@@ -303,6 +304,7 @@ module('Mirage | Crates', function (hooks) {
303304
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
304305
},
305306
num: '1.0.0',
307+
published_by: null,
306308
updated_at: '2017-02-24T12:34:56Z',
307309
yanked: false,
308310
},
@@ -320,6 +322,7 @@ module('Mirage | Crates', function (hooks) {
320322
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
321323
},
322324
num: '1.1.0',
325+
published_by: null,
323326
updated_at: '2017-02-24T12:34:56Z',
324327
yanked: false,
325328
},
@@ -337,6 +340,7 @@ module('Mirage | Crates', function (hooks) {
337340
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
338341
},
339342
num: '1.2.0',
343+
published_by: null,
340344
updated_at: '2017-02-24T12:34:56Z',
341345
yanked: false,
342346
},
@@ -540,9 +544,10 @@ module('Mirage | Crates', function (hooks) {
540544
});
541545

542546
test('returns all versions belonging to the specified crate', async function (assert) {
547+
let user = this.server.create('user');
543548
this.server.create('crate', { name: 'rand' });
544549
this.server.create('version', { crateId: 'rand', num: '1.0.0' });
545-
this.server.create('version', { crateId: 'rand', num: '1.1.0' });
550+
this.server.create('version', { crateId: 'rand', num: '1.1.0', publishedBy: user });
546551
this.server.create('version', { crateId: 'rand', num: '1.2.0' });
547552

548553
let response = await fetch('/api/v1/crates/rand/versions');
@@ -565,6 +570,7 @@ module('Mirage | Crates', function (hooks) {
565570
version_downloads: '/api/v1/crates/rand/1.0.0/downloads',
566571
},
567572
num: '1.0.0',
573+
published_by: null,
568574
updated_at: '2017-02-24T12:34:56Z',
569575
yanked: false,
570576
},
@@ -582,6 +588,13 @@ module('Mirage | Crates', function (hooks) {
582588
version_downloads: '/api/v1/crates/rand/1.1.0/downloads',
583589
},
584590
num: '1.1.0',
591+
published_by: {
592+
id: 1,
593+
avatar: 'https://avatars1.githubusercontent.com/u/14631425?v=4',
594+
login: 'user-1',
595+
name: 'User 1',
596+
url: 'https://github.com/user-1',
597+
},
585598
updated_at: '2017-02-24T12:34:56Z',
586599
yanked: false,
587600
},
@@ -599,6 +612,7 @@ module('Mirage | Crates', function (hooks) {
599612
version_downloads: '/api/v1/crates/rand/1.2.0/downloads',
600613
},
601614
num: '1.2.0',
615+
published_by: null,
602616
updated_at: '2017-02-24T12:34:56Z',
603617
yanked: false,
604618
},
@@ -995,6 +1009,7 @@ module('Mirage | Crates', function (hooks) {
9951009
version_downloads: '/api/v1/crates/bar/1.0.0/downloads',
9961010
},
9971011
num: '1.0.0',
1012+
published_by: null,
9981013
updated_at: '2017-02-24T12:34:56Z',
9991014
yanked: false,
10001015
},
@@ -1012,6 +1027,7 @@ module('Mirage | Crates', function (hooks) {
10121027
version_downloads: '/api/v1/crates/baz/1.0.1/downloads',
10131028
},
10141029
num: '1.0.1',
1030+
published_by: null,
10151031
updated_at: '2017-02-24T12:34:56Z',
10161032
yanked: false,
10171033
},

tests/mirage/me-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ module('Mirage | /me', function (hooks) {
241241
version_downloads: '/api/v1/crates/foo/1.2.3/downloads',
242242
},
243243
num: '1.2.3',
244+
published_by: null,
244245
updated_at: '2017-02-24T12:34:56Z',
245246
yanked: false,
246247
},

0 commit comments

Comments
 (0)