Skip to content

Commit 1f917ad

Browse files
committed
mirage/crate: Replace versions property with relationship
1 parent 8cec74f commit 1f917ad

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

mirage/factories/crate.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default Factory.extend({
2323
updated_at: '2017-02-24T12:34:56Z',
2424

2525
badges: () => [],
26-
versions: () => [],
2726
_extra_downloads: () => [],
2827
_owner_teams: () => [],
2928
_owner_users: () => [],

mirage/factories/version.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ export default Factory.extend({
1818

1919
crate_size: i => (((i + 13) * 42) % 13) * 54321,
2020

21-
afterCreate(version, server) {
21+
afterCreate(version) {
2222
if (!version.crateId) {
2323
throw new Error(`Missing \`crate\` relationship on \`version:${version.num}\``);
2424
}
25-
26-
let crate = server.schema.crates.find(version.crateId);
27-
crate.update({ versions: crate.versions.concat(parseInt(version.id, 10)) });
2825
},
2926
});

mirage/fixtures/crates.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default [
2121
name: 'kinetic-rust',
2222
repository: 'https://github.com/icorderi/kinetic-rust/',
2323
updated_at: '2015-04-21T00:15:49Z',
24-
versions: null,
24+
versionIds: [],
2525
},
2626
{
2727
badges: [
@@ -46,7 +46,7 @@ export default [
4646
name: 'nanomsg',
4747
repository: 'https://github.com/thehydroimpulse/nanomsg.rs',
4848
updated_at: '2016-12-28T08:40:00Z',
49-
versions: [40906, 40905, 28431, 21273, 18445, 17384, 13574, 9014, 8236, 7190, 4944, 940, 924],
49+
versionIds: [40906, 40905, 28431, 21273, 18445, 17384, 13574, 9014, 8236, 7190, 4944, 940, 924],
5050
_extra_downloads: [
5151
{
5252
date: '2017-02-02',
@@ -132,7 +132,7 @@ export default [
132132
badge_type: 'coveralls',
133133
},
134134
],
135-
versions: null,
135+
versionIds: [],
136136
},
137137
{
138138
created_at: '2015-02-27T11:51:58Z',
@@ -149,7 +149,7 @@ export default [
149149
name: 'external_mixin',
150150
repository: 'https://github.com/huonw/external_mixin',
151151
updated_at: '2015-02-27T11:51:58Z',
152-
versions: null,
152+
versionIds: [],
153153
},
154154
{
155155
created_at: '2015-02-27T11:51:40Z',
@@ -165,7 +165,7 @@ export default [
165165
name: 'external_mixin_umbrella',
166166
repository: 'https://github.com/huonw/external_mixin',
167167
updated_at: '2015-02-27T11:52:30Z',
168-
versions: null,
168+
versionIds: [],
169169
},
170170
{
171171
created_at: '2015-10-10T15:26:24Z',
@@ -182,7 +182,7 @@ export default [
182182
name: 'Inflector',
183183
repository: 'https://github.com/whatisinternet/inflector',
184184
updated_at: '2015-10-27T01:51:42Z',
185-
versions: null,
185+
versionIds: [],
186186
},
187187
{
188188
created_at: '2015-05-21T17:43:38Z',
@@ -198,7 +198,7 @@ export default [
198198
name: 'rs-es',
199199
repository: 'https://github.com/benashford/rs-es',
200200
updated_at: '2015-09-09T15:34:50Z',
201-
versions: null,
201+
versionIds: [],
202202
},
203203
{
204204
created_at: '2014-11-21T05:12:08Z',
@@ -214,7 +214,7 @@ export default [
214214
name: 'rust-crypto',
215215
repository: 'https://github.com/DaGenix/rust-crypto/',
216216
updated_at: '2015-10-29T01:16:17Z',
217-
versions: null,
217+
versionIds: [],
218218
},
219219
{
220220
created_at: '2015-03-20T13:46:04Z',
@@ -230,7 +230,7 @@ export default [
230230
name: 'rust-htslib',
231231
repository: 'https://github.com/rust-bio/rust-htslib.git',
232232
updated_at: '2015-11-11T00:10:43Z',
233-
versions: null,
233+
versionIds: [],
234234
},
235235
{
236236
created_at: '2014-11-29T17:51:55Z',
@@ -246,7 +246,7 @@ export default [
246246
name: 'rustless',
247247
repository: 'https://crates.io/crates/rustless',
248248
updated_at: '2015-10-31T11:49:29Z',
249-
versions: null,
249+
versionIds: [],
250250
},
251251
{
252252
created_at: '2014-12-05T20:20:39Z',
@@ -262,7 +262,7 @@ export default [
262262
name: 'serde',
263263
repository: 'https://github.com/serde-rs/serde',
264264
updated_at: '2015-10-18T03:10:21Z',
265-
versions: null,
265+
versionIds: [],
266266
},
267267
{
268268
created_at: '2015-08-26T13:50:58Z',
@@ -278,7 +278,7 @@ export default [
278278
name: 'rusted_cypher',
279279
repository: 'https://github.com/livioribeiro/rusted-cypher',
280280
updated_at: '2015-11-07T17:26:55Z',
281-
versions: null,
281+
versionIds: [],
282282
},
283283
{
284284
created_at: '2015-01-02T20:54:04Z',
@@ -295,7 +295,7 @@ export default [
295295
name: 'zlib',
296296
repository: null,
297297
updated_at: '2015-01-02T20:54:04Z',
298-
versions: null,
298+
versionIds: [],
299299
},
300300
{
301301
created_at: '2015-05-08T19:34:16Z',
@@ -312,7 +312,7 @@ export default [
312312
name: 'rustful',
313313
repository: 'https://github.com/Ogeon/rustful',
314314
updated_at: '2015-09-19T21:10:27Z',
315-
versions: null,
315+
versionIds: [],
316316
},
317317
{
318318
created_at: '2014-11-24T02:34:44Z',
@@ -328,7 +328,7 @@ export default [
328328
name: 'postgres',
329329
repository: 'https://github.com/sfackler/rust-postgres',
330330
updated_at: '2015-11-08T00:48:59Z',
331-
versions: null,
331+
versionIds: [],
332332
},
333333
{
334334
created_at: '2014-11-21T00:20:47Z',
@@ -344,7 +344,7 @@ export default [
344344
name: 'quickcheck',
345345
repository: 'https://github.com/BurntSushi/quickcheck',
346346
updated_at: '2015-09-20T21:53:38Z',
347-
versions: null,
347+
versionIds: [],
348348
},
349349
{
350350
created_at: '2014-11-21T00:21:04Z',
@@ -360,7 +360,7 @@ export default [
360360
name: 'quickcheck_macros',
361361
repository: 'https://github.com/BurntSushi/quickcheck',
362362
updated_at: '2015-09-20T21:53:57Z',
363-
versions: null,
363+
versionIds: [],
364364
},
365365
{
366366
created_at: '2015-08-25T19:15:35Z',
@@ -377,7 +377,7 @@ export default [
377377
name: 'unicorn-rpc',
378378
repository: 'https://github.com/nicolas-cherel/rustlex',
379379
updated_at: '2015-08-25T19:15:35Z',
380-
versions: null,
380+
versionIds: [],
381381
},
382382
{
383383
created_at: '2015-01-17T17:47:52Z',
@@ -393,7 +393,7 @@ export default [
393393
name: 'nom',
394394
repository: 'https://github.com/Geal/nom',
395395
updated_at: '2015-11-22T22:00:41Z',
396-
versions: null,
396+
versionIds: [],
397397
},
398398
{
399399
id: 'libc',

mirage/models/crate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { Model, hasMany } from 'ember-cli-mirage';
33
export default Model.extend({
44
categories: hasMany(),
55
keywords: hasMany(),
6+
versions: hasMany(),
67
});

mirage/route-handlers/crates.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ export function register(server) {
3838
let crate = schema.crates.find(crateId);
3939
if (!crate) return notFound();
4040

41-
let versions = schema.versions
42-
.all()
43-
.filter(version => (crate.versions || []).indexOf(parseInt(version.id, 10)) !== -1);
44-
4541
return {
4642
...this.serialize(crate),
4743
...this.serialize(crate.categories),
4844
...this.serialize(crate.keywords),
49-
...this.serialize(versions),
45+
...this.serialize(crate.versions),
5046
};
5147
});
5248

@@ -59,7 +55,7 @@ export function register(server) {
5955
let crate = schema.crates.find(crateId);
6056
if (!crate) return notFound();
6157

62-
return schema.versions.where({ crateId }).sort((a, b) => compareIsoDates(b.created_at, a.created_at));
58+
return crate.versions.sort((a, b) => compareIsoDates(b.created_at, a.created_at));
6359
});
6460

6561
server.get('/api/v1/crates/:crate_id/:version_num/authors', (schema, request) => {
@@ -155,9 +151,7 @@ export function register(server) {
155151
let crate = schema.crates.find(crateId);
156152
if (!crate) return notFound();
157153

158-
let versionDownloads = schema.versionDownloads
159-
.all()
160-
.filter(it => crate.versions.indexOf(parseInt(it.versionId, 10)) !== -1);
154+
let versionDownloads = schema.versionDownloads.all().filter(it => it.version.crateId === crateId);
161155

162156
return withMeta(this.serialize(versionDownloads), { extra_downloads: crate._extra_downloads });
163157
});

mirage/serializers/crate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,8 @@ export default BaseSerializer.extend({
5151

5252
hash.keywords = hash.keyword_ids;
5353
delete hash.keyword_ids;
54+
55+
hash.versions = hash.version_ids;
56+
delete hash.version_ids;
5457
},
5558
});

tests/mirage/crates-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ module('Mirage | Keywords', function(hooks) {
224224
assert.equal(response.status, 200);
225225

226226
let responsePayload = await response.json();
227+
assert.deepEqual(responsePayload.crate.versions, ['1', '2', '3']);
227228
assert.deepEqual(responsePayload.versions, [
228229
{
229230
id: '1',

0 commit comments

Comments
 (0)