File tree Expand file tree Collapse file tree 5 files changed +44
-17
lines changed Expand file tree Collapse file tree 5 files changed +44
-17
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,4 @@ export default Factory.extend({
6
6
id ( ) {
7
7
return this . keyword ;
8
8
} ,
9
-
10
- crates_cnt : 0 ,
11
9
} ) ;
Original file line number Diff line number Diff line change 1
1
export default [
2
2
{
3
- crates_cnt : 38 ,
4
3
created_at : '2014-11-23T06:47:40Z' ,
5
4
id : 'network' ,
6
5
keyword : 'network' ,
7
6
} ,
8
7
{
9
- crates_cnt : 1 ,
10
8
created_at : '2014-11-23T06:47:40Z' ,
11
9
id : 'rust' ,
12
10
keyword : 'rust' ,
13
11
} ,
14
12
{
15
- crates_cnt : 1 ,
16
13
created_at : '2014-11-23T06:47:40Z' ,
17
14
id : 'plugin' ,
18
15
keyword : 'plugin' ,
19
16
} ,
20
17
{
21
- crates_cnt : 1 ,
22
18
created_at : '2014-11-23T06:47:40Z' ,
23
19
id : 'code-generation' ,
24
20
keyword : 'code-generation' ,
25
21
} ,
26
22
{
27
- crates_cnt : 1 ,
28
23
created_at : '2014-11-23T06:47:40Z' ,
29
24
id : 'python' ,
30
25
keyword : 'python' ,
31
26
} ,
32
27
{
33
- crates_cnt : 1 ,
34
28
created_at : '2014-11-23T06:47:40Z' ,
35
29
id : 'ruby' ,
36
30
keyword : 'ruby' ,
37
31
} ,
38
32
{
39
- crates_cnt : 1 ,
40
33
created_at : '2014-11-23T06:47:40Z' ,
41
34
id : 'shell' ,
42
35
keyword : 'shell' ,
43
36
} ,
44
37
{
45
- crates_cnt : 1 ,
46
38
created_at : '2014-11-23T06:47:40Z' ,
47
39
id : 'string' ,
48
40
keyword : 'string' ,
49
41
} ,
50
42
{
51
- crates_cnt : 1 ,
52
43
created_at : '2014-11-23T06:47:40Z' ,
53
44
id : 'case' ,
54
45
keyword : 'case' ,
55
46
} ,
56
47
{
57
- crates_cnt : 1 ,
58
48
created_at : '2014-11-23T06:47:40Z' ,
59
49
id : 'camel' ,
60
50
keyword : 'camel' ,
61
51
} ,
62
52
{
63
- crates_cnt : 1 ,
64
53
created_at : '2014-11-23T06:47:40Z' ,
65
54
id : 'snake' ,
66
55
keyword : 'snake' ,
67
56
} ,
68
57
{
69
- crates_cnt : 1 ,
70
58
created_at : '2014-11-23T06:47:40Z' ,
71
59
id : 'inflection' ,
72
60
keyword : 'inflection' ,
73
61
} ,
74
62
{
75
- crates_cnt : 1 ,
76
63
created_at : '2014-11-23T06:47:40Z' ,
77
64
id : 'elastic' ,
78
65
keyword : 'elastic' ,
79
66
} ,
80
67
{
81
- crates_cnt : 1 ,
82
68
created_at : '2014-11-23T06:47:40Z' ,
83
69
id : 'elasticsearch' ,
84
70
keyword : 'elasticsearch' ,
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -314,7 +314,7 @@ module('Mirage | Keywords', function(hooks) {
314
314
assert . deepEqual ( responsePayload . crate . keywords , [ 'no-std' ] ) ;
315
315
assert . deepEqual ( responsePayload . keywords , [
316
316
{
317
- crates_cnt : 0 ,
317
+ crates_cnt : 1 ,
318
318
id : 'no-std' ,
319
319
keyword : 'no-std' ,
320
320
} ,
Original file line number Diff line number Diff line change @@ -107,5 +107,24 @@ module('Mirage | Keywords', function(hooks) {
107
107
} ,
108
108
} ) ;
109
109
} ) ;
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
+ } ) ;
110
129
} ) ;
111
130
} ) ;
You can’t perform that action at this time.
0 commit comments