active_model_serializers 0.10.9 → 0.10.13

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.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -2
  3. data/README.md +18 -19
  4. data/lib/action_controller/serialization.rb +9 -1
  5. data/lib/active_model/serializer/collection_serializer.rb +11 -2
  6. data/lib/active_model/serializer/concerns/caching.rb +2 -1
  7. data/lib/active_model/serializer/fieldset.rb +1 -1
  8. data/lib/active_model/serializer/reflection.rb +4 -4
  9. data/lib/active_model/serializer/version.rb +1 -1
  10. data/lib/active_model/serializer.rb +13 -4
  11. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  12. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +5 -1
  13. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  14. data/lib/active_model_serializers/model/caching.rb +26 -0
  15. metadata +20 -292
  16. data/.github/ISSUE_TEMPLATE.md +0 -29
  17. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  18. data/.gitignore +0 -35
  19. data/.rubocop.yml +0 -109
  20. data/.simplecov +0 -110
  21. data/.travis.yml +0 -63
  22. data/CODE_OF_CONDUCT.md +0 -74
  23. data/CONTRIBUTING.md +0 -105
  24. data/Gemfile +0 -74
  25. data/Rakefile +0 -76
  26. data/active_model_serializers.gemspec +0 -64
  27. data/appveyor.yml +0 -28
  28. data/bin/bench +0 -171
  29. data/bin/bench_regression +0 -316
  30. data/bin/rubocop +0 -38
  31. data/bin/serve_benchmark +0 -39
  32. data/docs/README.md +0 -41
  33. data/docs/STYLE.md +0 -58
  34. data/docs/general/adapters.md +0 -269
  35. data/docs/general/caching.md +0 -58
  36. data/docs/general/configuration_options.md +0 -185
  37. data/docs/general/deserialization.md +0 -100
  38. data/docs/general/fields.md +0 -31
  39. data/docs/general/getting_started.md +0 -133
  40. data/docs/general/instrumentation.md +0 -40
  41. data/docs/general/key_transforms.md +0 -40
  42. data/docs/general/logging.md +0 -21
  43. data/docs/general/rendering.md +0 -293
  44. data/docs/general/serializers.md +0 -495
  45. data/docs/how-open-source-maintained.jpg +0 -0
  46. data/docs/howto/add_pagination_links.md +0 -138
  47. data/docs/howto/add_relationship_links.md +0 -140
  48. data/docs/howto/add_root_key.md +0 -62
  49. data/docs/howto/grape_integration.md +0 -42
  50. data/docs/howto/outside_controller_use.md +0 -66
  51. data/docs/howto/passing_arbitrary_options.md +0 -27
  52. data/docs/howto/serialize_poro.md +0 -73
  53. data/docs/howto/test.md +0 -154
  54. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  55. data/docs/integrations/ember-and-json-api.md +0 -147
  56. data/docs/integrations/grape.md +0 -19
  57. data/docs/jsonapi/errors.md +0 -56
  58. data/docs/jsonapi/schema/schema.json +0 -366
  59. data/docs/jsonapi/schema.md +0 -151
  60. data/docs/rfcs/0000-namespace.md +0 -106
  61. data/docs/rfcs/template.md +0 -15
  62. data/test/action_controller/adapter_selector_test.rb +0 -64
  63. data/test/action_controller/explicit_serializer_test.rb +0 -137
  64. data/test/action_controller/json/include_test.rb +0 -248
  65. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  66. data/test/action_controller/json_api/errors_test.rb +0 -42
  67. data/test/action_controller/json_api/fields_test.rb +0 -68
  68. data/test/action_controller/json_api/linked_test.rb +0 -204
  69. data/test/action_controller/json_api/pagination_test.rb +0 -126
  70. data/test/action_controller/json_api/transform_test.rb +0 -191
  71. data/test/action_controller/lookup_proc_test.rb +0 -51
  72. data/test/action_controller/namespace_lookup_test.rb +0 -234
  73. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  74. data/test/action_controller/serialization_test.rb +0 -480
  75. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  76. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  77. data/test/active_model_serializers/logging_test.rb +0 -79
  78. data/test/active_model_serializers/model_test.rb +0 -144
  79. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  80. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  81. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  82. data/test/active_model_serializers/test/schema_test.rb +0 -133
  83. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  84. data/test/active_record_test.rb +0 -11
  85. data/test/adapter/attributes_test.rb +0 -42
  86. data/test/adapter/deprecation_test.rb +0 -102
  87. data/test/adapter/json/belongs_to_test.rb +0 -47
  88. data/test/adapter/json/collection_test.rb +0 -106
  89. data/test/adapter/json/has_many_test.rb +0 -55
  90. data/test/adapter/json/transform_test.rb +0 -95
  91. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  92. data/test/adapter/json_api/collection_test.rb +0 -98
  93. data/test/adapter/json_api/errors_test.rb +0 -78
  94. data/test/adapter/json_api/fields_test.rb +0 -98
  95. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  96. data/test/adapter/json_api/has_many_test.rb +0 -175
  97. data/test/adapter/json_api/has_one_test.rb +0 -82
  98. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  99. data/test/adapter/json_api/json_api_test.rb +0 -35
  100. data/test/adapter/json_api/linked_test.rb +0 -415
  101. data/test/adapter/json_api/links_test.rb +0 -112
  102. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  103. data/test/adapter/json_api/parse_test.rb +0 -139
  104. data/test/adapter/json_api/relationship_test.rb +0 -399
  105. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  106. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  107. data/test/adapter/json_api/transform_test.rb +0 -514
  108. data/test/adapter/json_api/type_test.rb +0 -195
  109. data/test/adapter/json_test.rb +0 -48
  110. data/test/adapter/null_test.rb +0 -24
  111. data/test/adapter/polymorphic_test.rb +0 -220
  112. data/test/adapter_test.rb +0 -69
  113. data/test/array_serializer_test.rb +0 -24
  114. data/test/benchmark/app.rb +0 -67
  115. data/test/benchmark/benchmarking_support.rb +0 -69
  116. data/test/benchmark/bm_active_record.rb +0 -83
  117. data/test/benchmark/bm_adapter.rb +0 -40
  118. data/test/benchmark/bm_caching.rb +0 -121
  119. data/test/benchmark/bm_lookup_chain.rb +0 -85
  120. data/test/benchmark/bm_transform.rb +0 -47
  121. data/test/benchmark/config.ru +0 -3
  122. data/test/benchmark/controllers.rb +0 -85
  123. data/test/benchmark/fixtures.rb +0 -221
  124. data/test/cache_test.rb +0 -717
  125. data/test/collection_serializer_test.rb +0 -129
  126. data/test/fixtures/active_record.rb +0 -115
  127. data/test/fixtures/poro.rb +0 -227
  128. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  129. data/test/generators/serializer_generator_test.rb +0 -77
  130. data/test/grape_test.rb +0 -198
  131. data/test/lint_test.rb +0 -51
  132. data/test/logger_test.rb +0 -22
  133. data/test/poro_test.rb +0 -11
  134. data/test/serializable_resource_test.rb +0 -81
  135. data/test/serializers/association_macros_test.rb +0 -39
  136. data/test/serializers/associations_test.rb +0 -520
  137. data/test/serializers/attribute_test.rb +0 -155
  138. data/test/serializers/attributes_test.rb +0 -54
  139. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  140. data/test/serializers/configuration_test.rb +0 -34
  141. data/test/serializers/fieldset_test.rb +0 -16
  142. data/test/serializers/meta_test.rb +0 -204
  143. data/test/serializers/options_test.rb +0 -34
  144. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  145. data/test/serializers/reflection_test.rb +0 -481
  146. data/test/serializers/root_test.rb +0 -23
  147. data/test/serializers/serialization_test.rb +0 -57
  148. data/test/serializers/serializer_for_test.rb +0 -138
  149. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  150. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  151. data/test/support/isolated_unit.rb +0 -86
  152. data/test/support/rails5_shims.rb +0 -55
  153. data/test/support/rails_app.rb +0 -40
  154. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  156. data/test/support/schemas/custom/show.json +0 -7
  157. data/test/support/schemas/hyper_schema.json +0 -93
  158. data/test/support/schemas/render_using_json_api.json +0 -43
  159. data/test/support/schemas/simple_json_pointers.json +0 -10
  160. data/test/support/serialization_testing.rb +0 -81
  161. data/test/test_helper.rb +0 -72
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Test
7
- class SerializerTest < ActionController::TestCase
8
- include ActiveModelSerializers::Test::Serializer
9
-
10
- class MyController < ActionController::Base
11
- def render_using_serializer
12
- render json: Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
13
- end
14
-
15
- def render_some_text
16
- render(plain: 'ok')
17
- end
18
- end
19
-
20
- tests MyController
21
-
22
- def test_supports_specifying_serializers_with_a_serializer_class
23
- get :render_using_serializer
24
- assert_serializer ProfileSerializer
25
- end
26
-
27
- def test_supports_specifying_serializers_with_a_regexp
28
- get :render_using_serializer
29
- assert_serializer(/\AProfile.+\Z/)
30
- end
31
-
32
- def test_supports_specifying_serializers_with_a_string
33
- get :render_using_serializer
34
- assert_serializer 'ProfileSerializer'
35
- end
36
-
37
- def test_supports_specifying_serializers_with_a_symbol
38
- get :render_using_serializer
39
- assert_serializer :profile_serializer
40
- end
41
-
42
- def test_supports_specifying_serializers_with_a_nil
43
- get :render_some_text
44
- assert_serializer nil
45
- end
46
-
47
- def test_raises_descriptive_error_message_when_serializer_was_not_rendered
48
- get :render_using_serializer
49
- e = assert_raise ActiveSupport::TestCase::Assertion do
50
- assert_serializer 'PostSerializer'
51
- end
52
- assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message
53
- end
54
-
55
- def test_raises_argument_error_when_asserting_with_invalid_object
56
- get :render_using_serializer
57
- e = assert_raise ArgumentError do
58
- assert_serializer Hash
59
- end
60
- assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message
61
- end
62
- end
63
- end
64
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ActiveRecordTest < ActiveSupport::TestCase
6
- include ActiveModel::Serializer::Lint::Tests
7
-
8
- def setup
9
- @resource = ARModels::Post.new
10
- end
11
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class AttributesTest < ActiveSupport::TestCase
8
- class Person < ActiveModelSerializers::Model
9
- attributes :first_name, :last_name
10
- end
11
-
12
- class PersonSerializer < ActiveModel::Serializer
13
- attributes :first_name, :last_name
14
- end
15
-
16
- def setup
17
- ActionController::Base.cache_store.clear
18
- end
19
-
20
- def test_serializable_hash
21
- person = Person.new(first_name: 'Arthur', last_name: 'Dent')
22
- serializer = PersonSerializer.new(person)
23
- adapter = ActiveModelSerializers::Adapter::Attributes.new(serializer)
24
-
25
- assert_equal({ first_name: 'Arthur', last_name: 'Dent' },
26
- adapter.serializable_hash)
27
- end
28
-
29
- def test_serializable_hash_with_transform_key_casing
30
- person = Person.new(first_name: 'Arthur', last_name: 'Dent')
31
- serializer = PersonSerializer.new(person)
32
- adapter = ActiveModelSerializers::Adapter::Attributes.new(
33
- serializer,
34
- key_transform: :camel_lower
35
- )
36
-
37
- assert_equal({ firstName: 'Arthur', lastName: 'Dent' },
38
- adapter.serializable_hash)
39
- end
40
- end
41
- end
42
- end
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
- module ActiveModel
5
- class Serializer
6
- module Adapter
7
- class DeprecationTest < ActiveSupport::TestCase
8
- class PostSerializer < ActiveModel::Serializer
9
- attribute :body
10
- end
11
- setup do
12
- post = Post.new(id: 1, body: 'Hello')
13
- @serializer = PostSerializer.new(post)
14
- end
15
-
16
- def test_null_adapter_serialization_deprecation
17
- expected = {}
18
- assert_deprecated do
19
- assert_equal(expected, Null.new(@serializer).as_json)
20
- end
21
- end
22
-
23
- def test_json_adapter_serialization_deprecation
24
- expected = { post: { body: 'Hello' } }
25
- assert_deprecated do
26
- assert_equal(expected, Json.new(@serializer).as_json)
27
- end
28
- end
29
-
30
- def test_jsonapi_adapter_serialization_deprecation
31
- expected = {
32
- data: {
33
- id: '1',
34
- type: 'posts',
35
- attributes: {
36
- body: 'Hello'
37
- }
38
- }
39
- }
40
- assert_deprecated do
41
- assert_equal(expected, JsonApi.new(@serializer).as_json)
42
- end
43
- end
44
-
45
- def test_attributes_adapter_serialization_deprecation
46
- expected = { body: 'Hello' }
47
- assert_deprecated do
48
- assert_equal(expected, Attributes.new(@serializer).as_json)
49
- end
50
- end
51
-
52
- def test_adapter_create_deprecation
53
- assert_deprecated do
54
- Adapter.create(@serializer)
55
- end
56
- end
57
-
58
- def test_adapter_adapter_map_deprecation
59
- assert_deprecated do
60
- Adapter.adapter_map
61
- end
62
- end
63
-
64
- def test_adapter_adapters_deprecation
65
- assert_deprecated do
66
- Adapter.adapters
67
- end
68
- end
69
-
70
- def test_adapter_adapter_class_deprecation
71
- assert_deprecated do
72
- Adapter.adapter_class(:json_api)
73
- end
74
- end
75
-
76
- def test_adapter_register_deprecation
77
- assert_deprecated do
78
- begin
79
- Adapter.register(:test, Class.new)
80
- ensure
81
- Adapter.adapter_map.delete('test')
82
- end
83
- end
84
- end
85
-
86
- def test_adapter_lookup_deprecation
87
- assert_deprecated do
88
- Adapter.lookup(:json_api)
89
- end
90
- end
91
-
92
- private
93
-
94
- def assert_deprecated
95
- assert_output(nil, /deprecated/) do
96
- yield
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class Json
8
- class BelongsToTest < ActiveSupport::TestCase
9
- def setup
10
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
11
- @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
12
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
13
- @post.comments = [@comment]
14
- @anonymous_post.comments = []
15
- @comment.post = @post
16
- @comment.author = nil
17
- @anonymous_post.author = nil
18
- @blog = Blog.new(id: 1, name: 'My Blog!!')
19
- @post.blog = @blog
20
- @anonymous_post.blog = nil
21
-
22
- @serializer = CommentSerializer.new(@comment)
23
- @adapter = ActiveModelSerializers::Adapter::Json.new(@serializer)
24
- ActionController::Base.cache_store.clear
25
- end
26
-
27
- def test_includes_post
28
- assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
29
- end
30
-
31
- def test_include_nil_author
32
- serializer = PostSerializer.new(@anonymous_post)
33
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
34
-
35
- assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
36
- end
37
-
38
- def test_include_nil_author_with_specified_serializer
39
- serializer = PostPreviewSerializer.new(@anonymous_post)
40
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
41
-
42
- assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class Json
8
- class Collection < ActiveSupport::TestCase
9
- def setup
10
- @author = Author.new(id: 1, name: 'Steve K.')
11
- @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
12
- @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
13
- @first_post.comments = []
14
- @second_post.comments = []
15
- @first_post.author = @author
16
- @second_post.author = @author
17
- @blog = Blog.new(id: 1, name: 'My Blog!!')
18
- @first_post.blog = @blog
19
- @second_post.blog = nil
20
-
21
- ActionController::Base.cache_store.clear
22
- end
23
-
24
- def test_with_serializer_option
25
- @blog.special_attribute = 'Special'
26
- @blog.articles = [@first_post, @second_post]
27
- serializer = ActiveModel::Serializer::CollectionSerializer.new([@blog], serializer: CustomBlogSerializer)
28
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
29
-
30
- expected = { blogs: [{
31
- id: 1,
32
- special_attribute: 'Special',
33
- articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
34
- }] }
35
- assert_equal expected, adapter.serializable_hash
36
- end
37
-
38
- def test_include_multiple_posts
39
- serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
40
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
41
-
42
- expected = { posts: [{
43
- title: 'Hello!!',
44
- body: 'Hello, world!!',
45
- id: 1,
46
- comments: [],
47
- author: {
48
- id: 1,
49
- name: 'Steve K.'
50
- },
51
- blog: {
52
- id: 999,
53
- name: 'Custom blog'
54
- }
55
- }, {
56
- title: 'New Post',
57
- body: 'Body',
58
- id: 2,
59
- comments: [],
60
- author: {
61
- id: 1,
62
- name: 'Steve K.'
63
- },
64
- blog: {
65
- id: 999,
66
- name: 'Custom blog'
67
- }
68
- }] }
69
- assert_equal expected, adapter.serializable_hash
70
- end
71
-
72
- def test_root_is_underscored
73
- virtual_value = VirtualValue.new(id: 1)
74
- serializer = ActiveModel::Serializer::CollectionSerializer.new([virtual_value])
75
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
76
-
77
- assert_equal 1, adapter.serializable_hash[:virtual_values].length
78
- end
79
-
80
- def test_include_option
81
- serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
82
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer, include: '')
83
- actual = adapter.serializable_hash
84
- expected = { posts: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' },
85
- { id: 2, title: 'New Post', body: 'Body' }] }
86
-
87
- assert_equal(expected, actual)
88
- end
89
-
90
- def test_fields_with_no_associations_include_option
91
- actual = ActiveModelSerializers::SerializableResource.new(
92
- [@first_post, @second_post], adapter: :json, fields: [:id], include: []
93
- ).as_json
94
-
95
- expected = { posts: [{
96
- id: 1
97
- }, {
98
- id: 2
99
- }] }
100
-
101
- assert_equal(expected, actual)
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class Json
8
- class HasManyTestTest < ActiveSupport::TestCase
9
- class ModelWithoutSerializer < ::Model
10
- attributes :id, :name
11
- end
12
-
13
- def setup
14
- ActionController::Base.cache_store.clear
15
- @author = Author.new(id: 1, name: 'Steve K.')
16
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
17
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
18
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
19
- @post.comments = [@first_comment, @second_comment]
20
- @post.author = @author
21
- @first_comment.post = @post
22
- @second_comment.post = @post
23
- @blog = Blog.new(id: 1, name: 'My Blog!!')
24
- @post.blog = @blog
25
- @tag = ModelWithoutSerializer.new(id: 1, name: '#hash_tag')
26
- @post.tags = [@tag]
27
- end
28
-
29
- def test_has_many
30
- serializer = PostSerializer.new(@post)
31
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
32
- assert_equal([
33
- { id: 1, body: 'ZOMG A COMMENT' },
34
- { id: 2, body: 'ZOMG ANOTHER COMMENT' }
35
- ], adapter.serializable_hash[:post][:comments])
36
- end
37
-
38
- def test_has_many_with_no_serializer
39
- post_serializer_class = Class.new(ActiveModel::Serializer) do
40
- attributes :id
41
- has_many :tags
42
- end
43
- serializer = post_serializer_class.new(@post)
44
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
45
- assert_equal({
46
- id: 42,
47
- tags: [
48
- { 'id' => 1, 'name' => '#hash_tag' }
49
- ]
50
- }.to_json, adapter.serializable_hash[:post].to_json)
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,95 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class Json
8
- class KeyCaseTest < ActiveSupport::TestCase
9
- def mock_request(key_transform = nil)
10
- context = Minitest::Mock.new
11
- context.expect(:request_url, URI)
12
- context.expect(:query_parameters, {})
13
- options = {}
14
- options[:key_transform] = key_transform if key_transform
15
- options[:serialization_context] = context
16
- serializer = CustomBlogSerializer.new(@blog)
17
- @adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options)
18
- end
19
-
20
- class Post < ::Model; end
21
- class PostSerializer < ActiveModel::Serializer
22
- attributes :id, :title, :body, :publish_at
23
- end
24
-
25
- setup do
26
- ActionController::Base.cache_store.clear
27
- @blog = Blog.new(id: 1, name: 'My Blog!!', special_attribute: 'neat')
28
- end
29
-
30
- def test_transform_default
31
- mock_request
32
- assert_equal({
33
- blog: { id: 1, special_attribute: 'neat', articles: nil }
34
- }, @adapter.serializable_hash)
35
- end
36
-
37
- def test_transform_global_config
38
- mock_request
39
- result = with_config(key_transform: :camel_lower) do
40
- @adapter.serializable_hash
41
- end
42
- assert_equal({
43
- blog: { id: 1, specialAttribute: 'neat', articles: nil }
44
- }, result)
45
- end
46
-
47
- def test_transform_serialization_ctx_overrides_global_config
48
- mock_request(:camel)
49
- result = with_config(key_transform: :camel_lower) do
50
- @adapter.serializable_hash
51
- end
52
- assert_equal({
53
- Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
54
- }, result)
55
- end
56
-
57
- def test_transform_undefined
58
- mock_request(:blam)
59
- result = nil
60
- assert_raises NoMethodError do
61
- result = @adapter.serializable_hash
62
- end
63
- end
64
-
65
- def test_transform_dash
66
- mock_request(:dash)
67
- assert_equal({
68
- blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
69
- }, @adapter.serializable_hash)
70
- end
71
-
72
- def test_transform_unaltered
73
- mock_request(:unaltered)
74
- assert_equal({
75
- blog: { id: 1, special_attribute: 'neat', articles: nil }
76
- }, @adapter.serializable_hash)
77
- end
78
-
79
- def test_transform_camel
80
- mock_request(:camel)
81
- assert_equal({
82
- Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
83
- }, @adapter.serializable_hash)
84
- end
85
-
86
- def test_transform_camel_lower
87
- mock_request(:camel_lower)
88
- assert_equal({
89
- blog: { id: 1, specialAttribute: 'neat', articles: nil }
90
- }, @adapter.serializable_hash)
91
- end
92
- end
93
- end
94
- end
95
- end
@@ -1,157 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class JsonApi
8
- class BelongsToTest < ActiveSupport::TestCase
9
- def setup
10
- @author = Author.new(id: 1, name: 'Steve K.')
11
- @author.bio = nil
12
- @author.roles = []
13
- @blog = Blog.new(id: 23, name: 'AMS Blog')
14
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
15
- @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
16
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
17
- @post.comments = [@comment]
18
- @post.blog = @blog
19
- @anonymous_post.comments = []
20
- @anonymous_post.blog = nil
21
- @comment.post = @post
22
- @comment.author = nil
23
- @post.author = @author
24
- @anonymous_post.author = nil
25
- @blog = Blog.new(id: 1, name: 'My Blog!!')
26
- @blog.writer = @author
27
- @blog.articles = [@post, @anonymous_post]
28
- @author.posts = []
29
-
30
- @serializer = CommentSerializer.new(@comment)
31
- @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
32
- ActionController::Base.cache_store.clear
33
- end
34
-
35
- def test_includes_post_id
36
- expected = { data: { type: 'posts', id: '42' } }
37
-
38
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
39
- end
40
-
41
- def test_includes_linked_post
42
- @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post])
43
- expected = [{
44
- id: '42',
45
- type: 'posts',
46
- attributes: {
47
- title: 'New Post',
48
- body: 'Body'
49
- },
50
- relationships: {
51
- comments: { data: [{ type: 'comments', id: '1' }] },
52
- blog: { data: { type: 'blogs', id: '999' } },
53
- author: { data: { type: 'authors', id: '1' } }
54
- }
55
- }]
56
- assert_equal expected, @adapter.serializable_hash[:included]
57
- end
58
-
59
- def test_limiting_linked_post_fields
60
- @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title, :comments, :blog, :author] })
61
- expected = [{
62
- id: '42',
63
- type: 'posts',
64
- attributes: {
65
- title: 'New Post'
66
- },
67
- relationships: {
68
- comments: { data: [{ type: 'comments', id: '1' }] },
69
- blog: { data: { type: 'blogs', id: '999' } },
70
- author: { data: { type: 'authors', id: '1' } }
71
- }
72
- }]
73
- assert_equal expected, @adapter.serializable_hash[:included]
74
- end
75
-
76
- def test_include_nil_author
77
- serializer = PostSerializer.new(@anonymous_post)
78
- adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
79
-
80
- assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
81
- end
82
-
83
- def test_include_type_for_association_when_different_than_name
84
- serializer = BlogSerializer.new(@blog)
85
- adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
86
- relationships = adapter.serializable_hash[:data][:relationships]
87
- expected = {
88
- writer: {
89
- data: {
90
- type: 'authors',
91
- id: '1'
92
- }
93
- },
94
- articles: {
95
- data: [
96
- {
97
- type: 'posts',
98
- id: '42'
99
- },
100
- {
101
- type: 'posts',
102
- id: '43'
103
- }
104
- ]
105
- }
106
- }
107
- assert_equal expected, relationships
108
- end
109
-
110
- def test_include_linked_resources_with_type_name
111
- serializer = BlogSerializer.new(@blog)
112
- adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, include: [:writer, :articles])
113
- linked = adapter.serializable_hash[:included]
114
- expected = [
115
- {
116
- id: '1',
117
- type: 'authors',
118
- attributes: {
119
- name: 'Steve K.'
120
- },
121
- relationships: {
122
- posts: { data: [] },
123
- roles: { data: [] },
124
- bio: { data: nil }
125
- }
126
- }, {
127
- id: '42',
128
- type: 'posts',
129
- attributes: {
130
- title: 'New Post',
131
- body: 'Body'
132
- },
133
- relationships: {
134
- comments: { data: [{ type: 'comments', id: '1' }] },
135
- blog: { data: { type: 'blogs', id: '999' } },
136
- author: { data: { type: 'authors', id: '1' } }
137
- }
138
- }, {
139
- id: '43',
140
- type: 'posts',
141
- attributes: {
142
- title: 'Hello!!',
143
- body: 'Hello, world!!'
144
- },
145
- relationships: {
146
- comments: { data: [] },
147
- blog: { data: { type: 'blogs', id: '999' } },
148
- author: { data: nil }
149
- }
150
- }
151
- ]
152
- assert_equal expected, linked
153
- end
154
- end
155
- end
156
- end
157
- end