Skip to content

Commit 9cc23fa

Browse files
committed
Auto merge of #2137 - Turbo87:relationships, r=locks
mirage: Use relationship system see https://www.ember-cli-mirage.com/docs/data-layer/relationships Until now we've been keeping track of the relationships between the mirage resources manually, but mirage has a built-in relationship system for this that can simplify the code quite a bit when used correctly. This PR is replacing this manual tracking with declarative relationships. r? @locks
2 parents 63d45b6 + bf34146 commit 9cc23fa

21 files changed

+369
-201
lines changed

mirage/factories/crate.js

-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,5 @@ export default Factory.extend({
2323
updated_at: '2017-02-24T12:34:56Z',
2424

2525
badges: () => [],
26-
categories: () => [],
27-
keywords: () => [],
28-
versions: () => [],
2926
_extra_downloads: () => [],
30-
_owner_teams: () => [],
31-
_owner_users: () => [],
3227
});

mirage/factories/dependency.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import { Factory } from 'ember-cli-mirage';
33
const REQS = ['^0.1.0', '^2.1.3', '0.3.7', '~5.2.12'];
44

55
export default Factory.extend({
6-
// crate_id,
7-
// version_id,
8-
96
default_features: i => i % 4 === 3,
107
features: () => [],
118
kind: i => (i % 3 === 0 ? 'dev' : 'normal'),
129
optional: i => i % 4 !== 3,
1310
req: i => REQS[i % REQS.length],
1411
target: null,
12+
13+
afterCreate(self) {
14+
if (!self.crateId) {
15+
throw new Error(`Missing \`crate\` relationship on \`dependency:${self.id}\``);
16+
}
17+
if (!self.versionId) {
18+
throw new Error(`Missing \`version\` relationship on \`dependency:${self.id}\``);
19+
}
20+
},
1521
});

mirage/factories/version-download.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { Factory } from 'ember-cli-mirage';
22

33
export default Factory.extend({
4-
// version
5-
64
date: '2019-05-21',
75
downloads: i => (((i * 42) % 13) + 4) * 2345,
6+
7+
afterCreate(self) {
8+
if (!self.versionId) {
9+
throw new Error(`Missing \`version\` relationship on \`version-download:${self.date}\``);
10+
}
11+
},
812
});

mirage/factories/version.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ export default Factory.extend({
1111
yanked: false,
1212
license: i => LICENSES[i % LICENSES.length],
1313

14-
dl_path() {
15-
return `/api/v1/crates/${this.crate}/${this.num}/download`;
16-
},
17-
1814
downloads: i => (((i + 13) * 42) % 13) * 1234,
1915

2016
features: () => {},
2117
_authors: () => [],
2218

2319
crate_size: i => (((i + 13) * 42) % 13) * 54321,
2420

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

0 commit comments

Comments
 (0)