Skip to content

Commit 5f802cd

Browse files
committed
mirage/keywords: Calculate crates_cnt property
1 parent 6cafe0d commit 5f802cd

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

mirage/factories/keyword.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ export default Factory.extend({
66
id() {
77
return this.keyword;
88
},
9-
10-
crates_cnt: 0,
119
});

mirage/fixtures/keywords.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,70 @@
11
export default [
22
{
3-
crates_cnt: 38,
43
created_at: '2014-11-23T06:47:40Z',
54
id: 'network',
65
keyword: 'network',
76
},
87
{
9-
crates_cnt: 1,
108
created_at: '2014-11-23T06:47:40Z',
119
id: 'rust',
1210
keyword: 'rust',
1311
},
1412
{
15-
crates_cnt: 1,
1613
created_at: '2014-11-23T06:47:40Z',
1714
id: 'plugin',
1815
keyword: 'plugin',
1916
},
2017
{
21-
crates_cnt: 1,
2218
created_at: '2014-11-23T06:47:40Z',
2319
id: 'code-generation',
2420
keyword: 'code-generation',
2521
},
2622
{
27-
crates_cnt: 1,
2823
created_at: '2014-11-23T06:47:40Z',
2924
id: 'python',
3025
keyword: 'python',
3126
},
3227
{
33-
crates_cnt: 1,
3428
created_at: '2014-11-23T06:47:40Z',
3529
id: 'ruby',
3630
keyword: 'ruby',
3731
},
3832
{
39-
crates_cnt: 1,
4033
created_at: '2014-11-23T06:47:40Z',
4134
id: 'shell',
4235
keyword: 'shell',
4336
},
4437
{
45-
crates_cnt: 1,
4638
created_at: '2014-11-23T06:47:40Z',
4739
id: 'string',
4840
keyword: 'string',
4941
},
5042
{
51-
crates_cnt: 1,
5243
created_at: '2014-11-23T06:47:40Z',
5344
id: 'case',
5445
keyword: 'case',
5546
},
5647
{
57-
crates_cnt: 1,
5848
created_at: '2014-11-23T06:47:40Z',
5949
id: 'camel',
6050
keyword: 'camel',
6151
},
6252
{
63-
crates_cnt: 1,
6453
created_at: '2014-11-23T06:47:40Z',
6554
id: 'snake',
6655
keyword: 'snake',
6756
},
6857
{
69-
crates_cnt: 1,
7058
created_at: '2014-11-23T06:47:40Z',
7159
id: 'inflection',
7260
keyword: 'inflection',
7361
},
7462
{
75-
crates_cnt: 1,
7663
created_at: '2014-11-23T06:47:40Z',
7764
id: 'elastic',
7865
keyword: 'elastic',
7966
},
8067
{
81-
crates_cnt: 1,
8268
created_at: '2014-11-23T06:47:40Z',
8369
id: 'elasticsearch',
8470
keyword: 'elasticsearch',

mirage/serializers/keyword.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import BaseSerializer from './application';
2+
3+
export default BaseSerializer.extend({
4+
getHashForResource() {
5+
let [hash, addToIncludes] = BaseSerializer.prototype.getHashForResource.apply(this, arguments);
6+
7+
if (Array.isArray(hash)) {
8+
for (let resource of hash) {
9+
this._adjust(resource);
10+
}
11+
} else {
12+
this._adjust(hash);
13+
}
14+
15+
return [hash, addToIncludes];
16+
},
17+
18+
_adjust(hash) {
19+
let allCrates = this.schema.crates.all();
20+
let associatedCrates = allCrates.filter(it => it.keywordIds.includes(hash.id));
21+
22+
hash.crates_cnt = associatedCrates.length;
23+
},
24+
});

tests/mirage/crates-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ module('Mirage | Keywords', function(hooks) {
314314
assert.deepEqual(responsePayload.crate.keywords, ['no-std']);
315315
assert.deepEqual(responsePayload.keywords, [
316316
{
317-
crates_cnt: 0,
317+
crates_cnt: 1,
318318
id: 'no-std',
319319
keyword: 'no-std',
320320
},

tests/mirage/keywords-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,24 @@ module('Mirage | Keywords', function(hooks) {
107107
},
108108
});
109109
});
110+
111+
test('calculates `crates_cnt` correctly', async function(assert) {
112+
this.server.create('keyword', { keyword: 'cli' });
113+
this.server.createList('crate', 7, { keywordIds: ['cli'] });
114+
this.server.create('keyword', { keyword: 'not-cli' });
115+
this.server.createList('crate', 3, { keywordIds: ['not-cli'] });
116+
117+
let response = await fetch('/api/v1/keywords/cli');
118+
assert.equal(response.status, 200);
119+
120+
let responsePayload = await response.json();
121+
assert.deepEqual(responsePayload, {
122+
keyword: {
123+
id: 'cli',
124+
crates_cnt: 7,
125+
keyword: 'cli',
126+
},
127+
});
128+
});
110129
});
111130
});

0 commit comments

Comments
 (0)