active_model_serializers 0.9.12 → 0.10.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +21 -0
  3. data/.travis.yml +27 -0
  4. data/CHANGELOG.md +8 -231
  5. data/CONTRIBUTING.md +23 -12
  6. data/Gemfile +17 -0
  7. data/{MIT-LICENSE → LICENSE.txt} +3 -2
  8. data/README.md +151 -781
  9. data/Rakefile +12 -0
  10. data/active_model_serializers.gemspec +26 -0
  11. data/lib/action_controller/serialization.rb +30 -84
  12. data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
  13. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
  14. data/lib/active_model/serializer/adapter/json.rb +52 -0
  15. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
  16. data/lib/active_model/serializer/adapter/json_api.rb +152 -0
  17. data/lib/active_model/serializer/adapter/null.rb +11 -0
  18. data/lib/active_model/serializer/adapter.rb +87 -0
  19. data/lib/active_model/serializer/array_serializer.rb +32 -0
  20. data/lib/active_model/serializer/configuration.rb +13 -0
  21. data/lib/active_model/serializer/fieldset.rb +40 -0
  22. data/lib/active_model/serializer/version.rb +1 -3
  23. data/lib/active_model/serializer.rb +192 -263
  24. data/lib/active_model_serializers.rb +5 -19
  25. data/lib/generators/serializer/USAGE +6 -0
  26. data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -10
  27. data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
  28. data/test/action_controller/adapter_selector_test.rb +51 -0
  29. data/test/action_controller/explicit_serializer_test.rb +110 -0
  30. data/test/action_controller/json_api_linked_test.rb +173 -0
  31. data/test/action_controller/serialization_scope_name_test.rb +63 -0
  32. data/test/action_controller/serialization_test.rb +365 -0
  33. data/test/adapter/fragment_cache_test.rb +27 -0
  34. data/test/adapter/json/belongs_to_test.rb +41 -0
  35. data/test/adapter/json/collection_test.rb +59 -0
  36. data/test/adapter/json/has_many_test.rb +36 -0
  37. data/test/adapter/json_api/belongs_to_test.rb +147 -0
  38. data/test/adapter/json_api/collection_test.rb +89 -0
  39. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  40. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  41. data/test/adapter/json_api/has_many_test.rb +106 -0
  42. data/test/adapter/json_api/has_one_test.rb +59 -0
  43. data/test/adapter/json_api/linked_test.rb +257 -0
  44. data/test/adapter/json_test.rb +34 -0
  45. data/test/adapter/null_test.rb +25 -0
  46. data/test/adapter_test.rb +43 -0
  47. data/test/array_serializer_test.rb +29 -0
  48. data/test/fixtures/poro.rb +123 -172
  49. data/test/serializers/adapter_for_test.rb +50 -0
  50. data/test/serializers/associations_test.rb +106 -0
  51. data/test/serializers/attribute_test.rb +23 -0
  52. data/test/serializers/attributes_test.rb +28 -0
  53. data/test/serializers/cache_test.rb +128 -0
  54. data/test/serializers/configuration_test.rb +15 -0
  55. data/test/serializers/fieldset_test.rb +26 -0
  56. data/test/serializers/generators_test.rb +59 -0
  57. data/test/serializers/meta_test.rb +78 -0
  58. data/test/serializers/options_test.rb +21 -0
  59. data/test/serializers/serializer_for_test.rb +56 -0
  60. data/test/serializers/urls_test.rb +26 -0
  61. data/test/test_helper.rb +20 -10
  62. metadata +121 -158
  63. data/DESIGN.textile +0 -586
  64. data/lib/action_controller/serialization_test_case.rb +0 -82
  65. data/lib/active_model/array_serializer.rb +0 -70
  66. data/lib/active_model/default_serializer.rb +0 -30
  67. data/lib/active_model/serializable/utils.rb +0 -18
  68. data/lib/active_model/serializable.rb +0 -61
  69. data/lib/active_model/serializer/association/has_many.rb +0 -41
  70. data/lib/active_model/serializer/association/has_one.rb +0 -27
  71. data/lib/active_model/serializer/association.rb +0 -58
  72. data/lib/active_model/serializer/config.rb +0 -33
  73. data/lib/active_model/serializer/generators/resource_override.rb +0 -15
  74. data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
  75. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
  76. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
  77. data/lib/active_model/serializer/railtie.rb +0 -24
  78. data/lib/active_model/serializer_support.rb +0 -7
  79. data/test/benchmark/app.rb +0 -60
  80. data/test/benchmark/benchmarking_support.rb +0 -67
  81. data/test/benchmark/bm_active_record.rb +0 -41
  82. data/test/benchmark/setup.rb +0 -75
  83. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  84. data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
  85. data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
  86. data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
  87. data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
  88. data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
  89. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  90. data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
  91. data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
  92. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  93. data/test/fixtures/active_record.rb +0 -96
  94. data/test/fixtures/template.html.erb +0 -1
  95. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
  96. data/test/integration/action_controller/serialization_test.rb +0 -287
  97. data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
  98. data/test/integration/active_record/active_record_test.rb +0 -94
  99. data/test/integration/generators/resource_generator_test.rb +0 -26
  100. data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
  101. data/test/integration/generators/serializer_generator_test.rb +0 -41
  102. data/test/test_app.rb +0 -18
  103. data/test/tmp/app/assets/javascripts/accounts.js +0 -2
  104. data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
  105. data/test/tmp/app/controllers/accounts_controller.rb +0 -3
  106. data/test/tmp/app/helpers/accounts_helper.rb +0 -3
  107. data/test/tmp/app/serializers/account_serializer.rb +0 -4
  108. data/test/tmp/config/routes.rb +0 -2
  109. data/test/unit/active_model/array_serializer/except_test.rb +0 -18
  110. data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
  111. data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
  112. data/test/unit/active_model/array_serializer/only_test.rb +0 -18
  113. data/test/unit/active_model/array_serializer/options_test.rb +0 -16
  114. data/test/unit/active_model/array_serializer/root_test.rb +0 -102
  115. data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
  116. data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
  117. data/test/unit/active_model/default_serializer_test.rb +0 -13
  118. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
  119. data/test/unit/active_model/serializer/associations_test.rb +0 -49
  120. data/test/unit/active_model/serializer/attributes_test.rb +0 -57
  121. data/test/unit/active_model/serializer/config_test.rb +0 -91
  122. data/test/unit/active_model/serializer/filter_test.rb +0 -69
  123. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  124. data/test/unit/active_model/serializer/has_many_test.rb +0 -265
  125. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  126. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  127. data/test/unit/active_model/serializer/has_one_test.rb +0 -253
  128. data/test/unit/active_model/serializer/key_format_test.rb +0 -25
  129. data/test/unit/active_model/serializer/meta_test.rb +0 -39
  130. data/test/unit/active_model/serializer/options_test.rb +0 -42
  131. data/test/unit/active_model/serializer/root_test.rb +0 -117
  132. data/test/unit/active_model/serializer/scope_test.rb +0 -49
  133. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
  134. data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ class CollectionTest < Minitest::Test
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @author.bio = nil
11
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
12
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
13
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
14
+ @first_post.comments = []
15
+ @second_post.comments = []
16
+ @first_post.blog = @blog
17
+ @second_post.blog = nil
18
+ @first_post.author = @author
19
+ @second_post.author = @author
20
+ @author.posts = [@first_post, @second_post]
21
+
22
+ @serializer = ArraySerializer.new([@first_post, @second_post])
23
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
24
+ ActionController::Base.cache_store.clear
25
+ end
26
+
27
+ def test_include_multiple_posts
28
+ expected = [
29
+ {
30
+ id: "1",
31
+ type: "posts",
32
+ title: "Hello!!",
33
+ body: "Hello, world!!",
34
+ links: {
35
+ comments: { linkage: [] },
36
+ blog: { linkage: { type: "blogs", id: "999" } },
37
+ author: { linkage: { type: "authors", id: "1" } }
38
+ }
39
+ },
40
+ {
41
+ id: "2",
42
+ type: "posts",
43
+ title: "New Post",
44
+ body: "Body",
45
+ links: {
46
+ comments: { linkage: [] },
47
+ blog: { linkage: { type: "blogs", id: "999" } },
48
+ author: { linkage: { type: "authors", id: "1" } }
49
+ }
50
+ }
51
+ ]
52
+
53
+ assert_equal(expected, @adapter.serializable_hash[:data])
54
+ end
55
+
56
+ def test_limiting_fields
57
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
58
+
59
+ expected = [
60
+ {
61
+ id: "1",
62
+ type: "posts",
63
+ title: "Hello!!",
64
+ links: {
65
+ comments: { linkage: [] },
66
+ blog: { linkage: { type: "blogs", id: "999" } },
67
+ author: { linkage: { type: "authors", id: "1" } }
68
+ }
69
+ },
70
+ {
71
+ id: "2",
72
+ type: "posts",
73
+ title: "New Post",
74
+ links: {
75
+ comments: { linkage: [] },
76
+ blog: { linkage: { type: "blogs", id: "999" } },
77
+ author: { linkage: { type: "authors", id: "1" } }
78
+ }
79
+ }
80
+ ]
81
+
82
+ assert_equal(expected, @adapter.serializable_hash[:data])
83
+ end
84
+
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ class HasManyEmbedIdsTest < Minitest::Test
8
+ def setup
9
+ @author = Author.new(name: 'Steve K.')
10
+ @author.bio = nil
11
+ @author.roles = nil
12
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
13
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
14
+ @author.posts = [@first_post, @second_post]
15
+ @first_post.author = @author
16
+ @second_post.author = @author
17
+ @first_post.comments = []
18
+ @second_post.comments = []
19
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
20
+ @first_post.blog = @blog
21
+ @second_post.blog = nil
22
+
23
+ @serializer = AuthorSerializer.new(@author)
24
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
25
+ end
26
+
27
+ def test_includes_comment_ids
28
+ expected = {
29
+ linkage: [
30
+ { type: "posts", id: "1"},
31
+ { type: "posts", id: "2"}
32
+ ]
33
+ }
34
+
35
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
36
+ end
37
+
38
+ def test_no_includes_linked_comments
39
+ assert_nil @adapter.serializable_hash[:linked]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,98 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ # Test 'has_many :assocs, serializer: AssocXSerializer'
8
+ class HasManyExplicitSerializerTest < Minitest::Test
9
+ def setup
10
+ @post = Post.new(title: 'New Post', body: 'Body')
11
+ @author = Author.new(name: 'Jane Blogger')
12
+ @author.posts = [@post]
13
+ @post.author = @author
14
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
15
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
16
+ @post.comments = [@first_comment, @second_comment]
17
+ @first_comment.post = @post
18
+ @first_comment.author = nil
19
+ @second_comment.post = @post
20
+ @second_comment.author = nil
21
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
22
+ @post.blog = @blog
23
+
24
+ @serializer = PostPreviewSerializer.new(@post)
25
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
26
+ @serializer,
27
+ include: ['comments', 'author']
28
+ )
29
+ end
30
+
31
+ def test_includes_comment_ids
32
+ expected = {
33
+ linkage: [
34
+ { type: 'comments', id: '1' },
35
+ { type: 'comments', id: '2' }
36
+ ]
37
+ }
38
+
39
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
40
+ end
41
+
42
+ def test_includes_linked_data
43
+ # If CommentPreviewSerializer is applied correctly the body text will not be present in the output
44
+ expected = [
45
+ {
46
+ id: '1',
47
+ type: 'comments',
48
+ links: {
49
+ post: { linkage: { type: 'posts', id: @post.id.to_s } }
50
+ }
51
+ },
52
+ {
53
+ id: '2',
54
+ type: 'comments',
55
+ links: {
56
+ post: { linkage: { type: 'posts', id: @post.id.to_s } }
57
+ }
58
+ },
59
+ {
60
+ id: @author.id.to_s,
61
+ type: "authors",
62
+ links: {
63
+ posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] }
64
+ }
65
+ }
66
+ ]
67
+
68
+ assert_equal(expected, @adapter.serializable_hash[:included])
69
+ end
70
+
71
+ def test_includes_author_id
72
+ expected = {
73
+ linkage: { type: "authors", id: @author.id.to_s }
74
+ }
75
+
76
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
77
+ end
78
+
79
+ def test_explicit_serializer_with_null_resource
80
+ @post.author = nil
81
+
82
+ expected = { linkage: nil }
83
+
84
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
85
+ end
86
+
87
+ def test_explicit_serializer_with_null_collection
88
+ @post.comments = []
89
+
90
+ expected = { linkage: [] }
91
+
92
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,106 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ class HasManyTest < Minitest::Test
8
+ def setup
9
+ ActionController::Base.cache_store.clear
10
+ @author = Author.new(id: 1, name: 'Steve K.')
11
+ @author.posts = []
12
+ @author.bio = nil
13
+ @post = Post.new(id: 1, title: 'New Post', body: 'Body')
14
+ @post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
15
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16
+ @first_comment.author = nil
17
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
18
+ @second_comment.author = nil
19
+ @post.comments = [@first_comment, @second_comment]
20
+ @post_without_comments.comments = []
21
+ @first_comment.post = @post
22
+ @second_comment.post = @post
23
+ @post.author = @author
24
+ @post_without_comments.author = nil
25
+ @blog = Blog.new(id: 1, name: "My Blog!!")
26
+ @blog.writer = @author
27
+ @blog.articles = [@post]
28
+ @post.blog = @blog
29
+ @post_without_comments.blog = nil
30
+
31
+ @serializer = PostSerializer.new(@post)
32
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
33
+ end
34
+
35
+ def test_includes_comment_ids
36
+ expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
37
+
38
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
39
+ end
40
+
41
+ def test_includes_linked_comments
42
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
43
+ expected = [{
44
+ id: "1",
45
+ type: "comments",
46
+ body: 'ZOMG A COMMENT',
47
+ links: {
48
+ post: { linkage: { type: "posts", id: "1" } },
49
+ author: { linkage: nil }
50
+ }
51
+ }, {
52
+ id: "2",
53
+ type: "comments",
54
+ body: 'ZOMG ANOTHER COMMENT',
55
+ links: {
56
+ post: { linkage: { type: "posts", id: "1" } },
57
+ author: { linkage: nil }
58
+ }
59
+ }]
60
+ assert_equal expected, @adapter.serializable_hash[:included]
61
+ end
62
+
63
+ def test_limit_fields_of_linked_comments
64
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
65
+ expected = [{
66
+ id: "1",
67
+ type: "comments",
68
+ links: {
69
+ post: { linkage: { type: "posts", id: "1" } },
70
+ author: { linkage: nil }
71
+ }
72
+ }, {
73
+ id: "2",
74
+ type: "comments",
75
+ links: {
76
+ post: { linkage: { type: "posts", id: "1" } },
77
+ author: { linkage: nil }
78
+ }
79
+ }]
80
+ assert_equal expected, @adapter.serializable_hash[:included]
81
+ end
82
+
83
+ def test_no_include_linked_if_comments_is_empty
84
+ serializer = PostSerializer.new(@post_without_comments)
85
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
86
+
87
+ assert_nil adapter.serializable_hash[:linked]
88
+ end
89
+
90
+ def test_include_type_for_association_when_different_than_name
91
+ serializer = BlogSerializer.new(@blog)
92
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
93
+ actual = adapter.serializable_hash[:data][:links][:articles]
94
+ expected = {
95
+ linkage: [{
96
+ type: "posts",
97
+ id: "1"
98
+ }]
99
+ }
100
+ assert_equal expected, actual
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ class HasOneTest < Minitest::Test
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @bio = Bio.new(id: 43, content: 'AMS Contributor')
11
+ @author.bio = @bio
12
+ @bio.author = @author
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
+ @anonymous_post.comments = []
18
+ @comment.post = @post
19
+ @comment.author = nil
20
+ @post.author = @author
21
+ @anonymous_post.author = nil
22
+ @blog = Blog.new(id: 1, name: "My Blog!!")
23
+ @blog.writer = @author
24
+ @blog.articles = [@post, @anonymous_post]
25
+ @author.posts = []
26
+ @author.roles = []
27
+
28
+ @serializer = AuthorSerializer.new(@author)
29
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
30
+ end
31
+
32
+ def test_includes_bio_id
33
+ expected = { linkage: { type: "bios", id: "43" } }
34
+
35
+ assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
36
+ end
37
+
38
+ def test_includes_linked_bio
39
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
40
+
41
+ expected = [
42
+ {
43
+ id: "43",
44
+ rating: nil,
45
+ type: "bios",
46
+ content:"AMS Contributor",
47
+ links: {
48
+ author: { linkage: { type: "authors", id: "1" } }
49
+ }
50
+ }
51
+ ]
52
+
53
+ assert_equal(expected, @adapter.serializable_hash[:included])
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,257 @@
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
+ title: "Hello!!",
58
+ body: "Hello, world!!",
59
+ type: "posts",
60
+ links: {
61
+ comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
62
+ blog: { linkage: { type: "blogs", id: "999" } },
63
+ author: { linkage: { type: "authors", id: "1" } }
64
+ }
65
+ },
66
+ {
67
+ id: "20",
68
+ title: "New Post",
69
+ body: "Body",
70
+ type: "posts",
71
+ links: {
72
+ comments: { linkage: [] },
73
+ blog: { linkage: { type: "blogs", id: "999" } },
74
+ author: { linkage: { type: "authors", id: "2" } }
75
+ }
76
+ }
77
+ ],
78
+ included: [
79
+ {
80
+ id: "1",
81
+ body: "ZOMG A COMMENT",
82
+ type: "comments",
83
+ links: {
84
+ post: { linkage: { type: "posts", id: "10" } },
85
+ author: { linkage: nil }
86
+ }
87
+ }, {
88
+ id: "2",
89
+ body: "ZOMG ANOTHER COMMENT",
90
+ type: "comments",
91
+ links: {
92
+ post: { linkage: { type: "posts", id: "10" } },
93
+ author: { linkage: nil }
94
+ }
95
+ }, {
96
+ id: "1",
97
+ name: "Steve K.",
98
+ type: "authors",
99
+ links: {
100
+ posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
101
+ roles: { linkage: [] },
102
+ bio: { linkage: { type: "bios", id: "1" } }
103
+ }
104
+ }, {
105
+ id: "1",
106
+ rating: nil,
107
+ type: "bios",
108
+ content: "AMS Contributor",
109
+ links: {
110
+ author: { linkage: { type: "authors", id: "1" } }
111
+ }
112
+ }, {
113
+ id: "2",
114
+ name: "Tenderlove",
115
+ type: "authors",
116
+ links: {
117
+ posts: { linkage: [ { type: "posts", id:"20" } ] },
118
+ roles: { linkage: [] },
119
+ bio: { linkage: { type: "bios", id: "2" } }
120
+ }
121
+ }, {
122
+ id: "2",
123
+ rating: nil,
124
+ type: "bios",
125
+ content: "Rails Contributor",
126
+ links: {
127
+ author: { linkage: { type: "authors", id: "2" } }
128
+ }
129
+ }
130
+ ]
131
+ }
132
+ assert_equal expected, adapter.serializable_hash
133
+ assert_equal expected, alt_adapter.serializable_hash
134
+ end
135
+
136
+ def test_include_multiple_posts_and_linked
137
+ serializer = BioSerializer.new @bio1
138
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
139
+ serializer,
140
+ include: ['author', 'author.posts']
141
+ )
142
+ alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
143
+ serializer,
144
+ include: 'author,author.posts'
145
+ )
146
+
147
+ expected = [
148
+ {
149
+ id: "1",
150
+ type: "authors",
151
+ name: "Steve K.",
152
+ links: {
153
+ posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
154
+ roles: { linkage: [] },
155
+ bio: { linkage: { type: "bios", id: "1" }}
156
+ }
157
+ }, {
158
+ id: "10",
159
+ type: "posts",
160
+ title: "Hello!!",
161
+ body: "Hello, world!!",
162
+ links: {
163
+ comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
164
+ blog: { linkage: { type: "blogs", id: "999" } },
165
+ author: { linkage: { type: "authors", id: "1" } }
166
+ }
167
+ }, {
168
+ id: "30",
169
+ type: "posts",
170
+ title: "Yet Another Post",
171
+ body: "Body",
172
+ links: {
173
+ comments: { linkage: [] },
174
+ blog: { linkage: { type: "blogs", id: "999" } },
175
+ author: { linkage: { type: "authors", id: "1" } }
176
+ }
177
+ }
178
+ ]
179
+
180
+ assert_equal expected, adapter.serializable_hash[:included]
181
+ assert_equal expected, alt_adapter.serializable_hash[:included]
182
+ end
183
+
184
+ def test_ignore_model_namespace_for_linked_resource_type
185
+ spammy_post = Post.new(id: 123)
186
+ spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
187
+ serializer = SpammyPostSerializer.new(spammy_post)
188
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
189
+ links = adapter.serializable_hash[:data][:links]
190
+ expected = {
191
+ related: {
192
+ linkage: [{
193
+ type: 'unrelated_links',
194
+ id: '456'
195
+ }]
196
+ }
197
+ }
198
+ assert_equal expected, links
199
+ end
200
+
201
+ def test_multiple_references_to_same_resource
202
+ serializer = ArraySerializer.new([@first_comment, @second_comment])
203
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
204
+ serializer,
205
+ include: ['post']
206
+ )
207
+
208
+ expected = [
209
+ {
210
+ id: "10",
211
+ title: "Hello!!",
212
+ body: "Hello, world!!",
213
+ type: "posts",
214
+ links: {
215
+ comments: {
216
+ linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
217
+ },
218
+ blog: {
219
+ linkage: {type: "blogs", id: "999"}
220
+ },
221
+ author: {
222
+ linkage: {type: "authors", id: "1"}
223
+ }
224
+ }
225
+ }
226
+ ]
227
+
228
+ assert_equal expected, adapter.serializable_hash[:included]
229
+ end
230
+
231
+ def test_nil_link_with_specified_serializer
232
+ @first_post.author = nil
233
+ serializer = PostPreviewSerializer.new(@first_post)
234
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
235
+ serializer,
236
+ include: ['author']
237
+ )
238
+
239
+ expected = {
240
+ data: {
241
+ id: "10",
242
+ title: "Hello!!",
243
+ body: "Hello, world!!",
244
+ type: "posts",
245
+ links: {
246
+ comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
247
+ author: { linkage: nil }
248
+ }
249
+ }
250
+ }
251
+ assert_equal expected, adapter.serializable_hash
252
+ end
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,34 @@
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(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[:comments])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+