@@ -4,16 +4,18 @@ module ActiveModel
4
4
class Serializer
5
5
class MetaTest < Minitest ::Test
6
6
def setup
7
- ActionController ::Base . cache_store . clear
8
7
@blog = Blog . new ( id : 1 ,
9
8
name : 'AMS Hints' ,
10
9
writer : Author . new ( id : 2 , name : 'Steve' ) ,
11
10
articles : [ Post . new ( id : 3 , title : 'AMS' ) ] )
12
11
end
13
12
14
13
def test_meta_is_present_with_root
15
- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } )
16
- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
14
+ actual = ActiveModel ::SerializableResource . new ( @blog ,
15
+ adapter : :json ,
16
+ serializer : AlternateBlogSerializer ,
17
+ meta : { total : 10 } )
18
+ . as_json
17
19
expected = {
18
20
blog : {
19
21
id : 1 ,
@@ -23,22 +25,29 @@ def test_meta_is_present_with_root
23
25
total : 10
24
26
}
25
27
}
26
- assert_equal expected , adapter . as_json
28
+ assert_equal expected , actual
27
29
end
28
30
29
31
def test_meta_is_not_included_when_root_is_missing
30
- # load_adapter uses Attributes Adapter
31
- adapter = load_adapter ( meta : { total : 10 } )
32
+ actual = ActiveModel ::SerializableResource . new ( @blog ,
33
+ adapter : :attributes ,
34
+ serializer : AlternateBlogSerializer ,
35
+ meta : { total : 10 } )
36
+ . as_json
32
37
expected = {
33
38
id : 1 ,
34
39
title : 'AMS Hints'
35
40
}
36
- assert_equal expected , adapter . as_json
41
+ assert_equal expected , actual
37
42
end
38
43
39
44
def test_meta_key_is_used
40
- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } , meta_key : 'haha_meta' )
41
- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
45
+ actual = ActiveModel ::SerializableResource . new ( @blog ,
46
+ adapter : :json ,
47
+ serializer : AlternateBlogSerializer ,
48
+ meta : { total : 10 } ,
49
+ meta_key : 'haha_meta' )
50
+ . as_json
42
51
expected = {
43
52
blog : {
44
53
id : 1 ,
@@ -48,12 +57,16 @@ def test_meta_key_is_used
48
57
total : 10
49
58
}
50
59
}
51
- assert_equal expected , adapter . as_json
60
+ assert_equal expected , actual
52
61
end
53
62
54
63
def test_meta_key_is_used_with_json_api
55
- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } , meta_key : 'haha_meta' )
56
- adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
64
+ actual = ActiveModel ::SerializableResource . new ( @blog ,
65
+ adapter : :json_api ,
66
+ serializer : AlternateBlogSerializer ,
67
+ meta : { total : 10 } ,
68
+ meta_key : 'haha_meta' )
69
+ . as_json
57
70
expected = {
58
71
data : {
59
72
id : '1' ,
@@ -62,13 +75,14 @@ def test_meta_key_is_used_with_json_api
62
75
} ,
63
76
'haha_meta' => { total : 10 }
64
77
}
65
- assert_equal expected , adapter . as_json
78
+ assert_equal expected , actual
66
79
end
67
80
68
81
def test_meta_is_not_present_on_arrays_without_root
69
- serializer = ArraySerializer . new ( [ @blog ] , meta : { total : 10 } )
70
- # Attributes doesn't have support to root
71
- adapter = ActiveModel ::Serializer ::Adapter ::Attributes . new ( serializer )
82
+ actual = ActiveModel ::SerializableResource . new ( [ @blog ] ,
83
+ adapter : :attributes ,
84
+ meta : { total : 10 } )
85
+ . as_json
72
86
expected = [ {
73
87
id : 1 ,
74
88
name : 'AMS Hints' ,
@@ -82,13 +96,15 @@ def test_meta_is_not_present_on_arrays_without_root
82
96
body : nil
83
97
} ]
84
98
} ]
85
- assert_equal expected , adapter . as_json
99
+ assert_equal expected , actual
86
100
end
87
101
88
102
def test_meta_is_present_on_arrays_with_root
89
- serializer = ArraySerializer . new ( [ @blog ] , meta : { total : 10 } , meta_key : 'haha_meta' )
90
- # JSON adapter adds root by default
91
- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
103
+ actual = ActiveModel ::SerializableResource . new ( [ @blog ] ,
104
+ adapter : :json ,
105
+ meta : { total : 10 } ,
106
+ meta_key : 'haha_meta' )
107
+ . as_json
92
108
expected = {
93
109
blogs : [ {
94
110
id : 1 ,
@@ -107,14 +123,7 @@ def test_meta_is_present_on_arrays_with_root
107
123
total : 10
108
124
}
109
125
}
110
- assert_equal expected , adapter . as_json
111
- end
112
-
113
- private
114
-
115
- def load_adapter ( options )
116
- options = options . merge ( adapter : :attributes , serializer : AlternateBlogSerializer )
117
- ActiveModel ::SerializableResource . new ( @blog , options )
126
+ assert_equal expected , actual
118
127
end
119
128
end
120
129
end
0 commit comments