active_model_serializers 0.10.0 → 0.10.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +9 -1
- data/CHANGELOG.md +81 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +24 -24
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +20 -24
- data/docs/ARCHITECTURE.md +6 -7
- data/docs/README.md +2 -0
- data/docs/general/adapters.md +4 -2
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/rendering.md +42 -3
- data/docs/general/serializers.md +97 -8
- data/docs/howto/add_pagination_links.md +4 -5
- data/docs/howto/add_relationship_links.md +137 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +9 -2
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +64 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +76 -37
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +38 -38
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/adapter/json_api.rb +44 -43
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/key_transform.rb +4 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +4 -2
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers.rb +7 -0
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/test/action_controller/adapter_selector_test.rb +4 -4
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +6 -7
- data/test/action_controller/json_api/fields_test.rb +57 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +3 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +226 -0
- data/test/action_controller/serialization_test.rb +10 -7
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/key_transform_test.rb +286 -252
- data/test/active_model_serializers/model_test.rb +17 -4
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +4 -3
- data/test/adapter/json_api/has_many_test.rb +39 -18
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +255 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +16 -5
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +143 -49
- data/test/collection_serializer_test.rb +3 -3
- data/test/fixtures/poro.rb +52 -48
- data/test/generators/serializer_generator_test.rb +22 -5
- data/test/grape_test.rb +152 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +107 -32
- data/test/serializers/attribute_test.rb +2 -2
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +6 -4
- data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +0 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +1 -0
- metadata +85 -34
- data/.rubocop_todo.yml +0 -167
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
3
2
|
module ActiveModel
|
4
3
|
class Serializer
|
5
4
|
class AssociationsTest < ActiveSupport::TestCase
|
@@ -7,10 +6,10 @@ module ActiveModel
|
|
7
6
|
@author = Author.new(name: 'Steve K.')
|
8
7
|
@author.bio = nil
|
9
8
|
@author.roles = []
|
10
|
-
@blog = Blog.new(
|
11
|
-
@post = Post.new(
|
12
|
-
@tag = Tag.new(
|
13
|
-
@comment = Comment.new(
|
9
|
+
@blog = Blog.new(name: 'AMS Blog')
|
10
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
11
|
+
@tag = Tag.new(name: '#hashtagged')
|
12
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
14
13
|
@post.comments = [@comment]
|
15
14
|
@post.tags = [@tag]
|
16
15
|
@post.blog = @blog
|
@@ -19,7 +18,7 @@ module ActiveModel
|
|
19
18
|
@post.author = @author
|
20
19
|
@author.posts = [@post]
|
21
20
|
|
22
|
-
@post_serializer = PostSerializer.new(@post,
|
21
|
+
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
23
22
|
@author_serializer = AuthorSerializer.new(@author)
|
24
23
|
@comment_serializer = CommentSerializer.new(@comment)
|
25
24
|
end
|
@@ -32,13 +31,13 @@ module ActiveModel
|
|
32
31
|
|
33
32
|
case key
|
34
33
|
when :posts
|
35
|
-
assert_equal
|
34
|
+
assert_equal true, options.fetch(:include_data)
|
36
35
|
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
37
36
|
when :bio
|
38
|
-
assert_equal
|
37
|
+
assert_equal true, options.fetch(:include_data)
|
39
38
|
assert_nil serializer
|
40
39
|
when :roles
|
41
|
-
assert_equal
|
40
|
+
assert_equal true, options.fetch(:include_data)
|
42
41
|
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
43
42
|
else
|
44
43
|
flunk "Unknown association: #{key}"
|
@@ -80,7 +79,7 @@ module ActiveModel
|
|
80
79
|
flunk "Unknown association: #{key}"
|
81
80
|
end
|
82
81
|
|
83
|
-
assert_equal
|
82
|
+
assert_equal true, association.options.fetch(:include_data)
|
84
83
|
end
|
85
84
|
end
|
86
85
|
|
@@ -104,13 +103,13 @@ module ActiveModel
|
|
104
103
|
end
|
105
104
|
|
106
105
|
assert(
|
107
|
-
PostSerializer._reflections.all? do |reflection|
|
108
|
-
inherited_klass._reflections.include?(reflection)
|
106
|
+
PostSerializer._reflections.values.all? do |reflection|
|
107
|
+
inherited_klass._reflections.values.include?(reflection)
|
109
108
|
end
|
110
109
|
)
|
111
110
|
|
112
111
|
assert(
|
113
|
-
inherited_klass._reflections.any? do |reflection|
|
112
|
+
inherited_klass._reflections.values.any? do |reflection|
|
114
113
|
reflection.name == :top_comments
|
115
114
|
end
|
116
115
|
)
|
@@ -143,12 +142,12 @@ module ActiveModel
|
|
143
142
|
)
|
144
143
|
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
145
144
|
expected = {
|
146
|
-
:
|
147
|
-
{ :
|
148
|
-
{ :
|
145
|
+
comments: [
|
146
|
+
{ id: 1, contents: 'first comment' },
|
147
|
+
{ id: 2, contents: 'last comment' }
|
149
148
|
],
|
150
|
-
:
|
151
|
-
{ :
|
149
|
+
last_comments: [
|
150
|
+
{ id: 2, contents: 'last comment' }
|
152
151
|
]
|
153
152
|
}
|
154
153
|
|
@@ -160,18 +159,18 @@ module ActiveModel
|
|
160
159
|
|
161
160
|
class NamespacedResourcesTest < ActiveSupport::TestCase
|
162
161
|
class ResourceNamespace
|
163
|
-
Post
|
164
|
-
Comment
|
165
|
-
Author
|
166
|
-
Description
|
162
|
+
class Post < ::Model; end
|
163
|
+
class Comment < ::Model; end
|
164
|
+
class Author < ::Model; end
|
165
|
+
class Description < ::Model; end
|
167
166
|
class PostSerializer < ActiveModel::Serializer
|
168
167
|
has_many :comments
|
169
168
|
belongs_to :author
|
170
169
|
has_one :description
|
171
170
|
end
|
172
|
-
CommentSerializer
|
173
|
-
AuthorSerializer
|
174
|
-
DescriptionSerializer
|
171
|
+
class CommentSerializer < ActiveModel::Serializer; end
|
172
|
+
class AuthorSerializer < ActiveModel::Serializer; end
|
173
|
+
class DescriptionSerializer < ActiveModel::Serializer; end
|
175
174
|
end
|
176
175
|
|
177
176
|
def setup
|
@@ -201,17 +200,17 @@ module ActiveModel
|
|
201
200
|
end
|
202
201
|
|
203
202
|
class NestedSerializersTest < ActiveSupport::TestCase
|
204
|
-
Post
|
205
|
-
Comment
|
206
|
-
Author
|
207
|
-
Description
|
203
|
+
class Post < ::Model; end
|
204
|
+
class Comment < ::Model; end
|
205
|
+
class Author < ::Model; end
|
206
|
+
class Description < ::Model; end
|
208
207
|
class PostSerializer < ActiveModel::Serializer
|
209
208
|
has_many :comments
|
210
|
-
CommentSerializer
|
209
|
+
class CommentSerializer < ActiveModel::Serializer; end
|
211
210
|
belongs_to :author
|
212
|
-
AuthorSerializer
|
211
|
+
class AuthorSerializer < ActiveModel::Serializer; end
|
213
212
|
has_one :description
|
214
|
-
DescriptionSerializer
|
213
|
+
class DescriptionSerializer < ActiveModel::Serializer; end
|
215
214
|
end
|
216
215
|
|
217
216
|
def setup
|
@@ -290,6 +289,82 @@ module ActiveModel
|
|
290
289
|
assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
291
290
|
end
|
292
291
|
end
|
292
|
+
|
293
|
+
class InheritedSerializerTest < ActiveSupport::TestCase
|
294
|
+
class PostSerializer < ActiveModel::Serializer
|
295
|
+
belongs_to :author
|
296
|
+
has_many :comments
|
297
|
+
belongs_to :blog
|
298
|
+
end
|
299
|
+
|
300
|
+
class InheritedPostSerializer < PostSerializer
|
301
|
+
belongs_to :author, polymorphic: true
|
302
|
+
has_many :comments, key: :reviews
|
303
|
+
end
|
304
|
+
|
305
|
+
class AuthorSerializer < ActiveModel::Serializer
|
306
|
+
has_many :posts
|
307
|
+
has_many :roles
|
308
|
+
has_one :bio
|
309
|
+
end
|
310
|
+
|
311
|
+
class InheritedAuthorSerializer < AuthorSerializer
|
312
|
+
has_many :roles, polymorphic: true
|
313
|
+
has_one :bio, polymorphic: true
|
314
|
+
end
|
315
|
+
|
316
|
+
def setup
|
317
|
+
@author = Author.new(name: 'Steve K.')
|
318
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
319
|
+
@post_serializer = PostSerializer.new(@post)
|
320
|
+
@author_serializer = AuthorSerializer.new(@author)
|
321
|
+
@inherited_post_serializer = InheritedPostSerializer.new(@post)
|
322
|
+
@inherited_author_serializer = InheritedAuthorSerializer.new(@author)
|
323
|
+
@author_associations = @author_serializer.associations.to_a
|
324
|
+
@inherited_author_associations = @inherited_author_serializer.associations.to_a
|
325
|
+
@post_associations = @post_serializer.associations.to_a
|
326
|
+
@inherited_post_associations = @inherited_post_serializer.associations.to_a
|
327
|
+
end
|
328
|
+
|
329
|
+
test 'an author serializer must have [posts,roles,bio] associations' do
|
330
|
+
expected = [:posts, :roles, :bio].sort
|
331
|
+
result = @author_serializer.associations.map(&:name).sort
|
332
|
+
assert_equal(result, expected)
|
333
|
+
end
|
334
|
+
|
335
|
+
test 'a post serializer must have [author,comments,blog] associations' do
|
336
|
+
expected = [:author, :comments, :blog].sort
|
337
|
+
result = @post_serializer.associations.map(&:name).sort
|
338
|
+
assert_equal(result, expected)
|
339
|
+
end
|
340
|
+
|
341
|
+
test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
|
342
|
+
expected = [:roles, :bio].sort
|
343
|
+
result = (@inherited_author_associations - @author_associations).map(&:name).sort
|
344
|
+
assert_equal(result, expected)
|
345
|
+
end
|
346
|
+
|
347
|
+
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
|
348
|
+
assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
|
349
|
+
assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
|
350
|
+
|
351
|
+
refute @post_associations.detect { |assoc| assoc.name == :author }.options.key?(:polymorphic)
|
352
|
+
assert_equal true, @inherited_post_associations.detect { |assoc| assoc.name == :author }.options.fetch(:polymorphic)
|
353
|
+
|
354
|
+
refute @post_associations.detect { |assoc| assoc.name == :comments }.options.key?(:key)
|
355
|
+
original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
|
356
|
+
refute original_comment_assoc.options.key?(:key)
|
357
|
+
assert_equal :reviews, new_comments_assoc.options.fetch(:key)
|
358
|
+
|
359
|
+
assert_equal @post_associations.detect { |assoc| assoc.name == :blog }, @inherited_post_associations.detect { |assoc| assoc.name == :blog }
|
360
|
+
end
|
361
|
+
|
362
|
+
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
|
363
|
+
expected = [:author, :comments, :blog, :reviews].sort
|
364
|
+
result = @inherited_post_serializer.associations.map { |a| a.options.fetch(:key, a.name) }.sort
|
365
|
+
assert_equal(result, expected)
|
366
|
+
end
|
367
|
+
end
|
293
368
|
end
|
294
369
|
end
|
295
370
|
end
|
@@ -22,7 +22,7 @@ module ActiveModel
|
|
22
22
|
inherited_klass = Class.new(AlternateBlogSerializer)
|
23
23
|
blog_serializer = inherited_klass.new(@blog)
|
24
24
|
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
25
|
-
assert_equal({ :
|
25
|
+
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_multiple_calls_with_the_same_attribute
|
@@ -81,7 +81,7 @@ module ActiveModel
|
|
81
81
|
assert_equal('custom', hash[:blog][:id])
|
82
82
|
end
|
83
83
|
|
84
|
-
PostWithVirtualAttribute
|
84
|
+
class PostWithVirtualAttribute < ::Model; end
|
85
85
|
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
|
86
86
|
attribute :name do
|
87
87
|
"#{object.first_name} #{object.last_name}"
|
@@ -4,7 +4,7 @@ module ActiveModel
|
|
4
4
|
class Serializer
|
5
5
|
class AttributesTest < ActiveSupport::TestCase
|
6
6
|
def setup
|
7
|
-
@profile = Profile.new(
|
7
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
8
8
|
@profile_serializer = ProfileSerializer.new(@profile)
|
9
9
|
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
10
10
|
@serializer_klass = Class.new(CommentSerializer)
|
@@ -5,7 +5,7 @@ module ActiveModel
|
|
5
5
|
class FieldsetTest < ActiveSupport::TestCase
|
6
6
|
def test_fieldset_with_hash
|
7
7
|
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
8
|
-
expected = { :
|
8
|
+
expected = { post: [:id, :title], comment: [:body] }
|
9
9
|
|
10
10
|
assert_equal(expected, fieldset.fields)
|
11
11
|
end
|
@@ -15,7 +15,8 @@ module ActiveModel
|
|
15
15
|
@blog,
|
16
16
|
adapter: :json,
|
17
17
|
serializer: AlternateBlogSerializer,
|
18
|
-
meta: { total: 10 }
|
18
|
+
meta: { total: 10 }
|
19
|
+
).as_json
|
19
20
|
expected = {
|
20
21
|
blog: {
|
21
22
|
id: 1,
|
@@ -65,7 +66,8 @@ module ActiveModel
|
|
65
66
|
@blog,
|
66
67
|
adapter: :attributes,
|
67
68
|
serializer: AlternateBlogSerializer,
|
68
|
-
meta: { total: 10 }
|
69
|
+
meta: { total: 10 }
|
70
|
+
).as_json
|
69
71
|
expected = {
|
70
72
|
id: 1,
|
71
73
|
title: 'AMS Hints'
|
@@ -79,7 +81,8 @@ module ActiveModel
|
|
79
81
|
adapter: :json,
|
80
82
|
serializer: AlternateBlogSerializer,
|
81
83
|
meta: { total: 10 },
|
82
|
-
meta_key: 'haha_meta'
|
84
|
+
meta_key: 'haha_meta'
|
85
|
+
).as_json
|
83
86
|
expected = {
|
84
87
|
blog: {
|
85
88
|
id: 1,
|
@@ -98,7 +101,8 @@ module ActiveModel
|
|
98
101
|
adapter: :json_api,
|
99
102
|
serializer: AlternateBlogSerializer,
|
100
103
|
meta: { total: 10 },
|
101
|
-
meta_key: 'haha_meta'
|
104
|
+
meta_key: 'haha_meta'
|
105
|
+
).as_json
|
102
106
|
expected = {
|
103
107
|
data: {
|
104
108
|
id: '1',
|
@@ -148,7 +152,8 @@ module ActiveModel
|
|
148
152
|
actual = ActiveModelSerializers::SerializableResource.new(
|
149
153
|
[@blog],
|
150
154
|
adapter: :attributes,
|
151
|
-
meta: { total: 10 }
|
155
|
+
meta: { total: 10 }
|
156
|
+
).as_json
|
152
157
|
expected = [{
|
153
158
|
id: 1,
|
154
159
|
name: 'AMS Hints',
|
@@ -170,7 +175,8 @@ module ActiveModel
|
|
170
175
|
[@blog],
|
171
176
|
adapter: :json,
|
172
177
|
meta: { total: 10 },
|
173
|
-
meta_key: 'haha_meta'
|
178
|
+
meta_key: 'haha_meta'
|
179
|
+
).as_json
|
174
180
|
expected = {
|
175
181
|
blogs: [{
|
176
182
|
id: 1,
|
@@ -28,8 +28,8 @@ module ActiveModel
|
|
28
28
|
|
29
29
|
class SerializerTest < ActiveSupport::TestCase
|
30
30
|
module ResourceNamespace
|
31
|
-
Post
|
32
|
-
Comment
|
31
|
+
class Post < ::Model; end
|
32
|
+
class Comment < ::Model; end
|
33
33
|
|
34
34
|
class PostSerializer < ActiveModel::Serializer
|
35
35
|
class CommentSerializer < ActiveModel::Serializer
|
@@ -41,10 +41,12 @@ module ActiveModel
|
|
41
41
|
end
|
42
42
|
|
43
43
|
class CustomProfile
|
44
|
-
def serializer_class
|
44
|
+
def serializer_class
|
45
|
+
ProfileSerializer
|
46
|
+
end
|
45
47
|
end
|
46
48
|
|
47
|
-
Tweet
|
49
|
+
class Tweet < ::Model; end
|
48
50
|
TweetSerializer = Class.new
|
49
51
|
|
50
52
|
def setup
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class SerializerForWithNamespaceTest < ActiveSupport::TestCase
|
6
|
+
class Book < ::Model; end
|
7
|
+
class Page < ::Model; end
|
8
|
+
class Publisher < ::Model; end
|
9
|
+
|
10
|
+
module Api
|
11
|
+
module V3
|
12
|
+
class BookSerializer < ActiveModel::Serializer
|
13
|
+
attributes :title, :author_name
|
14
|
+
|
15
|
+
has_many :pages
|
16
|
+
belongs_to :publisher
|
17
|
+
end
|
18
|
+
|
19
|
+
class PageSerializer < ActiveModel::Serializer
|
20
|
+
attributes :number, :text
|
21
|
+
|
22
|
+
belongs_to :book
|
23
|
+
end
|
24
|
+
|
25
|
+
class PublisherSerializer < ActiveModel::Serializer
|
26
|
+
attributes :name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class BookSerializer < ActiveModel::Serializer
|
32
|
+
attributes :title, :author_name
|
33
|
+
end
|
34
|
+
test 'resource without a namespace' do
|
35
|
+
book = Book.new(title: 'A Post', author_name: 'hello')
|
36
|
+
|
37
|
+
# TODO: this should be able to pull up this serializer without explicitly specifying the serializer
|
38
|
+
# currently, with no options, it still uses the Api::V3 serializer
|
39
|
+
result = ActiveModelSerializers::SerializableResource.new(book, serializer: BookSerializer).serializable_hash
|
40
|
+
|
41
|
+
expected = { title: 'A Post', author_name: 'hello' }
|
42
|
+
assert_equal expected, result
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'resource with namespace' do
|
46
|
+
book = Book.new(title: 'A Post', author_name: 'hi')
|
47
|
+
|
48
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
49
|
+
|
50
|
+
expected = { title: 'A Post', author_name: 'hi', pages: nil, publisher: nil }
|
51
|
+
assert_equal expected, result
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'has_many with nested serializer under the namespace' do
|
55
|
+
page = Page.new(number: 1, text: 'hello')
|
56
|
+
book = Book.new(title: 'A Post', author_name: 'hi', pages: [page])
|
57
|
+
|
58
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
59
|
+
|
60
|
+
expected = {
|
61
|
+
title: 'A Post', author_name: 'hi',
|
62
|
+
publisher: nil,
|
63
|
+
pages: [{
|
64
|
+
number: 1, text: 'hello'
|
65
|
+
}]
|
66
|
+
}
|
67
|
+
assert_equal expected, result
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'belongs_to with nested serializer under the namespace' do
|
71
|
+
publisher = Publisher.new(name: 'Disney')
|
72
|
+
book = Book.new(title: 'A Post', author_name: 'hi', publisher: publisher)
|
73
|
+
|
74
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
75
|
+
|
76
|
+
expected = {
|
77
|
+
title: 'A Post', author_name: 'hi',
|
78
|
+
pages: nil,
|
79
|
+
publisher: {
|
80
|
+
name: 'Disney'
|
81
|
+
}
|
82
|
+
}
|
83
|
+
assert_equal expected, result
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -41,6 +41,7 @@ require 'active_support/core_ext/string/access'
|
|
41
41
|
|
42
42
|
# These files do not require any others and are needed
|
43
43
|
# to run the tests
|
44
|
+
require 'active_support/testing/autorun'
|
44
45
|
require 'active_support/testing/isolation'
|
45
46
|
|
46
47
|
module TestHelpers
|
@@ -74,6 +75,8 @@ module TestHelpers
|
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
77
|
-
|
78
|
-
|
78
|
+
module ActiveSupport
|
79
|
+
class TestCase
|
80
|
+
include TestHelpers::Generation
|
81
|
+
end
|
79
82
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Rails5Shims
|
2
2
|
module ControllerTests
|
3
3
|
# https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
|
4
|
-
REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr].freeze
|
4
|
+
REQUEST_KWARGS = [:params, :headers, :session, :flash, :method, :body, :xhr].freeze
|
5
5
|
|
6
6
|
def get(path, *args)
|
7
7
|
fold_kwargs!(args)
|
@@ -30,7 +30,12 @@ module Rails5Shims
|
|
30
30
|
return unless hash.respond_to?(:key)
|
31
31
|
Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg|
|
32
32
|
next unless hash.key?(kwarg)
|
33
|
-
|
33
|
+
value = hash.delete(kwarg)
|
34
|
+
if value.is_a? String
|
35
|
+
args.insert(0, value)
|
36
|
+
else
|
37
|
+
hash.merge! value
|
38
|
+
end
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
@@ -44,4 +49,5 @@ module Rails5Shims
|
|
44
49
|
end
|
45
50
|
if Rails::VERSION::MAJOR < 5
|
46
51
|
ActionController::TestCase.send :include, Rails5Shims::ControllerTests
|
52
|
+
ActionDispatch::IntegrationTest.send :include, Rails5Shims::ControllerTests
|
47
53
|
end
|
data/test/support/rails_app.rb
CHANGED
@@ -22,15 +22,6 @@ ActionController::TestCase.class_eval do
|
|
22
22
|
def setup
|
23
23
|
@routes = Routes
|
24
24
|
end
|
25
|
-
|
26
|
-
# For Rails5
|
27
|
-
# https://github.com/rails/rails/commit/ca83436d1b3b6cedd1eca2259f65661e69b01909#diff-b9bbf56e85d3fe1999f16317f2751e76L17
|
28
|
-
def assigns(key = nil)
|
29
|
-
warn "DEPRECATION: Calling 'assigns(#{key})' from #{caller[0]}"
|
30
|
-
assigns = {}.with_indifferent_access
|
31
|
-
@controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) }
|
32
|
-
key.nil? ? assigns : assigns[key]
|
33
|
-
end
|
34
25
|
end
|
35
26
|
|
36
27
|
# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
|
@@ -9,6 +9,22 @@ module SerializationTesting
|
|
9
9
|
ActiveModelSerializers::SerializableResource.new(obj).to_json
|
10
10
|
end
|
11
11
|
|
12
|
+
def with_namespace_separator(separator)
|
13
|
+
original_separator = ActiveModelSerializers.config.jsonapi_namespace_separator
|
14
|
+
ActiveModelSerializers.config.jsonapi_namespace_separator = separator
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
ActiveModelSerializers.config.jsonapi_namespace_separator = original_separator
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_prepended_lookup(lookup_proc)
|
21
|
+
original_lookup = ActiveModelSerializers.config.serializer_lookup_cahin
|
22
|
+
ActiveModelSerializers.config.serializer_lookup_chain.unshift lookup_proc
|
23
|
+
yield
|
24
|
+
ensure
|
25
|
+
ActiveModelSerializers.config.serializer_lookup_cahin = original_lookup
|
26
|
+
end
|
27
|
+
|
12
28
|
# Aliased as :with_configured_adapter to clarify that
|
13
29
|
# this method tests the configured adapter.
|
14
30
|
# When not testing configuration, it may be preferable
|
@@ -44,10 +60,12 @@ module SerializationTesting
|
|
44
60
|
end
|
45
61
|
end
|
46
62
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
63
|
+
module Minitest
|
64
|
+
class Test
|
65
|
+
def before_setup
|
66
|
+
ActionController::Base.cache_store.clear
|
67
|
+
end
|
51
68
|
|
52
|
-
|
69
|
+
include SerializationTesting
|
70
|
+
end
|
53
71
|
end
|