active_model_serializers 0.10.0 → 0.10.1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -2
  3. data/Gemfile +1 -1
  4. data/README.md +21 -24
  5. data/active_model_serializers.gemspec +4 -8
  6. data/docs/general/adapters.md +4 -2
  7. data/docs/general/configuration_options.md +6 -1
  8. data/docs/general/deserialization.md +1 -1
  9. data/docs/general/serializers.md +30 -3
  10. data/docs/jsonapi/schema.md +1 -1
  11. data/lib/active_model/serializer.rb +54 -11
  12. data/lib/active_model/serializer/adapter/base.rb +2 -0
  13. data/lib/active_model/serializer/associations.rb +4 -5
  14. data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
  15. data/lib/active_model/serializer/caching.rb +62 -110
  16. data/lib/active_model/serializer/collection_serializer.rb +30 -10
  17. data/lib/active_model/serializer/configuration.rb +1 -0
  18. data/lib/active_model/serializer/has_many_reflection.rb +0 -3
  19. data/lib/active_model/serializer/has_one_reflection.rb +0 -3
  20. data/lib/active_model/serializer/reflection.rb +3 -3
  21. data/lib/active_model/serializer/version.rb +1 -1
  22. data/lib/active_model_serializers.rb +6 -0
  23. data/lib/active_model_serializers/adapter.rb +6 -0
  24. data/lib/active_model_serializers/adapter/attributes.rb +2 -67
  25. data/lib/active_model_serializers/adapter/base.rb +38 -38
  26. data/lib/active_model_serializers/adapter/json_api.rb +36 -28
  27. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  28. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  29. data/lib/active_model_serializers/deprecate.rb +1 -2
  30. data/lib/active_model_serializers/deserialization.rb +2 -0
  31. data/lib/active_model_serializers/model.rb +2 -0
  32. data/lib/active_model_serializers/railtie.rb +2 -0
  33. data/lib/active_model_serializers/register_jsonapi_renderer.rb +34 -23
  34. data/lib/active_model_serializers/serialization_context.rb +10 -3
  35. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  36. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  37. data/test/action_controller/adapter_selector_test.rb +1 -1
  38. data/test/action_controller/explicit_serializer_test.rb +5 -4
  39. data/test/action_controller/json/include_test.rb +106 -27
  40. data/test/action_controller/json_api/errors_test.rb +2 -2
  41. data/test/action_controller/json_api/linked_test.rb +26 -21
  42. data/test/action_controller/serialization_test.rb +9 -6
  43. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
  44. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  45. data/test/adapter/json/collection_test.rb +14 -0
  46. data/test/adapter/json_api/collection_test.rb +4 -3
  47. data/test/adapter/json_api/errors_test.rb +13 -15
  48. data/test/adapter/json_api/linked_test.rb +8 -5
  49. data/test/adapter/json_api/links_test.rb +3 -1
  50. data/test/adapter/json_api/pagination_links_test.rb +13 -1
  51. data/test/adapter/json_api/relationships_test.rb +9 -4
  52. data/test/adapter/json_api/resource_identifier_test.rb +7 -2
  53. data/test/adapter/json_api/transform_test.rb +76 -75
  54. data/test/adapter/json_test.rb +4 -3
  55. data/test/benchmark/app.rb +1 -1
  56. data/test/benchmark/bm_caching.rb +14 -14
  57. data/test/benchmark/bm_transform.rb +16 -5
  58. data/test/benchmark/controllers.rb +16 -17
  59. data/test/benchmark/fixtures.rb +72 -72
  60. data/test/cache_test.rb +73 -45
  61. data/test/fixtures/poro.rb +6 -5
  62. data/test/grape_test.rb +96 -2
  63. data/test/serializable_resource_test.rb +12 -12
  64. data/test/serializers/meta_test.rb +12 -6
  65. data/test/support/isolated_unit.rb +1 -0
  66. data/test/support/rails5_shims.rb +8 -2
  67. data/test/support/rails_app.rb +0 -9
  68. metadata +53 -23
  69. data/lib/active_model/serializer/include_tree.rb +0 -111
  70. data/test/include_tree/from_include_args_test.rb +0 -26
  71. data/test/include_tree/from_string_test.rb +0 -94
  72. data/test/include_tree/include_args_to_hash_test.rb +0 -64
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb97c8740c2530c085ee9186ea27ddd220367fc3
4
- data.tar.gz: 0a768d3ab122abe2c3e7f5b6c00a8dada6872433
3
+ metadata.gz: 01b7ca6e2acce14a076a2a63c87dc6d4677989dc
4
+ data.tar.gz: e04babe194acff0c4a0fd2b1daec6ee5128d8b8d
5
5
  SHA512:
6
- metadata.gz: 5589022dd6fa160c0a03981cedf46433fb1eb19e4bac5f3489908671e9e0969c48bb7a86cf03d12cf8ce825b2efe3ed6ad1b6594019635470319c899e7964617
7
- data.tar.gz: 989cf413780f12d47a553a7aae5cba33de1625bbc022366b7e4533810b87b151302ed6000116c03234bf2b2048a913048624728a54be87cead848076924cf6ce
6
+ metadata.gz: 5b4a986b9ab49ec7962be0ff855c9c3d2504e55743b8ba5cbbb22fdb936704456d5033160ab52d5efc2537a248f18de636aaafc7d5ec9c8a29a5e027f1c39063
7
+ data.tar.gz: d824f39238f997d1fd763f8cb8d6ce158209a7d4c3ab5de3ea5294d991e5f2a6a1dca6a2be090bc69195ed58506ae54e5934f61440095ab2a749a948397a6937
@@ -1,8 +1,36 @@
1
1
  ## 0.10.x
2
2
 
3
- ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...master)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...master)
4
4
 
5
- ### v0.10.0 (2016-05-17)
5
+ Breaking changes:
6
+
7
+ Features:
8
+
9
+ Fixes:
10
+
11
+ Misc:
12
+
13
+ ### [v0.10.1 (2016-06-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...v0.10.1)
14
+
15
+ Features:
16
+ - [#1668](https://github.com/rails-api/active_model_serializers/pull/1668) Exclude nil and empty links. (@sigmike)
17
+ - [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact)
18
+
19
+ Fixes:
20
+ - [#1754](https://github.com/rails-api/active_model_serializers/pull/1754) Fixes #1759, Grape integration, improves serialization_context
21
+ missing error message on pagination. Document overriding CollectionSerializer#paginated?. (@bf4)
22
+ Moved serialization_context creation to Grape formatter, so resource serialization works without explicit calls to the `render` helper method.
23
+ Added Grape collection tests. (@onomated)
24
+ - [#1287](https://github.com/rails-api/active_model_serializers/pull/1287) Pass `fields` options from adapter to serializer. (@vasilakisfil)
25
+ - [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
26
+ is set to `false`. (@groyoh)
27
+ - [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear)
28
+
29
+ Misc:
30
+ - [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
31
+ - [#1685](https://github.com/rails-api/active_model_serializers/pull/1685) Replace `IncludeTree` with `IncludeDirective` from the jsonapi gem.
32
+
33
+ ### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0)
6
34
 
7
35
  Breaking changes:
8
36
  - [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
@@ -22,6 +50,7 @@ Fixes:
22
50
 
23
51
  Misc:
24
52
  - [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre)
53
+ - [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever)
25
54
 
26
55
  ### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5)
27
56
 
data/Gemfile CHANGED
@@ -48,6 +48,6 @@ group :test do
48
48
  end
49
49
 
50
50
  group :development, :test do
51
- gem 'rubocop', '~> 0.39.0', require: false
51
+ gem 'rubocop', '~> 0.40.0', require: false
52
52
  gem 'yard', require: false
53
53
  end
data/README.md CHANGED
@@ -24,17 +24,6 @@
24
24
  </tr>
25
25
  </table>
26
26
 
27
-
28
- ## Documentation
29
-
30
- - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
31
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
32
- - [Guides](docs)
33
- - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
34
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
35
- - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
36
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
37
-
38
27
  ## About
39
28
 
40
29
  ActiveModelSerializers brings convention over configuration to your JSON generation.
@@ -50,33 +39,24 @@ resource serialization. The serialization has the `#as_json`, `#to_json` and `#s
50
39
  methods used by the Rails JSON Renderer. (SerializableResource actually delegates
51
40
  these methods to the adapter.)
52
41
 
53
- By default ActiveModelSerializers will use the **Attributes Adapter**.
42
+ By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
54
43
  But we strongly advise you to use **JsonApi Adapter**, which
55
44
  follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
56
45
  Check how to change the adapter in the sections below.
57
46
 
58
- ## RELEASE CANDIDATE, PLEASE READ
59
-
60
- This is the **master** branch of ActiveModelSerializers.
61
-
62
- It will become the `0.10.0` release when it's ready. Currently this is a release candidate.
63
-
64
47
  `0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
65
48
 
66
- `0.10.x` will be based on the `0.8.0` code, but with a more flexible
49
+ `0.10.x` is based on the `0.8.0` code, but with a more flexible
67
50
  architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
68
51
 
69
52
  It is generally safe and recommended to use the master branch.
70
53
 
71
- For more information, see the post '[The future of
72
- AMS](https://medium.com/@joaomdmoura/the-future-of-ams-e5f9047ca7e9)'.
73
-
74
54
  ## Installation
75
55
 
76
56
  Add this line to your application's Gemfile:
77
57
 
78
58
  ```
79
- gem 'active_model_serializers'
59
+ gem 'active_model_serializers', '~> 0.10.0'
80
60
  ```
81
61
 
82
62
  And then execute:
@@ -103,8 +83,25 @@ If you'd like to chat, we have a [community slack](http://amserializers.herokuap
103
83
 
104
84
  Thanks!
105
85
 
86
+ ## Documentation
87
+
88
+ - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
89
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
90
+ - [Guides](docs)
91
+ - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
92
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
93
+ - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
94
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
95
+
96
+
106
97
  ## High-level behavior
107
98
 
99
+ Choose an adapter from [adapters](lib/active_model_serializers/adapter):
100
+
101
+ ``` ruby
102
+ ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
103
+ ```
104
+
108
105
  Given a [serializable model](lib/active_model/serializer/lint.rb):
109
106
 
110
107
  ```ruby
@@ -162,6 +159,6 @@ serializer.associations
162
159
  ```
163
160
  See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
164
161
 
165
- # Contributing
162
+ ## Contributing
166
163
 
167
164
  See [CONTRIBUTING.md](CONTRIBUTING.md)
@@ -19,9 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
  spec.executables = []
21
21
 
22
- spec.required_ruby_version = '>= 2.0.0'
22
+ spec.required_ruby_version = '>= 2.1'
23
23
 
24
- rails_versions = '>= 4.0'
24
+ rails_versions = ['>= 4.1', '< 6']
25
25
  spec.add_runtime_dependency 'activemodel', rails_versions
26
26
  # 'activesupport', rails_versions
27
27
  # 'builder'
@@ -42,6 +42,8 @@ Gem::Specification.new do |spec|
42
42
  # 'minitest'
43
43
  # 'thread_safe'
44
44
 
45
+ spec.add_runtime_dependency 'jsonapi', '~> 0.1.1.beta2'
46
+
45
47
  spec.add_development_dependency 'activerecord', rails_versions
46
48
  # arel
47
49
  # activesupport
@@ -57,10 +59,4 @@ Gem::Specification.new do |spec|
57
59
  spec.add_development_dependency 'grape', ['>= 0.13', '< 1.0']
58
60
  spec.add_development_dependency 'json_schema'
59
61
  spec.add_development_dependency 'rake', ['>= 10.0', '< 12.0']
60
-
61
- spec.post_install_message = <<-EOF
62
- NOTE: The default key case for the JsonApi adapter has changed to dashed.
63
- See https://github.com/rails-api/active_model_serializers/blob/master/docs/general/key_transforms.md
64
- for more information on configuring this behavior.
65
- EOF
66
62
  end
@@ -67,9 +67,11 @@ Doesn't follow any specific convention.
67
67
 
68
68
  ### JSON
69
69
 
70
- The response document always with a root key.
70
+ The json response is always rendered with a root key.
71
71
 
72
- The root key **can't be overridden**, and will be derived from the resource being serialized.
72
+ The root key can be overridden by:
73
+ * passing the `root` option in the render call. See details in the [Rendering Guides](rendering.md#overriding-the-root-key).
74
+ * setting the `type` of the serializer. See details in the [Serializers Guide](serializers.md#type).
73
75
 
74
76
  Doesn't follow any specific convention.
75
77
 
@@ -52,9 +52,14 @@ Each adapter has a default key transform configured:
52
52
  `config.key_transform` is a global override of the adapter default. Adapters
53
53
  still prefer the render option `:key_transform` over this setting.
54
54
 
55
+ *NOTE: Key transforms can be expensive operations. If key transforms are unnecessary for the
56
+ application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*
55
57
 
56
- ## JSON API
58
+ ##### default_includes
59
+ What relationships to serialize by default. Default: `'*'`, which includes one level of related
60
+ objects. See [includes](adapters.md#included) for more info.
57
61
 
62
+ ## JSON API
58
63
 
59
64
  ##### jsonapi_resource_type
60
65
 
@@ -42,7 +42,7 @@ document = {
42
42
  'title' => 'Title 1',
43
43
  'date' => '2015-12-20'
44
44
  },
45
- 'associations' => {
45
+ 'relationships' => {
46
46
  'author' => {
47
47
  'data' => {
48
48
  'type' => 'user',
@@ -31,10 +31,21 @@ Serialization of the resource `title`
31
31
  |---------------------------- |-------------|
32
32
  | `attribute :title` | `{ title: 'Some Title' } `
33
33
  | `attribute :title, key: :name` | `{ name: 'Some Title' } `
34
- | `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } `
34
+ | `attribute(:title) { 'A Different Title'}` | `{ title: 'A Different Title' } `
35
35
  | `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
36
36
 
37
- [PR please for conditional attributes:)](https://github.com/rails-api/active_model_serializers/pull/1403)
37
+ An `if` or `unless` option can make an attribute conditional. It takes a symbol of a method name on the serializer, or a lambda literal.
38
+
39
+ e.g.
40
+
41
+ ```ruby
42
+ attribute :private_data, if: :is_current_user?
43
+ attribute :another_private_data, if: -> { scope.admin? }
44
+
45
+ def is_current_user?
46
+ object.id == current_user.id
47
+ end
48
+ ```
38
49
 
39
50
  ### Associations
40
51
 
@@ -255,7 +266,7 @@ In the controller, the scope/scope_name options are equal to
255
266
  the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
256
267
  which is `:current_user`, by default.
257
268
 
258
- Specfically, the `scope_name` is defaulted to `:current_user`, and may be set as
269
+ Specifically, the `scope_name` is defaulted to `:current_user`, and may be set as
259
270
  `serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
260
271
  present and the controller responds to `scope_name`.
261
272
 
@@ -370,3 +381,19 @@ class PostSerializer < ActiveModel::Serializer
370
381
  end
371
382
  end
372
383
  ```
384
+
385
+ ## Overriding association serializer lookup
386
+
387
+ If you want to define a specific serializer lookup for your associations, you can override
388
+ the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
389
+
390
+ ```ruby
391
+ class MySerializer < ActiveModel::Serializer
392
+ def self.serializer_for(model, options)
393
+ return SparseAdminSerializer if model.class == 'Admin'
394
+ super
395
+ end
396
+
397
+ # the rest of the serializer
398
+ end
399
+ ```
@@ -28,7 +28,7 @@ Example supported requests
28
28
  - Relationships
29
29
  - GET /articles/1/relationships/comments
30
30
  - GET /articles/1/relationships/author
31
- - Optional: [Inclusion of related resources](http://jsonapi.org/format/#fetching-includes) `ActiveModel::Serializer::IncludeTree`
31
+ - Optional: [Inclusion of related resources](http://jsonapi.org/format/#fetching-includes) `JSONAPI::IncludeDirective`
32
32
  - GET /articles/1?`include`=comments
33
33
  - GET /articles/1?`include`=comments.author
34
34
  - GET /articles/1?`include`=author,comments.author
@@ -1,9 +1,9 @@
1
1
  require 'thread_safe'
2
+ require 'jsonapi/include_directive'
2
3
  require 'active_model/serializer/collection_serializer'
3
4
  require 'active_model/serializer/array_serializer'
4
5
  require 'active_model/serializer/error_serializer'
5
6
  require 'active_model/serializer/errors_serializer'
6
- require 'active_model/serializer/include_tree'
7
7
  require 'active_model/serializer/associations'
8
8
  require 'active_model/serializer/attributes'
9
9
  require 'active_model/serializer/caching'
@@ -98,6 +98,22 @@ module ActiveModel
98
98
  end
99
99
  end
100
100
 
101
+ # @api private
102
+ def self.include_directive_from_options(options)
103
+ if options[:include_directive]
104
+ options[:include_directive]
105
+ elsif options[:include]
106
+ JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
107
+ else
108
+ ActiveModelSerializers.default_include_directive
109
+ end
110
+ end
111
+
112
+ # @api private
113
+ def self.serialization_adapter_instance
114
+ @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
115
+ end
116
+
101
117
  attr_accessor :object, :root, :scope
102
118
 
103
119
  # `scope_name` is set as :current_user by default in the controller.
@@ -123,9 +139,7 @@ module ActiveModel
123
139
  # associations, similar to how ActiveModel::Serializers::JSON is used
124
140
  # in ActiveRecord::Base.
125
141
  #
126
- # TODO: Move to here the Attributes adapter logic for
127
- # +serializable_hash_for_single_resource(options)+
128
- # and include <tt>ActiveModel::Serializers::JSON</tt>.
142
+ # TODO: Include <tt>ActiveModel::Serializers::JSON</tt>.
129
143
  # So that the below is true:
130
144
  # @param options [nil, Hash] The same valid options passed to `serializable_hash`
131
145
  # (:only, :except, :methods, and :include).
@@ -149,11 +163,13 @@ module ActiveModel
149
163
  # serializer.as_json(include: :posts)
150
164
  # # Second level and higher order associations work as well:
151
165
  # serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
152
- def serializable_hash(adapter_opts = nil)
153
- adapter_opts ||= {}
154
- adapter_opts = { include: '*', adapter: :attributes }.merge!(adapter_opts)
155
- adapter = ActiveModelSerializers::Adapter.create(self, adapter_opts)
156
- adapter.serializable_hash(adapter_opts)
166
+ def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
167
+ adapter_options ||= {}
168
+ options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
169
+ cached_attributes = adapter_options[:cached_attributes] ||= {}
170
+ resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
171
+ relationships = resource_relationships(adapter_options, options, adapter_instance)
172
+ resource.merge(relationships)
157
173
  end
158
174
  alias to_hash serializable_hash
159
175
  alias to_h serializable_hash
@@ -178,13 +194,40 @@ module ActiveModel
178
194
  def read_attribute_for_serialization(attr)
179
195
  if respond_to?(attr)
180
196
  send(attr)
181
- elsif self.class._fragmented
182
- self.class._fragmented.read_attribute_for_serialization(attr)
183
197
  else
184
198
  object.read_attribute_for_serialization(attr)
185
199
  end
186
200
  end
187
201
 
202
+ # @api private
203
+ def resource_relationships(adapter_options, options, adapter_instance)
204
+ relationships = {}
205
+ include_directive = options.fetch(:include_directive)
206
+ associations(include_directive).each do |association|
207
+ adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
208
+ relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance)
209
+ end
210
+
211
+ relationships
212
+ end
213
+
214
+ # @api private
215
+ def relationship_value_for(association, adapter_options, adapter_instance)
216
+ return association.options[:virtual_value] if association.options[:virtual_value]
217
+ association_serializer = association.serializer
218
+ association_object = association_serializer && association_serializer.object
219
+ return unless association_object
220
+
221
+ relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
222
+
223
+ if association.options[:polymorphic] && relationship_value
224
+ polymorphic_type = association_object.class.name.underscore
225
+ relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
226
+ end
227
+
228
+ relationship_value
229
+ end
230
+
188
231
  protected
189
232
 
190
233
  attr_accessor :instance_options
@@ -7,9 +7,11 @@ module ActiveModel
7
7
  deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
8
8
  end
9
9
 
10
+ # :nocov:
10
11
  def initialize(serializer, options = {})
11
12
  super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
12
13
  end
14
+ # :nocov:
13
15
  end
14
16
  end
15
17
  end
@@ -10,8 +10,6 @@ module ActiveModel
10
10
  module Associations
11
11
  extend ActiveSupport::Concern
12
12
 
13
- DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
14
-
15
13
  included do
16
14
  with_options instance_writer: false, instance_reader: true do |serializer|
17
15
  serializer.class_attribute :_reflections
@@ -80,17 +78,18 @@ module ActiveModel
80
78
  end
81
79
  end
82
80
 
83
- # @param [IncludeTree] include_tree (defaults to all associations when not provided)
81
+ # @param [JSONAPI::IncludeDirective] include_directive (defaults to the
82
+ # +default_include_directive+ config value when not provided)
84
83
  # @return [Enumerator<Association>]
85
84
  #
86
- def associations(include_tree = DEFAULT_INCLUDE_TREE)
85
+ def associations(include_directive = ActiveModelSerializers.default_include_directive)
87
86
  return unless object
88
87
 
89
88
  Enumerator.new do |y|
90
89
  self.class._reflections.each do |reflection|
91
90
  next if reflection.excluded?(self)
92
91
  key = reflection.options.fetch(:key, reflection.name)
93
- next unless include_tree.key?(key)
92
+ next unless include_directive.key?(key)
94
93
  y.yield reflection.build_association(self, instance_options)
95
94
  end
96
95
  end
@@ -2,9 +2,6 @@ module ActiveModel
2
2
  class Serializer
3
3
  # @api private
4
4
  class BelongsToReflection < SingularReflection
5
- def macro
6
- :belongs_to
7
- end
8
5
  end
9
6
  end
10
7
  end