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
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
159
+
160
+
## Architecture
161
+
162
+
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,
163
+
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).
167
+
168
+
### ActiveModel::Serializer
169
+
170
+
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
171
+
and exposes an `attributes` method, among a few others.
172
+
It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
173
+
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
180
+
and, if there is no serializer, primitives.
181
+
182
+
### ActiveModelSerializers::Adapter::Base
183
+
184
+
The **`ActiveModelSerializeres::Adapter::Base`** describes the structure of the JSON document generated from a
185
+
serializer. For example, the `Attributes` example represents each serializer as its
186
+
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
187
+
API](http://jsonapi.org/) document.
188
+
189
+
### ActiveModelSerializers::SerializableResource
190
+
191
+
The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
192
+
to an object that responds to `to_json`, and `as_json`. It is used in the controller to
193
+
encapsulate the serialization resource when rendered. However, it can also be used on its own
194
+
to serialize a resource outside of a controller, as well.
195
+
196
+
### Primitive handling
197
+
198
+
Definitions: A primitive is usually a String or Array. There is no serializer
199
+
defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
200
+
`to_json`). (The below also applies for any object with no serializer.)
201
+
202
+
- ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
203
+
204
+
Internally, if no serializer can be found in the controller, the resource is not decorated by
205
+
ActiveModelSerializers.
206
+
207
+
- However, when a primitive value is an attribute or in a collection, it is not modified.
208
+
209
+
When serializing a collection and the collection serializer (CollectionSerializer) cannot
210
+
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).
211
+
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`).
236
+
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
253
+
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).
254
+
1.**ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
255
+
resource as defined by the serializer.
256
+
257
+
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
258
+
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
259
+
to know about, but not part of ActiveModelSerializers.)
260
+
261
+
### 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