cheap_ams 0.10.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +13 -0
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +35 -0
- data/LICENSE.txt +22 -0
- data/README.md +348 -0
- data/Rakefile +12 -0
- data/appveyor.yml +25 -0
- data/cheap_ams.gemspec +49 -0
- data/docs/README.md +27 -0
- data/docs/general/adapters.md +51 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/lib/action_controller/serialization.rb +62 -0
- data/lib/active_model/serializable_resource.rb +84 -0
- data/lib/active_model/serializer/adapter/flatten_json.rb +19 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +82 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +16 -0
- data/lib/active_model/serializer/adapter/json.rb +53 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +24 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +183 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +98 -0
- data/lib/active_model/serializer/array_serializer.rb +35 -0
- data/lib/active_model/serializer/association.rb +21 -0
- data/lib/active_model/serializer/associations.rb +97 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +14 -0
- data/lib/active_model/serializer/fieldset.rb +42 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/lint.rb +131 -0
- data/lib/active_model/serializer/railtie.rb +9 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model/serializer.rb +205 -0
- data/lib/active_model_serializers.rb +29 -0
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +37 -0
- data/lib/generators/serializer/templates/serializer.rb +8 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api/linked_test.rb +180 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/serialization_scope_name_test.rb +67 -0
- data/test/action_controller/serialization_test.rb +426 -0
- data/test/adapter/fragment_cache_test.rb +37 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +82 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +38 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +52 -0
- data/test/array_serializer_test.rb +97 -0
- data/test/capture_warnings.rb +57 -0
- data/test/fixtures/active_record.rb +57 -0
- data/test/fixtures/poro.rb +266 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +56 -0
- data/test/lint_test.rb +44 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +150 -0
- data/test/serializers/attribute_test.rb +62 -0
- data/test/serializers/attributes_test.rb +63 -0
- data/test/serializers/cache_test.rb +164 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/meta_test.rb +121 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/support/rails_app.rb +21 -0
- data/test/support/stream_capture.rb +49 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +41 -0
- metadata +287 -0
@@ -0,0 +1,283 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
class Adapter
|
5
|
+
class JsonApi
|
6
|
+
class LinkedTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
ActionController::Base.cache_store.clear
|
9
|
+
@author1 = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
11
|
+
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
12
|
+
@bio2 = Bio.new(id: 2, content: 'Rails Contributor')
|
13
|
+
@first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
|
14
|
+
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
|
15
|
+
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
|
16
|
+
@blog = Blog.new({ name: 'AMS Blog' })
|
17
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
18
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
19
|
+
@first_post.blog = @blog
|
20
|
+
@second_post.blog = @blog
|
21
|
+
@third_post.blog = nil
|
22
|
+
@first_post.comments = [@first_comment, @second_comment]
|
23
|
+
@second_post.comments = []
|
24
|
+
@third_post.comments = []
|
25
|
+
@first_post.author = @author1
|
26
|
+
@second_post.author = @author2
|
27
|
+
@third_post.author = @author1
|
28
|
+
@first_comment.post = @first_post
|
29
|
+
@first_comment.author = nil
|
30
|
+
@second_comment.post = @first_post
|
31
|
+
@second_comment.author = nil
|
32
|
+
@author1.posts = [@first_post, @third_post]
|
33
|
+
@author1.bio = @bio1
|
34
|
+
@author1.roles = []
|
35
|
+
@author2.posts = [@second_post]
|
36
|
+
@author2.bio = @bio2
|
37
|
+
@author2.roles = []
|
38
|
+
@bio1.author = @author1
|
39
|
+
@bio2.author = @author2
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_include_multiple_posts_and_linked_array
|
43
|
+
serializer = ArraySerializer.new([@first_post, @second_post])
|
44
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
45
|
+
serializer,
|
46
|
+
include: ['author', 'author.bio', 'comments']
|
47
|
+
)
|
48
|
+
alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
49
|
+
serializer,
|
50
|
+
include: 'author,author.bio,comments'
|
51
|
+
)
|
52
|
+
|
53
|
+
expected = {
|
54
|
+
data: [
|
55
|
+
{
|
56
|
+
id: "10",
|
57
|
+
type: "posts",
|
58
|
+
attributes: {
|
59
|
+
title: "Hello!!",
|
60
|
+
body: "Hello, world!!"
|
61
|
+
},
|
62
|
+
relationships: {
|
63
|
+
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
64
|
+
blog: { data: { type: "blogs", id: "999" } },
|
65
|
+
author: { data: { type: "authors", id: "1" } }
|
66
|
+
}
|
67
|
+
},
|
68
|
+
{
|
69
|
+
id: "20",
|
70
|
+
type: "posts",
|
71
|
+
attributes: {
|
72
|
+
title: "New Post",
|
73
|
+
body: "Body"
|
74
|
+
},
|
75
|
+
relationships: {
|
76
|
+
comments: { data: [] },
|
77
|
+
blog: { data: { type: "blogs", id: "999" } },
|
78
|
+
author: { data: { type: "authors", id: "2" } }
|
79
|
+
}
|
80
|
+
}
|
81
|
+
],
|
82
|
+
included: [
|
83
|
+
{
|
84
|
+
id: "1",
|
85
|
+
type: "comments",
|
86
|
+
attributes: {
|
87
|
+
body: "ZOMG A COMMENT"
|
88
|
+
},
|
89
|
+
relationships: {
|
90
|
+
post: { data: { type: "posts", id: "10" } },
|
91
|
+
author: { data: nil }
|
92
|
+
}
|
93
|
+
}, {
|
94
|
+
id: "2",
|
95
|
+
type: "comments",
|
96
|
+
attributes: {
|
97
|
+
body: "ZOMG ANOTHER COMMENT",
|
98
|
+
},
|
99
|
+
relationships: {
|
100
|
+
post: { data: { type: "posts", id: "10" } },
|
101
|
+
author: { data: nil }
|
102
|
+
}
|
103
|
+
}, {
|
104
|
+
id: "1",
|
105
|
+
type: "authors",
|
106
|
+
attributes: {
|
107
|
+
name: "Steve K."
|
108
|
+
},
|
109
|
+
relationships: {
|
110
|
+
posts: { data: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
|
111
|
+
roles: { data: [] },
|
112
|
+
bio: { data: { type: "bios", id: "1" } }
|
113
|
+
}
|
114
|
+
}, {
|
115
|
+
id: "1",
|
116
|
+
type: "bios",
|
117
|
+
attributes: {
|
118
|
+
content: "AMS Contributor",
|
119
|
+
rating: nil
|
120
|
+
},
|
121
|
+
relationships: {
|
122
|
+
author: { data: { type: "authors", id: "1" } }
|
123
|
+
}
|
124
|
+
}, {
|
125
|
+
id: "2",
|
126
|
+
type: "authors",
|
127
|
+
attributes: {
|
128
|
+
name: "Tenderlove"
|
129
|
+
},
|
130
|
+
relationships: {
|
131
|
+
posts: { data: [ { type: "posts", id:"20" } ] },
|
132
|
+
roles: { data: [] },
|
133
|
+
bio: { data: { type: "bios", id: "2" } }
|
134
|
+
}
|
135
|
+
}, {
|
136
|
+
id: "2",
|
137
|
+
type: "bios",
|
138
|
+
attributes: {
|
139
|
+
rating: nil,
|
140
|
+
content: "Rails Contributor",
|
141
|
+
},
|
142
|
+
relationships: {
|
143
|
+
author: { data: { type: "authors", id: "2" } }
|
144
|
+
}
|
145
|
+
}
|
146
|
+
]
|
147
|
+
}
|
148
|
+
assert_equal expected, adapter.serializable_hash
|
149
|
+
assert_equal expected, alt_adapter.serializable_hash
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_include_multiple_posts_and_linked
|
153
|
+
serializer = BioSerializer.new @bio1
|
154
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
155
|
+
serializer,
|
156
|
+
include: ['author', 'author.posts']
|
157
|
+
)
|
158
|
+
alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
159
|
+
serializer,
|
160
|
+
include: 'author,author.posts'
|
161
|
+
)
|
162
|
+
|
163
|
+
expected = [
|
164
|
+
{
|
165
|
+
id: "1",
|
166
|
+
type: "authors",
|
167
|
+
attributes: {
|
168
|
+
name: "Steve K."
|
169
|
+
},
|
170
|
+
relationships: {
|
171
|
+
posts: { data: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
172
|
+
roles: { data: [] },
|
173
|
+
bio: { data: { type: "bios", id: "1" }}
|
174
|
+
}
|
175
|
+
}, {
|
176
|
+
id: "10",
|
177
|
+
type: "posts",
|
178
|
+
attributes: {
|
179
|
+
title: "Hello!!",
|
180
|
+
body: "Hello, world!!"
|
181
|
+
},
|
182
|
+
relationships: {
|
183
|
+
comments: { data: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
184
|
+
blog: { data: { type: "blogs", id: "999" } },
|
185
|
+
author: { data: { type: "authors", id: "1" } }
|
186
|
+
}
|
187
|
+
}, {
|
188
|
+
id: "30",
|
189
|
+
type: "posts",
|
190
|
+
attributes: {
|
191
|
+
title: "Yet Another Post",
|
192
|
+
body: "Body"
|
193
|
+
},
|
194
|
+
relationships: {
|
195
|
+
comments: { data: [] },
|
196
|
+
blog: { data: { type: "blogs", id: "999" } },
|
197
|
+
author: { data: { type: "authors", id: "1" } }
|
198
|
+
}
|
199
|
+
}
|
200
|
+
]
|
201
|
+
|
202
|
+
assert_equal expected, adapter.serializable_hash[:included]
|
203
|
+
assert_equal expected, alt_adapter.serializable_hash[:included]
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_underscore_model_namespace_for_linked_resource_type
|
207
|
+
spammy_post = Post.new(id: 123)
|
208
|
+
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
|
209
|
+
serializer = SpammyPostSerializer.new(spammy_post)
|
210
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
211
|
+
relationships = adapter.serializable_hash[:data][:relationships]
|
212
|
+
expected = {
|
213
|
+
related: {
|
214
|
+
data: [{
|
215
|
+
type: 'spam_unrelated_links',
|
216
|
+
id: '456'
|
217
|
+
}]
|
218
|
+
}
|
219
|
+
}
|
220
|
+
assert_equal expected, relationships
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_multiple_references_to_same_resource
|
224
|
+
serializer = ArraySerializer.new([@first_comment, @second_comment])
|
225
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
226
|
+
serializer,
|
227
|
+
include: ['post']
|
228
|
+
)
|
229
|
+
|
230
|
+
expected = [
|
231
|
+
{
|
232
|
+
id: "10",
|
233
|
+
type: "posts",
|
234
|
+
attributes: {
|
235
|
+
title: "Hello!!",
|
236
|
+
body: "Hello, world!!"
|
237
|
+
},
|
238
|
+
relationships: {
|
239
|
+
comments: {
|
240
|
+
data: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
|
241
|
+
},
|
242
|
+
blog: {
|
243
|
+
data: {type: "blogs", id: "999"}
|
244
|
+
},
|
245
|
+
author: {
|
246
|
+
data: {type: "authors", id: "1"}
|
247
|
+
}
|
248
|
+
}
|
249
|
+
}
|
250
|
+
]
|
251
|
+
|
252
|
+
assert_equal expected, adapter.serializable_hash[:included]
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_nil_link_with_specified_serializer
|
256
|
+
@first_post.author = nil
|
257
|
+
serializer = PostPreviewSerializer.new(@first_post)
|
258
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
259
|
+
serializer,
|
260
|
+
include: ['author']
|
261
|
+
)
|
262
|
+
|
263
|
+
expected = {
|
264
|
+
data: {
|
265
|
+
id: "10",
|
266
|
+
type: "posts",
|
267
|
+
attributes: {
|
268
|
+
title: "Hello!!",
|
269
|
+
body: "Hello, world!!"
|
270
|
+
},
|
271
|
+
relationships: {
|
272
|
+
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
273
|
+
author: { data: nil }
|
274
|
+
}
|
275
|
+
}
|
276
|
+
}
|
277
|
+
assert_equal expected, adapter.serializable_hash
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'will_paginate/array'
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/hooks'
|
5
|
+
::Kaminari::Hooks.init
|
6
|
+
|
7
|
+
module ActiveModel
|
8
|
+
class Serializer
|
9
|
+
class Adapter
|
10
|
+
class JsonApi
|
11
|
+
class PaginationLinksTest < Minitest::Test
|
12
|
+
URI = 'http://example.com'
|
13
|
+
|
14
|
+
def setup
|
15
|
+
ActionController::Base.cache_store.clear
|
16
|
+
@array = [
|
17
|
+
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
18
|
+
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
19
|
+
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
def mock_request(query_parameters={}, original_url=URI)
|
24
|
+
context = Minitest::Mock.new
|
25
|
+
context.expect(:original_url, original_url )
|
26
|
+
context.expect(:query_parameters, query_parameters)
|
27
|
+
@options = {}
|
28
|
+
@options[:context] = context
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_adapter(paginated_collection, options = {})
|
32
|
+
options = options.merge(adapter: :json_api)
|
33
|
+
ActiveModel::SerializableResource.new(paginated_collection, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def using_kaminari
|
37
|
+
Kaminari.paginate_array(@array).page(2).per(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def using_will_paginate
|
41
|
+
@array.paginate(page: 2, per_page: 1)
|
42
|
+
end
|
43
|
+
|
44
|
+
def data
|
45
|
+
{ data:[
|
46
|
+
{ id:"1", type:"profiles", attributes:{name:"Name 1", description:"Description 1" } },
|
47
|
+
{ id:"2", type:"profiles", attributes:{name:"Name 2", description:"Description 2" } },
|
48
|
+
{ id:"3", type:"profiles", attributes:{name:"Name 3", description:"Description 3" } }
|
49
|
+
]
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def links
|
54
|
+
{
|
55
|
+
links:{
|
56
|
+
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
57
|
+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
58
|
+
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
59
|
+
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
60
|
+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
|
61
|
+
}
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def expected_response_without_pagination_links
|
66
|
+
data
|
67
|
+
end
|
68
|
+
|
69
|
+
def expected_response_with_pagination_links
|
70
|
+
{}.tap do |hash|
|
71
|
+
hash[:data] = [data.values.flatten.second]
|
72
|
+
hash.merge! links
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def expected_response_with_pagination_links_and_additional_params
|
77
|
+
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" }
|
78
|
+
{}.tap do |hash|
|
79
|
+
hash[:data] = [data.values.flatten.second]
|
80
|
+
hash.merge! links: new_links
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_pagination_links_using_kaminari
|
85
|
+
adapter = load_adapter(using_kaminari)
|
86
|
+
|
87
|
+
mock_request
|
88
|
+
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_pagination_links_using_will_paginate
|
92
|
+
adapter = load_adapter(using_will_paginate)
|
93
|
+
|
94
|
+
mock_request
|
95
|
+
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_pagination_links_with_additional_params
|
99
|
+
adapter = load_adapter(using_will_paginate)
|
100
|
+
|
101
|
+
mock_request({ test: 'test' })
|
102
|
+
assert_equal expected_response_with_pagination_links_and_additional_params,
|
103
|
+
adapter.serializable_hash(@options)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_not_showing_pagination_links
|
107
|
+
adapter = load_adapter(@array)
|
108
|
+
|
109
|
+
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonApi
|
7
|
+
class ResourceTypeConfigTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@author.bio = nil
|
11
|
+
@author.roles = []
|
12
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
13
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
14
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
15
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
16
|
+
@post.comments = [@comment]
|
17
|
+
@post.blog = @blog
|
18
|
+
@anonymous_post.comments = []
|
19
|
+
@anonymous_post.blog = nil
|
20
|
+
@comment.post = @post
|
21
|
+
@comment.author = nil
|
22
|
+
@post.author = @author
|
23
|
+
@anonymous_post.author = nil
|
24
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
25
|
+
@blog.writer = @author
|
26
|
+
@blog.articles = [@post, @anonymous_post]
|
27
|
+
@author.posts = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_jsonapi_resource_type type
|
31
|
+
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
|
32
|
+
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
|
33
|
+
yield
|
34
|
+
ensure
|
35
|
+
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_config_plural
|
39
|
+
with_jsonapi_resource_type :plural do
|
40
|
+
serializer = CommentSerializer.new(@comment)
|
41
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
42
|
+
ActionController::Base.cache_store.clear
|
43
|
+
assert_equal('comments', adapter.serializable_hash[:data][:type])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_config_singular
|
48
|
+
with_jsonapi_resource_type :singular do
|
49
|
+
serializer = CommentSerializer.new(@comment)
|
50
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
51
|
+
ActionController::Base.cache_store.clear
|
52
|
+
assert_equal('comment', adapter.serializable_hash[:data][:type])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class JsonTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
ActionController::Base.cache_store.clear
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
11
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
12
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
13
|
+
@post.comments = [@first_comment, @second_comment]
|
14
|
+
@first_comment.post = @post
|
15
|
+
@second_comment.post = @post
|
16
|
+
@post.author = @author
|
17
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
18
|
+
@post.blog = @blog
|
19
|
+
|
20
|
+
@serializer = PostSerializer.new(@post)
|
21
|
+
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_has_many
|
25
|
+
assert_equal([
|
26
|
+
{id: 1, body: 'ZOMG A COMMENT'},
|
27
|
+
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
28
|
+
], @adapter.serializable_hash[:post][:comments])
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_custom_keys
|
32
|
+
serializer = PostWithCustomKeysSerializer.new(@post)
|
33
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
34
|
+
|
35
|
+
assert_equal({
|
36
|
+
id: 1,
|
37
|
+
reviews: [{id: 1, body: "ZOMG A COMMENT"},
|
38
|
+
{id: 2, body: "ZOMG ANOTHER COMMENT"}
|
39
|
+
],
|
40
|
+
writer: {id: 1, name: "Steve K."},
|
41
|
+
site: {id: 1, name: "My Blog!!"}
|
42
|
+
}, adapter.serializable_hash[:post])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class NullTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
serializer = ProfileSerializer.new(profile)
|
10
|
+
|
11
|
+
@adapter = Null.new(serializer)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_serializable_hash
|
15
|
+
assert_equal({}, @adapter.serializable_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_it_returns_empty_json
|
19
|
+
assert_equal('{}', @adapter.to_json)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AdapterTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
profile = Profile.new
|
8
|
+
@serializer = ProfileSerializer.new(profile)
|
9
|
+
@adapter = ActiveModel::Serializer::Adapter.new(@serializer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serializable_hash_is_abstract_method
|
13
|
+
assert_raises(NotImplementedError) do
|
14
|
+
@adapter.serializable_hash(only: [:name])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_serializer
|
19
|
+
assert_equal @serializer, @adapter.serializer
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_adapter_class_for_known_adapter
|
23
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
24
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_adapter_class_for_unknown_adapter
|
28
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_simple)
|
29
|
+
assert_nil klass
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_create_adapter
|
33
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
34
|
+
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_create_adapter_with_override
|
38
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
|
39
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_inflected_adapter_class_for_known_adapter
|
43
|
+
ActiveSupport::Inflector.inflections(:en){|inflect| inflect.acronym 'API' }
|
44
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
45
|
+
|
46
|
+
ActiveSupport::Inflector.inflections.acronyms.clear
|
47
|
+
|
48
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ArraySerializerTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@comment = Comment.new
|
8
|
+
@post = Post.new
|
9
|
+
@resource = build_named_collection @comment, @post
|
10
|
+
@serializer = ArraySerializer.new(@resource, {some: :options})
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_named_collection(*resource)
|
14
|
+
resource.define_singleton_method(:name){ 'MeResource' }
|
15
|
+
resource
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_respond_to_each
|
19
|
+
assert_respond_to @serializer, :each
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_each_object_should_be_serialized_with_appropriate_serializer
|
23
|
+
serializers = @serializer.to_a
|
24
|
+
|
25
|
+
assert_kind_of CommentSerializer, serializers.first
|
26
|
+
assert_kind_of Comment, serializers.first.object
|
27
|
+
|
28
|
+
assert_kind_of PostSerializer, serializers.last
|
29
|
+
assert_kind_of Post, serializers.last.object
|
30
|
+
|
31
|
+
assert_equal serializers.last.custom_options[:some], :options
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_serializer_option_not_passed_to_each_serializer
|
35
|
+
serializers = ArraySerializer.new([@post], {serializer: PostSerializer}).to_a
|
36
|
+
|
37
|
+
refute serializers.first.custom_options.key?(:serializer)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_meta_and_meta_key_attr_readers
|
41
|
+
meta_content = {meta: "the meta", meta_key: "the meta key"}
|
42
|
+
@serializer = ArraySerializer.new([@comment, @post], meta_content)
|
43
|
+
|
44
|
+
assert_equal @serializer.meta, "the meta"
|
45
|
+
assert_equal @serializer.meta_key, "the meta key"
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_root_default
|
49
|
+
@serializer = ArraySerializer.new([@comment, @post])
|
50
|
+
assert_equal @serializer.root, nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_root
|
54
|
+
expected = 'custom_root'
|
55
|
+
@serializer = ArraySerializer.new([@comment, @post], root: expected)
|
56
|
+
assert_equal @serializer.root, expected
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_root_with_no_serializers
|
60
|
+
expected = 'custom_root'
|
61
|
+
@serializer = ArraySerializer.new([], root: expected)
|
62
|
+
assert_equal @serializer.root, expected
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_json_key
|
66
|
+
assert_equal @serializer.json_key, 'comments'
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_json_key_with_resource_with_name_and_no_serializers
|
70
|
+
serializer = ArraySerializer.new(build_named_collection)
|
71
|
+
assert_equal serializer.json_key, 'me_resources'
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_json_key_with_resource_with_nil_name_and_no_serializers
|
75
|
+
resource = []
|
76
|
+
resource.define_singleton_method(:name){ nil }
|
77
|
+
serializer = ArraySerializer.new(resource)
|
78
|
+
assert_equal serializer.json_key, nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_json_key_with_resource_without_name_and_no_serializers
|
82
|
+
serializer = ArraySerializer.new([])
|
83
|
+
assert_equal serializer.json_key, nil
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_json_key_with_root
|
87
|
+
serializer = ArraySerializer.new(@resource, root: 'custom_root')
|
88
|
+
assert_equal serializer.json_key, 'custom_roots'
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_json_key_with_root_and_no_serializers
|
92
|
+
serializer = ArraySerializer.new(build_named_collection, root: 'custom_root')
|
93
|
+
assert_equal serializer.json_key, 'custom_roots'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|