active_model_serializers 0.10.9 → 0.10.10

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 (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -2
  3. data/lib/action_controller/serialization.rb +9 -1
  4. data/lib/active_model/serializer.rb +10 -2
  5. data/lib/active_model/serializer/version.rb +1 -1
  6. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  7. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  8. metadata +12 -259
  9. data/.github/ISSUE_TEMPLATE.md +0 -29
  10. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  11. data/.gitignore +0 -35
  12. data/.rubocop.yml +0 -109
  13. data/.simplecov +0 -110
  14. data/.travis.yml +0 -63
  15. data/CODE_OF_CONDUCT.md +0 -74
  16. data/CONTRIBUTING.md +0 -105
  17. data/Gemfile +0 -74
  18. data/Rakefile +0 -76
  19. data/active_model_serializers.gemspec +0 -64
  20. data/appveyor.yml +0 -28
  21. data/bin/bench +0 -171
  22. data/bin/bench_regression +0 -316
  23. data/bin/rubocop +0 -38
  24. data/bin/serve_benchmark +0 -39
  25. data/docs/README.md +0 -41
  26. data/docs/STYLE.md +0 -58
  27. data/docs/general/adapters.md +0 -269
  28. data/docs/general/caching.md +0 -58
  29. data/docs/general/configuration_options.md +0 -185
  30. data/docs/general/deserialization.md +0 -100
  31. data/docs/general/fields.md +0 -31
  32. data/docs/general/getting_started.md +0 -133
  33. data/docs/general/instrumentation.md +0 -40
  34. data/docs/general/key_transforms.md +0 -40
  35. data/docs/general/logging.md +0 -21
  36. data/docs/general/rendering.md +0 -293
  37. data/docs/general/serializers.md +0 -495
  38. data/docs/how-open-source-maintained.jpg +0 -0
  39. data/docs/howto/add_pagination_links.md +0 -138
  40. data/docs/howto/add_relationship_links.md +0 -140
  41. data/docs/howto/add_root_key.md +0 -62
  42. data/docs/howto/grape_integration.md +0 -42
  43. data/docs/howto/outside_controller_use.md +0 -66
  44. data/docs/howto/passing_arbitrary_options.md +0 -27
  45. data/docs/howto/serialize_poro.md +0 -73
  46. data/docs/howto/test.md +0 -154
  47. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  48. data/docs/integrations/ember-and-json-api.md +0 -147
  49. data/docs/integrations/grape.md +0 -19
  50. data/docs/jsonapi/errors.md +0 -56
  51. data/docs/jsonapi/schema.md +0 -151
  52. data/docs/jsonapi/schema/schema.json +0 -366
  53. data/docs/rfcs/0000-namespace.md +0 -106
  54. data/docs/rfcs/template.md +0 -15
  55. data/test/action_controller/adapter_selector_test.rb +0 -64
  56. data/test/action_controller/explicit_serializer_test.rb +0 -137
  57. data/test/action_controller/json/include_test.rb +0 -248
  58. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  59. data/test/action_controller/json_api/errors_test.rb +0 -42
  60. data/test/action_controller/json_api/fields_test.rb +0 -68
  61. data/test/action_controller/json_api/linked_test.rb +0 -204
  62. data/test/action_controller/json_api/pagination_test.rb +0 -126
  63. data/test/action_controller/json_api/transform_test.rb +0 -191
  64. data/test/action_controller/lookup_proc_test.rb +0 -51
  65. data/test/action_controller/namespace_lookup_test.rb +0 -234
  66. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  67. data/test/action_controller/serialization_test.rb +0 -480
  68. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  69. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  70. data/test/active_model_serializers/logging_test.rb +0 -79
  71. data/test/active_model_serializers/model_test.rb +0 -144
  72. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  73. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  74. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  75. data/test/active_model_serializers/test/schema_test.rb +0 -133
  76. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  77. data/test/active_record_test.rb +0 -11
  78. data/test/adapter/attributes_test.rb +0 -42
  79. data/test/adapter/deprecation_test.rb +0 -102
  80. data/test/adapter/json/belongs_to_test.rb +0 -47
  81. data/test/adapter/json/collection_test.rb +0 -106
  82. data/test/adapter/json/has_many_test.rb +0 -55
  83. data/test/adapter/json/transform_test.rb +0 -95
  84. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  85. data/test/adapter/json_api/collection_test.rb +0 -98
  86. data/test/adapter/json_api/errors_test.rb +0 -78
  87. data/test/adapter/json_api/fields_test.rb +0 -98
  88. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  89. data/test/adapter/json_api/has_many_test.rb +0 -175
  90. data/test/adapter/json_api/has_one_test.rb +0 -82
  91. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  92. data/test/adapter/json_api/json_api_test.rb +0 -35
  93. data/test/adapter/json_api/linked_test.rb +0 -415
  94. data/test/adapter/json_api/links_test.rb +0 -112
  95. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  96. data/test/adapter/json_api/parse_test.rb +0 -139
  97. data/test/adapter/json_api/relationship_test.rb +0 -399
  98. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  99. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  100. data/test/adapter/json_api/transform_test.rb +0 -514
  101. data/test/adapter/json_api/type_test.rb +0 -195
  102. data/test/adapter/json_test.rb +0 -48
  103. data/test/adapter/null_test.rb +0 -24
  104. data/test/adapter/polymorphic_test.rb +0 -220
  105. data/test/adapter_test.rb +0 -69
  106. data/test/array_serializer_test.rb +0 -24
  107. data/test/benchmark/app.rb +0 -67
  108. data/test/benchmark/benchmarking_support.rb +0 -69
  109. data/test/benchmark/bm_active_record.rb +0 -83
  110. data/test/benchmark/bm_adapter.rb +0 -40
  111. data/test/benchmark/bm_caching.rb +0 -121
  112. data/test/benchmark/bm_lookup_chain.rb +0 -85
  113. data/test/benchmark/bm_transform.rb +0 -47
  114. data/test/benchmark/config.ru +0 -3
  115. data/test/benchmark/controllers.rb +0 -85
  116. data/test/benchmark/fixtures.rb +0 -221
  117. data/test/cache_test.rb +0 -717
  118. data/test/collection_serializer_test.rb +0 -129
  119. data/test/fixtures/active_record.rb +0 -115
  120. data/test/fixtures/poro.rb +0 -227
  121. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  122. data/test/generators/serializer_generator_test.rb +0 -77
  123. data/test/grape_test.rb +0 -198
  124. data/test/lint_test.rb +0 -51
  125. data/test/logger_test.rb +0 -22
  126. data/test/poro_test.rb +0 -11
  127. data/test/serializable_resource_test.rb +0 -81
  128. data/test/serializers/association_macros_test.rb +0 -39
  129. data/test/serializers/associations_test.rb +0 -520
  130. data/test/serializers/attribute_test.rb +0 -155
  131. data/test/serializers/attributes_test.rb +0 -54
  132. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  133. data/test/serializers/configuration_test.rb +0 -34
  134. data/test/serializers/fieldset_test.rb +0 -16
  135. data/test/serializers/meta_test.rb +0 -204
  136. data/test/serializers/options_test.rb +0 -34
  137. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  138. data/test/serializers/reflection_test.rb +0 -481
  139. data/test/serializers/root_test.rb +0 -23
  140. data/test/serializers/serialization_test.rb +0 -57
  141. data/test/serializers/serializer_for_test.rb +0 -138
  142. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  143. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  144. data/test/support/isolated_unit.rb +0 -86
  145. data/test/support/rails5_shims.rb +0 -55
  146. data/test/support/rails_app.rb +0 -40
  147. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  148. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  149. data/test/support/schemas/custom/show.json +0 -7
  150. data/test/support/schemas/hyper_schema.json +0 -93
  151. data/test/support/schemas/render_using_json_api.json +0 -43
  152. data/test/support/schemas/simple_json_pointers.json +0 -10
  153. data/test/support/serialization_testing.rb +0 -81
  154. data/test/test_helper.rb +0 -72
@@ -1,106 +0,0 @@
1
- - Start Date: (2015-10-29)
2
- - RFC PR: https://github.com/rails-api/active_model_serializers/pull/1310
3
- - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/1298
4
-
5
- # Summary
6
-
7
- Provide a consistent API for the user of the AMS.
8
-
9
- # Motivation
10
-
11
- The actual public API is defined under `ActiveModelSerializers`,
12
- `ActiveModel::Serializer` and `ActiveModel`.
13
-
14
- At the `ActiveModel::Serializer` we have:
15
-
16
- - `ActiveModel::Serializer.config`
17
- - `ActiveModel::Serializer`
18
-
19
- At the `ActiveModelSerializers` we have:
20
-
21
- - `ActiveModelSerializers::Model`
22
- - `ActiveModelSerializers.logger`
23
-
24
- At `ActiveModel` we have:
25
-
26
- - `ActiveModel::SerializableResource`
27
-
28
- The idea here is to provide a single namespace `ActiveModelSerializers` to the user.
29
- Following the same idea we have on other gems like
30
- [Devise](https://github.com/plataformatec/devise/blob/e9c82472ffe7c43a448945f77e034a0e47dde0bb/lib/devise.rb),
31
- [Refile](https://github.com/refile/refile/blob/6b24c293d044862dafbf1bfa4606672a64903aa2/lib/refile.rb) and
32
- [Active Job](https://github.com/rails/rails/blob/30bacc26f8f258b39e12f63fe52389a968d9c1ea/activejob/lib/active_job.rb)
33
- for example.
34
-
35
- This way we are clarifing the boundaries of
36
- [ActiveModelSerializers and Rails](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md#prehistory)
37
- and make clear that the `ActiveModel::Serializer` class is no longer the primary
38
- behavior of the ActiveModelSerializers.
39
-
40
- # Detailed design
41
-
42
- ## New classes and modules organization
43
-
44
- Since this will be a big change we can do this on baby steps, read small pull requests. A
45
- possible approach is:
46
-
47
- - All new code will be in `lib/active_model_serializers/` using
48
- the module namespace `ActiveModelSerializers`.
49
- - Move all content under `ActiveModel::Serializer` to be under
50
- `ActiveModelSerializers`, the adapter is on this steps;
51
- - Move all content under `ActiveModel` to be under `ActiveModelSerializers`,
52
- the `SerializableResource` is on this step;
53
- - Change all public API that doesn't make sense, keeping in mind only to keep
54
- this in the same namespace
55
- - Update the README;
56
- - Update the docs;
57
-
58
- The following table represents the current and the desired classes and modules
59
- at the first moment.
60
-
61
- | Current | Desired | Notes |
62
- |--------------------------------------------------------|--------------------------------------------------|--------------------|
63
- | `ActiveModelSerializers` and `ActiveModel::Serializer` | `ActiveModelSerializers` | The main namespace |
64
- | `ActiveModelSerializers.logger` | `ActiveModelSerializers.logger` ||
65
- | `ActiveModelSerializers::Model` | `ActiveModelSerializers::Model` ||
66
- | `ActiveModel::SerializableResource` | `ActiveModelSerializers::SerializableResource` ||
67
- | `ActiveModel::Serializer` | `ActiveModelSerializers::Serializer` | The name can be discussed in a future pull request. For example, we can rename this to `Resource` [following this idea](https://github.com/rails-api/active_model_serializers/pull/1301/files#r42963185) more info about naming in the next section|
68
- | `ActiveModel::Serializer.config` | `ActiveModelSerializers.config` ||
69
-
70
- ## Renaming of class and modules
71
-
72
- When moving some content to the new namespace we can find some names that does
73
- not make much sense like `ActiveModel::Serializer::Adapter::JsonApi`.
74
- Discussion of renaming existing classes / modules and JsonApi objects will
75
- happen in separate pull requests, and issues, and in the google doc
76
- https://docs.google.com/document/d/1rcrJr0sVcazY2Opd_6Kmv1iIwuHbI84s1P_NzFn-05c/edit?usp=sharing
77
-
78
- Some of names already have a definition.
79
-
80
- - Adapters get their own namespace under ActiveModelSerializers. E.g
81
- `ActiveModelSerializers::Adapter`
82
- - Serializers get their own namespace under ActiveModelSerializers. E.g
83
- `ActiveModelSerializers::Serializer`
84
-
85
- ## Keeping compatibility
86
-
87
- All moved classes or modules be aliased to their old name and location with
88
- deprecation warnings, such as
89
- [was done for CollectionSerializer](https://github.com/rails-api/active_model_serializers/pull/1251).
90
-
91
- # Drawbacks
92
-
93
- This will be a breaking change, so all users serializers will be broken after a
94
- major bump.
95
- All pull requests will need to rebase since the architeture will change a lot.
96
-
97
- # Alternatives
98
-
99
- We can keep the way it is, and keep in mind to not add another namespace as a
100
- public API.
101
-
102
- # Unresolved questions
103
-
104
- What is the better class name to be used to the class that will be inherited at
105
- the creation of a serializer. This can be discussed in other RFC or directly via
106
- pull request.
@@ -1,15 +0,0 @@
1
- - Start Date: (YYYY-MM-DD)
2
- - RFC PR: https://github.com/rails-api/active_model_serializers/pull/dddd
3
- - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/dddd
4
-
5
- # Summary
6
-
7
- # Motivation
8
-
9
- # Detailed design
10
-
11
- # Drawbacks
12
-
13
- # Alternatives
14
-
15
- # Unresolved questions
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class AdapterSelectorTest < ActionController::TestCase
8
- class Profile < Model
9
- attributes :id, :name, :description
10
- associations :comments
11
- end
12
- class ProfileSerializer < ActiveModel::Serializer
13
- type 'profiles'
14
- attributes :name, :description
15
- end
16
-
17
- class AdapterSelectorTestController < ActionController::Base
18
- def render_using_default_adapter
19
- @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
20
- render json: @profile
21
- end
22
-
23
- def render_using_adapter_override
24
- @profile = Profile.new(id: 'render_using_adapter_override', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
25
- render json: @profile, adapter: :json_api
26
- end
27
-
28
- def render_skipping_adapter
29
- @profile = Profile.new(id: 'render_skipping_adapter_id', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
30
- render json: @profile, adapter: false
31
- end
32
- end
33
-
34
- tests AdapterSelectorTestController
35
-
36
- def test_render_using_default_adapter
37
- get :render_using_default_adapter
38
- assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
39
- end
40
-
41
- def test_render_using_adapter_override
42
- get :render_using_adapter_override
43
-
44
- expected = {
45
- data: {
46
- id: 'render_using_adapter_override',
47
- type: 'profiles',
48
- attributes: {
49
- name: 'Name 1',
50
- description: 'Description 1'
51
- }
52
- }
53
- }
54
-
55
- assert_equal expected.to_json, response.body
56
- end
57
-
58
- def test_render_skipping_adapter
59
- get :render_skipping_adapter
60
- assert_equal '{"id":"render_skipping_adapter_id","name":"Name 1","description":"Description 1"}', response.body
61
- end
62
- end
63
- end
64
- end
@@ -1,137 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class ExplicitSerializerTest < ActionController::TestCase
8
- class ExplicitSerializerTestController < ActionController::Base
9
- def render_using_explicit_serializer
10
- @profile = Profile.new(name: 'Name 1',
11
- description: 'Description 1',
12
- comments: 'Comments 1')
13
- render json: @profile, serializer: ProfilePreviewSerializer
14
- end
15
-
16
- def render_array_using_explicit_serializer
17
- array = [
18
- Profile.new(name: 'Name 1',
19
- description: 'Description 1',
20
- comments: 'Comments 1'),
21
- Profile.new(name: 'Name 2',
22
- description: 'Description 2',
23
- comments: 'Comments 2')
24
- ]
25
- render json: array,
26
- serializer: PaginatedSerializer,
27
- each_serializer: ProfilePreviewSerializer
28
- end
29
-
30
- def render_array_using_implicit_serializer
31
- array = [
32
- Profile.new(name: 'Name 1',
33
- description: 'Description 1',
34
- comments: 'Comments 1'),
35
- Profile.new(name: 'Name 2',
36
- description: 'Description 2',
37
- comments: 'Comments 2')
38
- ]
39
- render json: array,
40
- each_serializer: ProfilePreviewSerializer
41
- end
42
-
43
- def render_array_using_explicit_serializer_and_custom_serializers
44
- @post = Post.new(title: 'New Post', body: 'Body')
45
- @author = Author.new(name: 'Jane Blogger')
46
- @author.posts = [@post]
47
- @post.author = @author
48
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
49
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
50
- @post.comments = [@first_comment, @second_comment]
51
- @first_comment.post = @post
52
- @first_comment.author = nil
53
- @second_comment.post = @post
54
- @second_comment.author = nil
55
- @blog = Blog.new(id: 23, name: 'AMS Blog')
56
- @post.blog = @blog
57
-
58
- render json: [@post], each_serializer: PostPreviewSerializer
59
- end
60
-
61
- def render_using_explicit_each_serializer
62
- location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
63
- place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])
64
-
65
- render json: place, each_serializer: PlaceSerializer
66
- end
67
- end
68
-
69
- tests ExplicitSerializerTestController
70
-
71
- def test_render_using_explicit_serializer
72
- get :render_using_explicit_serializer
73
-
74
- assert_equal 'application/json', @response.content_type
75
- assert_equal '{"name":"Name 1"}', @response.body
76
- end
77
-
78
- def test_render_array_using_explicit_serializer
79
- get :render_array_using_explicit_serializer
80
- assert_equal 'application/json', @response.content_type
81
-
82
- expected = [
83
- { 'name' => 'Name 1' },
84
- { 'name' => 'Name 2' }
85
- ]
86
-
87
- assert_equal expected.to_json, @response.body
88
- end
89
-
90
- def test_render_array_using_implicit_serializer
91
- get :render_array_using_implicit_serializer
92
- assert_equal 'application/json', @response.content_type
93
-
94
- expected = [
95
- { 'name' => 'Name 1' },
96
- { 'name' => 'Name 2' }
97
- ]
98
- assert_equal expected.to_json, @response.body
99
- end
100
-
101
- def test_render_array_using_explicit_serializer_and_custom_serializers
102
- get :render_array_using_explicit_serializer_and_custom_serializers
103
-
104
- expected = [
105
- {
106
- 'title' => 'New Post',
107
- 'body' => 'Body',
108
- 'id' => @controller.instance_variable_get(:@post).id,
109
- 'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
110
- 'author' => { 'id' => @controller.instance_variable_get(:@author).id }
111
- }
112
- ]
113
-
114
- assert_equal expected.to_json, @response.body
115
- end
116
-
117
- def test_render_using_explicit_each_serializer
118
- get :render_using_explicit_each_serializer
119
-
120
- expected = {
121
- id: 1337,
122
- name: 'Amazing Place',
123
- locations: [
124
- {
125
- id: 42,
126
- lat: '-23.550520',
127
- lng: '-46.633309',
128
- address: 'Nowhere' # is a virtual attribute on LocationSerializer
129
- }
130
- ]
131
- }
132
-
133
- assert_equal expected.to_json, response.body
134
- end
135
- end
136
- end
137
- end
@@ -1,248 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class Json
8
- class IncludeTest < ActionController::TestCase
9
- INCLUDE_STRING = 'posts.comments'.freeze
10
- INCLUDE_HASH = { posts: :comments }.freeze
11
- DEEP_INCLUDE = 'posts.comments.author'.freeze
12
-
13
- class IncludeTestController < ActionController::Base
14
- def setup_data
15
- ActionController::Base.cache_store.clear
16
-
17
- @author = Author.new(id: 1, name: 'Steve K.')
18
-
19
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
20
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
21
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
22
-
23
- @post.comments = [@first_comment, @second_comment]
24
- @post.author = @author
25
-
26
- @first_comment.post = @post
27
- @second_comment.post = @post
28
-
29
- @blog = Blog.new(id: 1, name: 'My Blog!!')
30
- @post.blog = @blog
31
- @author.posts = [@post]
32
-
33
- @first_comment.author = @author
34
- @second_comment.author = @author
35
- @author.comments = [@first_comment, @second_comment]
36
- @author.roles = []
37
- @author.bio = {}
38
- end
39
-
40
- def render_without_include
41
- setup_data
42
- render json: @author, adapter: :json
43
- end
44
-
45
- def render_resource_with_include_hash
46
- setup_data
47
- render json: @author, include: INCLUDE_HASH, adapter: :json
48
- end
49
-
50
- def render_resource_with_include_string
51
- setup_data
52
- render json: @author, include: INCLUDE_STRING, adapter: :json
53
- end
54
-
55
- def render_resource_with_deep_include
56
- setup_data
57
- render json: @author, include: DEEP_INCLUDE, adapter: :json
58
- end
59
-
60
- def render_without_recursive_relationships
61
- # testing recursive includes ('**') can't have any cycles in the
62
- # relationships, or we enter an infinite loop.
63
- author = Author.new(id: 11, name: 'Jane Doe')
64
- post = Post.new(id: 12, title: 'Hello World', body: 'My first post')
65
- comment = Comment.new(id: 13, body: 'Commentary')
66
- author.posts = [post]
67
- post.comments = [comment]
68
- render json: author
69
- end
70
- end
71
-
72
- tests IncludeTestController
73
-
74
- def test_render_without_include
75
- get :render_without_include
76
- response = JSON.parse(@response.body)
77
- expected = {
78
- 'author' => {
79
- 'id' => 1,
80
- 'name' => 'Steve K.',
81
- 'posts' => [
82
- {
83
- 'id' => 42, 'title' => 'New Post', 'body' => 'Body'
84
- }
85
- ],
86
- 'roles' => [],
87
- 'bio' => {}
88
- }
89
- }
90
-
91
- assert_equal(expected, response)
92
- end
93
-
94
- def test_render_resource_with_include_hash
95
- get :render_resource_with_include_hash
96
- response = JSON.parse(@response.body)
97
-
98
- assert_equal(expected_include_response, response)
99
- end
100
-
101
- def test_render_resource_with_include_string
102
- get :render_resource_with_include_string
103
-
104
- response = JSON.parse(@response.body)
105
-
106
- assert_equal(expected_include_response, response)
107
- end
108
-
109
- def test_render_resource_with_deep_include
110
- get :render_resource_with_deep_include
111
-
112
- response = JSON.parse(@response.body)
113
-
114
- assert_equal(expected_deep_include_response, response)
115
- end
116
-
117
- def test_render_with_empty_default_includes
118
- with_default_includes '' do
119
- get :render_without_include
120
- response = JSON.parse(@response.body)
121
- expected = {
122
- 'author' => {
123
- 'id' => 1,
124
- 'name' => 'Steve K.'
125
- }
126
- }
127
- assert_equal(expected, response)
128
- end
129
- end
130
-
131
- def test_render_with_recursive_default_includes
132
- with_default_includes '**' do
133
- get :render_without_recursive_relationships
134
- response = JSON.parse(@response.body)
135
-
136
- expected = {
137
- 'id' => 11,
138
- 'name' => 'Jane Doe',
139
- 'roles' => nil,
140
- 'bio' => nil,
141
- 'posts' => [
142
- {
143
- 'id' => 12,
144
- 'title' => 'Hello World',
145
- 'body' => 'My first post',
146
- 'comments' => [
147
- {
148
- 'id' => 13,
149
- 'body' => 'Commentary',
150
- 'post' => nil, # not set to avoid infinite recursion
151
- 'author' => nil, # not set to avoid infinite recursion
152
- }
153
- ],
154
- 'blog' => {
155
- 'id' => 999,
156
- 'name' => 'Custom blog',
157
- 'writer' => nil,
158
- 'articles' => nil
159
- },
160
- 'author' => nil # not set to avoid infinite recursion
161
- }
162
- ]
163
- }
164
- assert_equal(expected, response)
165
- end
166
- end
167
-
168
- def test_render_with_includes_overrides_default_includes
169
- with_default_includes '' do
170
- get :render_resource_with_include_hash
171
- response = JSON.parse(@response.body)
172
-
173
- assert_equal(expected_include_response, response)
174
- end
175
- end
176
-
177
- private
178
-
179
- def expected_include_response
180
- {
181
- 'author' => {
182
- 'id' => 1,
183
- 'name' => 'Steve K.',
184
- 'posts' => [
185
- {
186
- 'id' => 42, 'title' => 'New Post', 'body' => 'Body',
187
- 'comments' => [
188
- {
189
- 'id' => 1, 'body' => 'ZOMG A COMMENT'
190
- },
191
- {
192
- 'id' => 2, 'body' => 'ZOMG ANOTHER COMMENT'
193
- }
194
- ]
195
- }
196
- ]
197
- }
198
- }
199
- end
200
-
201
- def expected_deep_include_response
202
- {
203
- 'author' => {
204
- 'id' => 1,
205
- 'name' => 'Steve K.',
206
- 'posts' => [
207
- {
208
- 'id' => 42, 'title' => 'New Post', 'body' => 'Body',
209
- 'comments' => [
210
- {
211
- 'id' => 1, 'body' => 'ZOMG A COMMENT',
212
- 'author' => {
213
- 'id' => 1,
214
- 'name' => 'Steve K.'
215
- }
216
- },
217
- {
218
- 'id' => 2, 'body' => 'ZOMG ANOTHER COMMENT',
219
- 'author' => {
220
- 'id' => 1,
221
- 'name' => 'Steve K.'
222
- }
223
- }
224
- ]
225
- }
226
- ]
227
- }
228
- }
229
- end
230
-
231
- def with_default_includes(include_directive)
232
- original = ActiveModelSerializers.config.default_includes
233
- ActiveModelSerializers.config.default_includes = include_directive
234
- clear_include_directive_cache
235
- yield
236
- ensure
237
- ActiveModelSerializers.config.default_includes = original
238
- clear_include_directive_cache
239
- end
240
-
241
- def clear_include_directive_cache
242
- ActiveModelSerializers
243
- .instance_variable_set(:@default_include_directive, nil)
244
- end
245
- end
246
- end
247
- end
248
- end