You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ Fixes:
10
10
11
11
Misc:
12
12
13
+
-[#1993](https://github.com/rails-api/active_model_serializers/pull/1993) Swap out KeyTransform for CaseTransform gem for the possibility of native extension use (@NullVoxPopuli)
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
165
+
166
+
## Architecture
167
+
168
+
This section focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
169
+
please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
173
+
174
+
### ActiveModel::Serializer
175
+
176
+
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
177
+
and exposes an `attributes` method, among a few others.
178
+
It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
179
+
It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
186
+
and, if there is no serializer, primitives.
187
+
188
+
### ActiveModelSerializers::Adapter::Base
189
+
190
+
The **`ActiveModelSerializeres::Adapter::Base`** describes the structure of the JSON document generated from a
191
+
serializer. For example, the `Attributes` example represents each serializer as its
192
+
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
193
+
API](http://jsonapi.org/) document.
194
+
195
+
### ActiveModelSerializers::SerializableResource
196
+
197
+
The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
198
+
to an object that responds to `to_json`, and `as_json`. It is used in the controller to
199
+
encapsulate the serialization resource when rendered. However, it can also be used on its own
200
+
to serialize a resource outside of a controller, as well.
201
+
202
+
### Primitive handling
203
+
204
+
Definitions: A primitive is usually a String or Array. There is no serializer
205
+
defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
206
+
`to_json`). (The below also applies for any object with no serializer.)
207
+
208
+
- ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
209
+
210
+
Internally, if no serializer can be found in the controller, the resource is not decorated by
211
+
ActiveModelSerializers.
212
+
213
+
- However, when a primitive value is an attribute or in a collection, it is not modified.
214
+
215
+
When serializing a collection and the collection serializer (CollectionSerializer) cannot
216
+
identify a serializer for a resource in its collection, it throws [`:no_serializer`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128).
217
+
For example, when caught by `Reflection#build_association`, and the association value is set directly:
1.`options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
242
+
The `adapter_opts` keys are defined in [`ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
259
+
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/a54d237e2828fe6bab1ea5dfe6360d4ecc8214cd/lib/active_model/serializer/array_serializer.rb#L14-L16).
260
+
1.**ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
261
+
resource as defined by the serializer.
262
+
263
+
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
264
+
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
265
+
to know about, but not part of ActiveModelSerializers.)
266
+
267
+
### What does a 'serializable resource' look like?
See the ActiveModelSerializers::Model for a base class that implements the full
55
-
API for a plain-old Ruby object (PORO).
51
+
See [README](../../README.md#what-does-a-serializable-resource-look-like)
56
52
57
53
## SerializableResource options
58
54
59
-
The `options` hash passed to `render` or `ActiveModelSerializers::SerializableResource.new(resource, options)`
60
-
are partitioned into `serializer_opts` and `adapter_opts`. `adapter_opts` are passed to new Adapters;
61
-
`serializer_opts` are passed to new Serializers.
62
-
63
-
The `adapter_opts` are specified in [ActiveModelSerializers::SerializableResource::ADAPTER_OPTIONS](../../lib/active_model_serializers/serializable_resource.rb#L5).
64
-
The `serializer_opts` are the remaining options.
65
-
66
-
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
67
-
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
68
-
to know about, but not part of ActiveModelSerializers.)
69
-
70
-
See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
55
+
See [README](../../README.md#activemodelserializersserializableresource)
0 commit comments