tiny-max-kit 0.0.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.
- checksums.yaml +7 -0
- data/active_model_serializers-0.10.16/CHANGELOG.md +793 -0
- data/active_model_serializers-0.10.16/MIT-LICENSE +22 -0
- data/active_model_serializers-0.10.16/README.md +304 -0
- data/active_model_serializers-0.10.16/lib/action_controller/serialization.rb +76 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializable_resource.rb +13 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter/base.rb +20 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter/json.rb +17 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter/null.rb +17 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/adapter.rb +26 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/array_serializer.rb +14 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/association.rb +73 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/attribute.rb +27 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/collection_serializer.rb +99 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/concerns/caching.rb +305 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/error_serializer.rb +16 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/field.rb +92 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/fieldset.rb +33 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/has_one_reflection.rb +9 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/lazy_association.rb +99 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/link.rb +23 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/lint.rb +152 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/null.rb +19 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/reflection.rb +212 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer/version.rb +7 -0
- data/active_model_serializers-0.10.16/lib/active_model/serializer.rb +430 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/attributes.rb +36 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/base.rb +85 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json.rb +23 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/pagination_links.rb +94 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter/null.rb +11 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/adapter.rb +100 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/callbacks.rb +57 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/deprecate.rb +56 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/deserialization.rb +17 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/json_pointer.rb +16 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/logging.rb +124 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/model.rb +132 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/railtie.rb +62 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/serialization_context.rb +41 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/test/schema.rb +140 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/test/serializer.rb +127 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers/test.rb +9 -0
- data/active_model_serializers-0.10.16/lib/active_model_serializers.rb +63 -0
- data/active_model_serializers-0.10.16/lib/generators/rails/USAGE +6 -0
- data/active_model_serializers-0.10.16/lib/generators/rails/resource_override.rb +12 -0
- data/active_model_serializers-0.10.16/lib/generators/rails/serializer_generator.rb +38 -0
- data/active_model_serializers-0.10.16/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/active_model_serializers-0.10.16/lib/grape/active_model_serializers.rb +18 -0
- data/active_model_serializers-0.10.16/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/active_model_serializers-0.10.16/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/active_model_serializers-0.10.16/lib/tasks/rubocop.rake +60 -0
- data/tiny-max-kit.gemspec +12 -0
- metadata +109 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Steve Klabnik
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# ActiveModelSerializers
|
|
2
|
+
|
|
3
|
+
<table>
|
|
4
|
+
<tr>
|
|
5
|
+
<td>Build Status</td>
|
|
6
|
+
<td>
|
|
7
|
+
<a href="https://github.com/rails-api/active_model_serializers/actions"><img src="https://github.com/rails-api/active_model_serializers/actions/workflows/ci.yml/badge.svg?branch=0-10-stable" alt="Build Status" ></a>
|
|
8
|
+
<a href="https://ci.appveyor.com/project/bf4/active-model-serializers/branch/0-10-stable"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
|
|
9
|
+
</td>
|
|
10
|
+
</tr>
|
|
11
|
+
<tr>
|
|
12
|
+
<td>Code Quality</td>
|
|
13
|
+
<td>
|
|
14
|
+
<a href="https://codeclimate.com/github/rails-api/active_model_serializers"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg" alt="Code Quality"></a>
|
|
15
|
+
<a href="https://codebeat.co/projects/github-com-rails-api-active_model_serializers"><img src="https://codebeat.co/badges/a9ab35fa-8b5a-4680-9d4e-a81f9a55ebcd" alt="codebeat" ></a>
|
|
16
|
+
</td>
|
|
17
|
+
</tr>
|
|
18
|
+
<tr>
|
|
19
|
+
<td>Issue Stats</td>
|
|
20
|
+
<td>
|
|
21
|
+
<a href="https://github.com/rails-api/active_model_serializers/pulse/monthly">Pulse</a>
|
|
22
|
+
</td>
|
|
23
|
+
</tr>
|
|
24
|
+
</table>
|
|
25
|
+
|
|
26
|
+
## About
|
|
27
|
+
|
|
28
|
+
ActiveModelSerializers brings convention over configuration to your JSON generation.
|
|
29
|
+
|
|
30
|
+
ActiveModelSerializers works through two components: **serializers** and **adapters**.
|
|
31
|
+
|
|
32
|
+
Serializers describe _which_ attributes and relationships should be serialized.
|
|
33
|
+
|
|
34
|
+
Adapters describe _how_ attributes and relationships should be serialized.
|
|
35
|
+
|
|
36
|
+
SerializableResource co-ordinates the resource, Adapter and Serializer to produce the
|
|
37
|
+
resource serialization. The serialization has the `#as_json`, `#to_json` and `#serializable_hash`
|
|
38
|
+
methods used by the Rails JSON Renderer. (SerializableResource actually delegates
|
|
39
|
+
these methods to the adapter.)
|
|
40
|
+
|
|
41
|
+
By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
|
|
42
|
+
But we strongly advise you to use **JsonApi Adapter**, which
|
|
43
|
+
follows 1.0 of the format specified in [jsonapi.org/format](https://jsonapi.org/format).
|
|
44
|
+
Check how to change the adapter in the sections below.
|
|
45
|
+
|
|
46
|
+
`0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
|
|
47
|
+
|
|
48
|
+
`0.10.x` is based on the `0.8.0` code, but with a more flexible
|
|
49
|
+
architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
Add this line to your application's Gemfile:
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
gem 'active_model_serializers', '~> 0.10.0'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
And then execute:
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
$ bundle
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Getting Started
|
|
66
|
+
|
|
67
|
+
See [Getting Started](docs/general/getting_started.md) for the nuts and bolts.
|
|
68
|
+
|
|
69
|
+
More information is available in the [Guides](docs) and
|
|
70
|
+
[High-level behavior](README.md#high-level-behavior).
|
|
71
|
+
|
|
72
|
+
## Getting Help
|
|
73
|
+
|
|
74
|
+
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new)
|
|
75
|
+
and see our [contributing guide](CONTRIBUTING.md).
|
|
76
|
+
|
|
77
|
+
If you have a question, please [post to Stack Overflow](https://stackoverflow.com/questions/tagged/active-model-serializers).
|
|
78
|
+
|
|
79
|
+
If you'd like to chat, we have a [community slack](https://amserializers.herokuapp.com).
|
|
80
|
+
|
|
81
|
+
Thanks!
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
If you're reading this at https://github.com/rails-api/active_model_serializers you are
|
|
86
|
+
reading documentation for our `master`, which may include features that have not
|
|
87
|
+
been released yet. Please see below for the documentation relevant to you.
|
|
88
|
+
|
|
89
|
+
- [0.10 (0-10-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-10-stable)
|
|
90
|
+
- [0.10.10 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.10)
|
|
91
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.10.10)
|
|
92
|
+
- [Guides](docs)
|
|
93
|
+
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
|
94
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.9.7)
|
|
95
|
+
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
|
96
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.8.4)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
## High-level behavior
|
|
100
|
+
|
|
101
|
+
Choose an adapter from [adapters](lib/active_model_serializers/adapter):
|
|
102
|
+
|
|
103
|
+
``` ruby
|
|
104
|
+
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Given a [serializable model](lib/active_model/serializer/lint.rb):
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
# either
|
|
111
|
+
class SomeResource < ActiveRecord::Base
|
|
112
|
+
# columns: title, body
|
|
113
|
+
end
|
|
114
|
+
# or
|
|
115
|
+
class SomeResource < ActiveModelSerializers::Model
|
|
116
|
+
attributes :title, :body
|
|
117
|
+
end
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
And initialized as:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
resource = SomeResource.new(title: 'ActiveModelSerializers', body: 'Convention over configuration')
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Given a serializer for the serializable model:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
class SomeSerializer < ActiveModel::Serializer
|
|
130
|
+
attribute :title, key: :name
|
|
131
|
+
attributes :body
|
|
132
|
+
end
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The model can be serialized as:
|
|
136
|
+
|
|
137
|
+
```ruby
|
|
138
|
+
options = {}
|
|
139
|
+
serialization = ActiveModelSerializers::SerializableResource.new(resource, options)
|
|
140
|
+
serialization.to_json
|
|
141
|
+
serialization.as_json
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
SerializableResource delegates to the adapter, which it builds as:
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
adapter_options = {}
|
|
148
|
+
adapter = ActiveModelSerializers::Adapter.create(serializer, adapter_options)
|
|
149
|
+
adapter.to_json
|
|
150
|
+
adapter.as_json
|
|
151
|
+
adapter.serializable_hash
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The adapter formats the serializer's attributes and associations (a.k.a. includes):
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
serializer_options = {}
|
|
158
|
+
serializer = SomeSerializer.new(resource, serializer_options)
|
|
159
|
+
serializer.attributes
|
|
160
|
+
serializer.associations
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Architecture
|
|
164
|
+
|
|
165
|
+
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,
|
|
166
|
+
please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
|
|
167
|
+
[0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
|
|
168
|
+
|
|
169
|
+
The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
|
|
170
|
+
|
|
171
|
+
### ActiveModel::Serializer
|
|
172
|
+
|
|
173
|
+
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/serialization.rb)
|
|
174
|
+
and exposes an `attributes` method, among a few others.
|
|
175
|
+
It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
|
|
176
|
+
It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
|
|
177
|
+
It may be useful to think of it as a
|
|
178
|
+
[presenter](https://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
|
|
179
|
+
|
|
180
|
+
#### ActiveModel::CollectionSerializer
|
|
181
|
+
|
|
182
|
+
The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
|
|
183
|
+
and, if there is no serializer, primitives.
|
|
184
|
+
|
|
185
|
+
### ActiveModelSerializers::Adapter::Base
|
|
186
|
+
|
|
187
|
+
The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
|
|
188
|
+
serializer. For example, the `Attributes` example represents each serializer as its
|
|
189
|
+
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
|
|
190
|
+
API](https://jsonapi.org/) document.
|
|
191
|
+
|
|
192
|
+
### ActiveModelSerializers::SerializableResource
|
|
193
|
+
|
|
194
|
+
The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
|
|
195
|
+
to an object that responds to `to_json`, and `as_json`. It is used in the controller to
|
|
196
|
+
encapsulate the serialization resource when rendered. However, it can also be used on its own
|
|
197
|
+
to serialize a resource outside of a controller, as well.
|
|
198
|
+
|
|
199
|
+
### Primitive handling
|
|
200
|
+
|
|
201
|
+
Definitions: A primitive is usually a String or Array. There is no serializer
|
|
202
|
+
defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
|
|
203
|
+
`to_json`). (The below also applies for any object with no serializer.)
|
|
204
|
+
|
|
205
|
+
- ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
|
|
206
|
+
|
|
207
|
+
Internally, if no serializer can be found in the controller, the resource is not decorated by
|
|
208
|
+
ActiveModelSerializers.
|
|
209
|
+
|
|
210
|
+
- However, when a primitive value is an attribute or in a collection, it is not modified.
|
|
211
|
+
|
|
212
|
+
When serializing a collection and the collection serializer (CollectionSerializer) cannot
|
|
213
|
+
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).
|
|
214
|
+
For example, when caught by `Reflection#build_association`, and the association value is set directly:
|
|
215
|
+
|
|
216
|
+
```ruby
|
|
217
|
+
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
(which is called by the adapter as `serializer.associations(*)`.)
|
|
221
|
+
|
|
222
|
+
### How options are parsed
|
|
223
|
+
|
|
224
|
+
High-level overview:
|
|
225
|
+
|
|
226
|
+
- For a **collection**
|
|
227
|
+
- `:serializer` specifies the collection serializer and
|
|
228
|
+
- `:each_serializer` specifies the serializer for each resource in the collection.
|
|
229
|
+
- For a **single resource**, the `:serializer` option is the resource serializer.
|
|
230
|
+
- Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
|
|
231
|
+
[`ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
|
|
232
|
+
The remaining options are serializer options.
|
|
233
|
+
|
|
234
|
+
Details:
|
|
235
|
+
|
|
236
|
+
1. **ActionController::Serialization**
|
|
237
|
+
1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
|
|
238
|
+
1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
|
|
239
|
+
The `adapter_opts` keys are defined in [`ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
|
|
240
|
+
1. **ActiveModelSerializers::SerializableResource**
|
|
241
|
+
1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
|
|
242
|
+
- Where `serializer?` is `use_adapter? && !!(serializer)`
|
|
243
|
+
- Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
|
|
244
|
+
False when explicit adapter is falsy (nil or false)'
|
|
245
|
+
- Where `serializer`:
|
|
246
|
+
1. from explicit `:serializer` option, else
|
|
247
|
+
2. implicitly from resource `ActiveModel::Serializer.serializer_for(resource)`
|
|
248
|
+
1. A side-effect of checking `serializer` is:
|
|
249
|
+
- The `:serializer` option is removed from the serializer_opts hash
|
|
250
|
+
- If the `:each_serializer` option is present, it is removed from the serializer_opts hash and set as the `:serializer` option
|
|
251
|
+
1. The serializer and adapter are created as
|
|
252
|
+
1. `serializer_instance = serializer.new(resource, serializer_opts)`
|
|
253
|
+
2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
|
|
254
|
+
1. **ActiveModel::Serializer::CollectionSerializer#new**
|
|
255
|
+
1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
|
|
256
|
+
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer/collection_serializer.rb#L77-L79).
|
|
257
|
+
1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
|
|
258
|
+
resource as defined by the serializer.
|
|
259
|
+
|
|
260
|
+
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
|
|
261
|
+
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
|
|
262
|
+
to know about, but not part of ActiveModelSerializers.)
|
|
263
|
+
|
|
264
|
+
### What does a 'serializable resource' look like?
|
|
265
|
+
|
|
266
|
+
- An `ActiveRecord::Base` object.
|
|
267
|
+
- Any Ruby object that passes the
|
|
268
|
+
[Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
|
|
269
|
+
[(code)](lib/active_model/serializer/lint.rb).
|
|
270
|
+
|
|
271
|
+
ActiveModelSerializers provides a
|
|
272
|
+
[`ActiveModelSerializers::Model`](lib/active_model_serializers/model.rb),
|
|
273
|
+
which is a simple serializable PORO (Plain-Old Ruby Object).
|
|
274
|
+
|
|
275
|
+
`ActiveModelSerializers::Model` may be used either as a reference implementation, or in production code.
|
|
276
|
+
|
|
277
|
+
```ruby
|
|
278
|
+
class MyModel < ActiveModelSerializers::Model
|
|
279
|
+
attributes :id, :name, :level
|
|
280
|
+
end
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
The default serializer for `MyModel` would be `MyModelSerializer` whether MyModel is an
|
|
284
|
+
ActiveRecord::Base object or not.
|
|
285
|
+
|
|
286
|
+
Outside of the controller the rules are **exactly** the same as for records. For example:
|
|
287
|
+
|
|
288
|
+
```ruby
|
|
289
|
+
render json: MyModel.new(level: 'awesome'), adapter: :json
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
would be serialized the same as
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Semantic Versioning
|
|
299
|
+
|
|
300
|
+
This project adheres to [semver](https://semver.org/)
|
|
301
|
+
|
|
302
|
+
## Contributing
|
|
303
|
+
|
|
304
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
|
4
|
+
require 'active_model_serializers/serialization_context'
|
|
5
|
+
|
|
6
|
+
module ActionController
|
|
7
|
+
module Serialization
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
include ActionController::Renderers
|
|
11
|
+
|
|
12
|
+
module ClassMethods
|
|
13
|
+
def serialization_scope(scope)
|
|
14
|
+
self._serialization_scope = scope
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
included do
|
|
19
|
+
class_attribute :_serialization_scope
|
|
20
|
+
self._serialization_scope = :current_user
|
|
21
|
+
|
|
22
|
+
attr_writer :namespace_for_serializer
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def namespace_for_serializer
|
|
26
|
+
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def namespace_for_class(klass)
|
|
30
|
+
if Module.method_defined?(:module_parent)
|
|
31
|
+
klass.module_parent
|
|
32
|
+
else
|
|
33
|
+
klass.parent
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def serialization_scope
|
|
38
|
+
return unless _serialization_scope && respond_to?(_serialization_scope, true)
|
|
39
|
+
|
|
40
|
+
send(_serialization_scope)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def get_serializer(resource, options = {})
|
|
44
|
+
unless use_adapter?
|
|
45
|
+
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
|
46
|
+
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
|
47
|
+
options[:adapter] = false
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
options.fetch(:namespace) { options[:namespace] = namespace_for_serializer }
|
|
51
|
+
|
|
52
|
+
serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
|
|
53
|
+
serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
|
|
54
|
+
serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
|
|
55
|
+
# For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
|
|
56
|
+
# Otherwise, since `serializable_resource` is not a string, the renderer would call
|
|
57
|
+
# `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
|
|
58
|
+
serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Deprecated
|
|
62
|
+
def use_adapter?
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
|
67
|
+
define_method renderer_method do |resource, options|
|
|
68
|
+
options.fetch(:serialization_context) do
|
|
69
|
+
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request, options)
|
|
70
|
+
end
|
|
71
|
+
serializable_resource = get_serializer(resource, options)
|
|
72
|
+
super(serializable_resource, options)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'set'
|
|
4
|
+
|
|
5
|
+
module ActiveModel
|
|
6
|
+
class SerializableResource
|
|
7
|
+
class << self
|
|
8
|
+
extend ActiveModelSerializers::Deprecate
|
|
9
|
+
|
|
10
|
+
delegate_and_deprecate :new, ActiveModelSerializers::SerializableResource
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveModel
|
|
4
|
+
class Serializer
|
|
5
|
+
module Adapter
|
|
6
|
+
class Attributes < DelegateClass(ActiveModelSerializers::Adapter::Attributes)
|
|
7
|
+
def initialize(serializer, options = {})
|
|
8
|
+
super(ActiveModelSerializers::Adapter::Attributes.new(serializer, options))
|
|
9
|
+
end
|
|
10
|
+
class << self
|
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Json.'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveModel
|
|
4
|
+
class Serializer
|
|
5
|
+
module Adapter
|
|
6
|
+
class Base < DelegateClass(ActiveModelSerializers::Adapter::Base)
|
|
7
|
+
class << self
|
|
8
|
+
extend ActiveModelSerializers::Deprecate
|
|
9
|
+
deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# :nocov:
|
|
13
|
+
def initialize(serializer, options = {})
|
|
14
|
+
super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
|
|
15
|
+
end
|
|
16
|
+
# :nocov:
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveModel
|
|
4
|
+
class Serializer
|
|
5
|
+
module Adapter
|
|
6
|
+
class Json < DelegateClass(ActiveModelSerializers::Adapter::Json)
|
|
7
|
+
def initialize(serializer, options = {})
|
|
8
|
+
super(ActiveModelSerializers::Adapter::Json.new(serializer, options))
|
|
9
|
+
end
|
|
10
|
+
class << self
|
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Json.new'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveModel
|
|
4
|
+
class Serializer
|
|
5
|
+
module Adapter
|
|
6
|
+
class JsonApi < DelegateClass(ActiveModelSerializers::Adapter::JsonApi)
|
|
7
|
+
def initialize(serializer, options = {})
|
|
8
|
+
super(ActiveModelSerializers::Adapter::JsonApi.new(serializer, options))
|
|
9
|
+
end
|
|
10
|
+
class << self
|
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::JsonApi.new'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveModel
|
|
4
|
+
class Serializer
|
|
5
|
+
module Adapter
|
|
6
|
+
class Null < DelegateClass(ActiveModelSerializers::Adapter::Null)
|
|
7
|
+
def initialize(serializer, options = {})
|
|
8
|
+
super(ActiveModelSerializers::Adapter::Null.new(serializer, options))
|
|
9
|
+
end
|
|
10
|
+
class << self
|
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Null.new'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_model_serializers/adapter'
|
|
4
|
+
require 'active_model_serializers/deprecate'
|
|
5
|
+
|
|
6
|
+
module ActiveModel
|
|
7
|
+
class Serializer
|
|
8
|
+
# @deprecated Use ActiveModelSerializers::Adapter instead
|
|
9
|
+
module Adapter
|
|
10
|
+
class << self
|
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
|
12
|
+
|
|
13
|
+
DEPRECATED_METHODS = [:create, :adapter_class, :adapter_map, :adapters, :register, :lookup].freeze
|
|
14
|
+
DEPRECATED_METHODS.each do |method|
|
|
15
|
+
delegate_and_deprecate method, ActiveModelSerializers::Adapter
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
require 'active_model/serializer/adapter/base'
|
|
23
|
+
require 'active_model/serializer/adapter/null'
|
|
24
|
+
require 'active_model/serializer/adapter/attributes'
|
|
25
|
+
require 'active_model/serializer/adapter/json'
|
|
26
|
+
require 'active_model/serializer/adapter/json_api'
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_model/serializer/collection_serializer'
|
|
4
|
+
|
|
5
|
+
module ActiveModel
|
|
6
|
+
class Serializer
|
|
7
|
+
class ArraySerializer < CollectionSerializer
|
|
8
|
+
class << self
|
|
9
|
+
extend ActiveModelSerializers::Deprecate
|
|
10
|
+
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_model/serializer/lazy_association'
|
|
4
|
+
|
|
5
|
+
module ActiveModel
|
|
6
|
+
class Serializer
|
|
7
|
+
# This class holds all information about serializer's association.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
10
|
+
Association = Struct.new(:reflection, :association_options) do
|
|
11
|
+
attr_reader :lazy_association
|
|
12
|
+
delegate :object, :include_data?, :virtual_value, :collection?, to: :lazy_association
|
|
13
|
+
|
|
14
|
+
def initialize(*)
|
|
15
|
+
super
|
|
16
|
+
@lazy_association = LazyAssociation.new(reflection, association_options)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @return [Symbol]
|
|
20
|
+
delegate :name, to: :reflection
|
|
21
|
+
|
|
22
|
+
# @return [Symbol]
|
|
23
|
+
def key
|
|
24
|
+
reflection_options.fetch(:key, name)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [True,False]
|
|
28
|
+
def key?
|
|
29
|
+
reflection_options.key?(:key)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @return [Hash]
|
|
33
|
+
def links
|
|
34
|
+
reflection_options.fetch(:links) || {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Hash, nil]
|
|
38
|
+
# This gets mutated, so cannot use the cached reflection_options
|
|
39
|
+
def meta
|
|
40
|
+
reflection.options[:meta]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def belongs_to?
|
|
44
|
+
reflection.foreign_key_on == :self
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def polymorphic?
|
|
48
|
+
true == reflection_options[:polymorphic]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @api private
|
|
52
|
+
def serializable_hash(adapter_options, adapter_instance)
|
|
53
|
+
association_serializer = lazy_association.serializer
|
|
54
|
+
return virtual_value if virtual_value
|
|
55
|
+
association_object = association_serializer && association_serializer.object
|
|
56
|
+
return unless association_object
|
|
57
|
+
|
|
58
|
+
serialization = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
|
|
59
|
+
|
|
60
|
+
if polymorphic? && serialization
|
|
61
|
+
polymorphic_type = association_object.class.name.underscore
|
|
62
|
+
serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
serialization
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
delegate :reflection_options, to: :lazy_association
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_model/serializer/field'
|
|
4
|
+
|
|
5
|
+
module ActiveModel
|
|
6
|
+
class Serializer
|
|
7
|
+
# Holds all the meta-data about an attribute as it was specified in the
|
|
8
|
+
# ActiveModel::Serializer class.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# class PostSerializer < ActiveModel::Serializer
|
|
12
|
+
# attribute :content
|
|
13
|
+
# attribute :name, key: :title
|
|
14
|
+
# attribute :email, key: :author_email, if: :user_logged_in?
|
|
15
|
+
# attribute :preview do
|
|
16
|
+
# truncate(object.content)
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# def user_logged_in?
|
|
20
|
+
# current_user.logged_in?
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
class Attribute < Field
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|