active_model_serializers 0.1.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{README.textile → DESIGN.textile} +31 -3
- data/Gemfile +3 -0
- data/MIT-LICENSE.txt +21 -0
- data/README.markdown +270 -0
- data/RELEASE_NOTES.md +4 -0
- data/active_model_serializers.gemspec +5 -1
- data/lib/action_controller/serialization.rb +14 -3
- data/lib/active_model/serializer.rb +231 -78
- data/lib/active_model/serializers/version.rb +5 -0
- data/lib/active_model_serializers.rb +11 -0
- data/lib/generators/resource_override.rb +13 -0
- data/lib/generators/serializer/serializer_generator.rb +1 -1
- data/test/association_test.rb +392 -0
- data/test/generators_test.rb +3 -3
- data/test/serialization_test.rb +50 -4
- data/test/serializer_test.rb +123 -93
- data/test/test_helper.rb +10 -0
- metadata +25 -8
@@ -3,6 +3,17 @@ require "active_support/core_ext/string/inflections"
|
|
3
3
|
require "active_model"
|
4
4
|
require "active_model/serializer"
|
5
5
|
|
6
|
+
if defined?(Rails)
|
7
|
+
module ActiveModel
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
generators do |app|
|
10
|
+
Rails::Generators.configure!(app.config.generators)
|
11
|
+
require "generators/resource_override"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
6
17
|
module ActiveModel::SerializerSupport
|
7
18
|
extend ActiveSupport::Concern
|
8
19
|
|
@@ -30,7 +30,7 @@ module Rails
|
|
30
30
|
# Only works on 3.2
|
31
31
|
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
|
32
32
|
# "ApplicationSerializer"
|
33
|
-
elsif
|
33
|
+
elsif defined?(:ApplicationSerializer)
|
34
34
|
"ApplicationSerializer"
|
35
35
|
else
|
36
36
|
"ActiveModel::Serializer"
|
@@ -0,0 +1,392 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssociationTest < ActiveModel::TestCase
|
4
|
+
def def_serializer(&block)
|
5
|
+
Class.new(ActiveModel::Serializer, &block)
|
6
|
+
end
|
7
|
+
|
8
|
+
class Model
|
9
|
+
def initialize(hash={})
|
10
|
+
@attributes = hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def read_attribute_for_serialization(name)
|
14
|
+
@attributes[name]
|
15
|
+
end
|
16
|
+
|
17
|
+
def as_json(*)
|
18
|
+
{ :model => "Model" }
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(meth, *args)
|
22
|
+
if meth.to_s =~ /^(.*)=$/
|
23
|
+
@attributes[$1.to_sym] = args[0]
|
24
|
+
elsif @attributes.key?(meth)
|
25
|
+
@attributes[meth]
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def setup
|
33
|
+
@post = Model.new(:title => "New Post", :body => "Body")
|
34
|
+
@comment = Model.new(:id => 1, :body => "ZOMG A COMMENT")
|
35
|
+
@post.comments = [ @comment ]
|
36
|
+
@post.comment = @comment
|
37
|
+
|
38
|
+
@comment_serializer_class = def_serializer do
|
39
|
+
attributes :id, :body
|
40
|
+
end
|
41
|
+
|
42
|
+
@post_serializer_class = def_serializer do
|
43
|
+
attributes :title, :body
|
44
|
+
end
|
45
|
+
|
46
|
+
@post_serializer = @post_serializer_class.new(@post)
|
47
|
+
|
48
|
+
@hash = {}
|
49
|
+
@root_hash = {}
|
50
|
+
end
|
51
|
+
|
52
|
+
def include!(key, options={})
|
53
|
+
@post_serializer.include! key, {
|
54
|
+
:embed => :ids,
|
55
|
+
:include => true,
|
56
|
+
:hash => @root_hash,
|
57
|
+
:node => @hash,
|
58
|
+
:serializer => @comment_serializer_class
|
59
|
+
}.merge(options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def include_bare!(key, options={})
|
63
|
+
@post_serializer.include! key, {
|
64
|
+
:hash => @root_hash,
|
65
|
+
:node => @hash,
|
66
|
+
:serializer => @comment_serializer_class
|
67
|
+
}.merge(options)
|
68
|
+
end
|
69
|
+
|
70
|
+
class NoDefaults < AssociationTest
|
71
|
+
def test_include_bang_has_many_associations
|
72
|
+
include! :comments, :value => @post.comments
|
73
|
+
|
74
|
+
assert_equal({
|
75
|
+
:comments => [ 1 ]
|
76
|
+
}, @hash)
|
77
|
+
|
78
|
+
assert_equal({
|
79
|
+
:comments => [
|
80
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
81
|
+
]
|
82
|
+
}, @root_hash)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_include_bang_with_embed_false
|
86
|
+
include! :comments, :value => @post.comments, :embed => false
|
87
|
+
|
88
|
+
assert_equal({}, @hash)
|
89
|
+
assert_equal({}, @root_hash)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_include_bang_with_embed_ids_include_false
|
93
|
+
include! :comments, :value => @post.comments, :embed => :ids, :include => false
|
94
|
+
|
95
|
+
assert_equal({
|
96
|
+
:comments => [ 1 ]
|
97
|
+
}, @hash)
|
98
|
+
|
99
|
+
assert_equal({}, @root_hash)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_include_bang_has_one_associations
|
103
|
+
include! :comment, :value => @post.comment
|
104
|
+
|
105
|
+
assert_equal({
|
106
|
+
:comment => 1
|
107
|
+
}, @hash)
|
108
|
+
|
109
|
+
assert_equal({
|
110
|
+
:comments => [{ :id => 1, :body => "ZOMG A COMMENT" }]
|
111
|
+
}, @root_hash)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
class DefaultsTest < AssociationTest
|
116
|
+
def test_with_default_has_many
|
117
|
+
@post_serializer_class.class_eval do
|
118
|
+
has_many :comments
|
119
|
+
end
|
120
|
+
|
121
|
+
include! :comments
|
122
|
+
|
123
|
+
assert_equal({
|
124
|
+
:comments => [ 1 ]
|
125
|
+
}, @hash)
|
126
|
+
|
127
|
+
assert_equal({
|
128
|
+
:comments => [
|
129
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
130
|
+
]
|
131
|
+
}, @root_hash)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_with_default_has_one
|
135
|
+
@post_serializer_class.class_eval do
|
136
|
+
has_one :comment
|
137
|
+
end
|
138
|
+
|
139
|
+
include! :comment
|
140
|
+
|
141
|
+
assert_equal({
|
142
|
+
:comment => 1
|
143
|
+
}, @hash)
|
144
|
+
|
145
|
+
assert_equal({
|
146
|
+
:comments => [
|
147
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
148
|
+
]
|
149
|
+
}, @root_hash)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_with_default_has_many_with_custom_key
|
153
|
+
@post_serializer_class.class_eval do
|
154
|
+
has_many :comments, :key => :custom_comments
|
155
|
+
end
|
156
|
+
|
157
|
+
include! :comments
|
158
|
+
|
159
|
+
assert_equal({
|
160
|
+
:custom_comments => [ 1 ]
|
161
|
+
}, @hash)
|
162
|
+
|
163
|
+
assert_equal({
|
164
|
+
:custom_comments => [
|
165
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
166
|
+
]
|
167
|
+
}, @root_hash)
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_with_default_has_one_with_custom_key
|
171
|
+
@post_serializer_class.class_eval do
|
172
|
+
has_one :comment, :key => :custom_comment
|
173
|
+
end
|
174
|
+
|
175
|
+
include! :comment
|
176
|
+
|
177
|
+
assert_equal({
|
178
|
+
:custom_comment => 1
|
179
|
+
}, @hash)
|
180
|
+
|
181
|
+
assert_equal({
|
182
|
+
:custom_comments => [
|
183
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
184
|
+
]
|
185
|
+
}, @root_hash)
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_with_default_has_one_with_custom_key
|
189
|
+
@post_serializer_class.class_eval do
|
190
|
+
has_one :comment, :key => :custom_comment
|
191
|
+
end
|
192
|
+
|
193
|
+
include! :comment
|
194
|
+
|
195
|
+
assert_equal({
|
196
|
+
:custom_comment => 1
|
197
|
+
}, @hash)
|
198
|
+
|
199
|
+
assert_equal({
|
200
|
+
:custom_comments => [
|
201
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
202
|
+
]
|
203
|
+
}, @root_hash)
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_embed_objects_for_has_many_associations
|
207
|
+
@post_serializer_class.class_eval do
|
208
|
+
has_many :comments, :embed => :objects
|
209
|
+
end
|
210
|
+
|
211
|
+
include_bare! :comments
|
212
|
+
|
213
|
+
assert_equal({
|
214
|
+
:comments => [
|
215
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
216
|
+
]
|
217
|
+
}, @hash)
|
218
|
+
|
219
|
+
assert_equal({}, @root_hash)
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_embed_ids_for_has_many_associations
|
223
|
+
@post_serializer_class.class_eval do
|
224
|
+
has_many :comments, :embed => :ids
|
225
|
+
end
|
226
|
+
|
227
|
+
include_bare! :comments
|
228
|
+
|
229
|
+
assert_equal({
|
230
|
+
:comments => [ 1 ]
|
231
|
+
}, @hash)
|
232
|
+
|
233
|
+
assert_equal({}, @root_hash)
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_embed_false_for_has_many_associations
|
237
|
+
@post_serializer_class.class_eval do
|
238
|
+
has_many :comments, :embed => false
|
239
|
+
end
|
240
|
+
|
241
|
+
include_bare! :comments
|
242
|
+
|
243
|
+
assert_equal({}, @hash)
|
244
|
+
assert_equal({}, @root_hash)
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_embed_ids_include_true_for_has_many_associations
|
248
|
+
@post_serializer_class.class_eval do
|
249
|
+
has_many :comments, :embed => :ids, :include => true
|
250
|
+
end
|
251
|
+
|
252
|
+
include_bare! :comments
|
253
|
+
|
254
|
+
assert_equal({
|
255
|
+
:comments => [ 1 ]
|
256
|
+
}, @hash)
|
257
|
+
|
258
|
+
assert_equal({
|
259
|
+
:comments => [
|
260
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
261
|
+
]
|
262
|
+
}, @root_hash)
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_embed_ids_for_has_one_associations
|
266
|
+
@post_serializer_class.class_eval do
|
267
|
+
has_one :comment, :embed => :ids
|
268
|
+
end
|
269
|
+
|
270
|
+
include_bare! :comment
|
271
|
+
|
272
|
+
assert_equal({
|
273
|
+
:comment => 1
|
274
|
+
}, @hash)
|
275
|
+
|
276
|
+
assert_equal({}, @root_hash)
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_embed_false_for_has_one_associations
|
280
|
+
@post_serializer_class.class_eval do
|
281
|
+
has_one :comment, :embed => false
|
282
|
+
end
|
283
|
+
|
284
|
+
include_bare! :comment
|
285
|
+
|
286
|
+
assert_equal({}, @hash)
|
287
|
+
assert_equal({}, @root_hash)
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_embed_ids_include_true_for_has_one_associations
|
291
|
+
@post_serializer_class.class_eval do
|
292
|
+
has_one :comment, :embed => :ids, :include => true
|
293
|
+
end
|
294
|
+
|
295
|
+
include_bare! :comment
|
296
|
+
|
297
|
+
assert_equal({
|
298
|
+
:comment => 1
|
299
|
+
}, @hash)
|
300
|
+
|
301
|
+
assert_equal({
|
302
|
+
:comments => [
|
303
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
304
|
+
]
|
305
|
+
}, @root_hash)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
class InclusionTest < AssociationTest
|
310
|
+
def setup
|
311
|
+
super
|
312
|
+
|
313
|
+
comment_serializer_class = @comment_serializer_class
|
314
|
+
|
315
|
+
@post_serializer_class.class_eval do
|
316
|
+
root :post
|
317
|
+
embed :ids, :include => true
|
318
|
+
has_many :comments, :serializer => comment_serializer_class
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_when_it_is_included
|
323
|
+
post_serializer = @post_serializer_class.new(
|
324
|
+
@post, :include => [:comments]
|
325
|
+
)
|
326
|
+
|
327
|
+
json = post_serializer.as_json
|
328
|
+
|
329
|
+
assert_equal({
|
330
|
+
:post => {
|
331
|
+
:title => "New Post",
|
332
|
+
:body => "Body",
|
333
|
+
:comments => [ 1 ]
|
334
|
+
},
|
335
|
+
:comments => [
|
336
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
337
|
+
]
|
338
|
+
}, json)
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_when_it_is_not_included
|
342
|
+
post_serializer = @post_serializer_class.new(
|
343
|
+
@post, :include => []
|
344
|
+
)
|
345
|
+
|
346
|
+
json = post_serializer.as_json
|
347
|
+
|
348
|
+
assert_equal({
|
349
|
+
:post => {
|
350
|
+
:title => "New Post",
|
351
|
+
:body => "Body",
|
352
|
+
:comments => [ 1 ]
|
353
|
+
}
|
354
|
+
}, json)
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_when_it_is_excluded
|
358
|
+
post_serializer = @post_serializer_class.new(
|
359
|
+
@post, :exclude => [:comments]
|
360
|
+
)
|
361
|
+
|
362
|
+
json = post_serializer.as_json
|
363
|
+
|
364
|
+
assert_equal({
|
365
|
+
:post => {
|
366
|
+
:title => "New Post",
|
367
|
+
:body => "Body",
|
368
|
+
:comments => [ 1 ]
|
369
|
+
}
|
370
|
+
}, json)
|
371
|
+
end
|
372
|
+
|
373
|
+
def test_when_it_is_not_excluded
|
374
|
+
post_serializer = @post_serializer_class.new(
|
375
|
+
@post, :exclude => []
|
376
|
+
)
|
377
|
+
|
378
|
+
json = post_serializer.as_json
|
379
|
+
|
380
|
+
assert_equal({
|
381
|
+
:post => {
|
382
|
+
:title => "New Post",
|
383
|
+
:body => "Body",
|
384
|
+
:comments => [ 1 ]
|
385
|
+
},
|
386
|
+
:comments => [
|
387
|
+
{ :id => 1, :body => "ZOMG A COMMENT" }
|
388
|
+
]
|
389
|
+
}, json)
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
data/test/generators_test.rb
CHANGED
@@ -16,12 +16,12 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|
16
16
|
|
17
17
|
def test_generates_a_serializer
|
18
18
|
run_generator
|
19
|
-
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer <
|
19
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_generates_a_namespaced_serializer
|
23
23
|
run_generator ["admin/account"]
|
24
|
-
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer <
|
24
|
+
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ApplicationSerializer/
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_uses_application_serializer_if_one_exists
|
@@ -62,6 +62,6 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|
62
62
|
|
63
63
|
def test_with_no_attributes_does_not_add_extra_space
|
64
64
|
run_generator ["account"]
|
65
|
-
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer <
|
65
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer\nend/
|
66
66
|
end
|
67
67
|
end
|