blob: d870d08d70b1a1b4775a2085206b176c460a0c9b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# Serializers
This is a documentation for classes located in `app/serializers` directory.
In GitLab, we use [grape-entities][grape-entity-project], accompanied by a
serializer, to convert a Ruby object to its JSON representation.
Serializers are typically used in a controllers to build a JSON response
that is usually consumed by a frontend code.
## What is a serializer?
Serializer is a class that encapsulates all business rules for building a JSON
response using serialization entities.
## What are serialization entities?
Entities are a lightweight structures that allow to represent domain models
in a consistent and abstracted way, and reuse them as a building blocks to
create a payload.
Entities located in `app/serializers` are usually derived from a
[`Grape::Entity`][grape-entity] class.
Serialization entities that do require a to have a knowledge about specific
elements of the request, need to mix `RequestAwareEntity` in.
## How to implement a serializer?
In order to effectively implement a serializer it is necessary to create a new
class in `app/serializers`. See existing serializers for an example.
## How to write tests for a serializer?
## How to use serializer in a controller?
[grape-entity-project]: https://github.com/ruby-grape/grape-entity
[grape-entity-class]: https://github.com/ruby-grape/grape-entity/blob/master/lib/grape_entity/entity.rb
|