|
| 1 | +# Serializers |
| 2 | + |
| 3 | +## About |
| 4 | + |
| 5 | +ActiveModelSerializers works through two components: **serializers** and **adapters**. |
| 6 | + |
| 7 | +Serializers describe _which_ attributes and relationships should be serialized. |
| 8 | + |
| 9 | +```ruby |
| 10 | +serializer = SomeSerializer.new(resource, serializer_options) |
| 11 | +serializer.attributes |
| 12 | +serializer.associations |
| 13 | +``` |
| 14 | + |
| 15 | +Adapters describe _how_ attributes and relationships should be serialized. |
| 16 | + |
| 17 | +```ruby |
| 18 | +adapter = Adapter.create(serializer, adapter_options) |
| 19 | +adapter.to_json |
| 20 | +adapter.as_json |
| 21 | +adapter.serializable_hash |
| 22 | +``` |
| 23 | + |
| 24 | +SerializableResource co-ordinates the resource, Adapter and Serializer to produce the |
| 25 | +resource serialization, which has the `#as_json`, `#to_json` and `#serializable_hash` |
| 26 | +methods used by the Rails JSON Renderer. |
| 27 | + |
| 28 | +```ruby |
| 29 | +serialization = SerializableResource.new(resource, options) |
| 30 | +serialization.to_json |
| 31 | +serialization.as_json |
| 32 | +serialization.serializable_hash |
| 33 | +``` |
| 34 | + |
| 35 | +See [ARCHITECTURE.md](../ARCHITECTURE.md) for more information. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +Given a serializer class: |
| 40 | + |
| 41 | +```ruby |
| 42 | +class SomeSerializer < ActiveModel::Serializer |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +The following methods may be defined in it: |
| 47 | + |
| 48 | +### Attributes |
| 49 | + |
| 50 | +#### ::attributes |
| 51 | + |
| 52 | +Serialization of the resource `title` and `body` |
| 53 | + |
| 54 | +| `attributes :title, :body` | `{ title: 'Some Title', body: 'Some Body' }` | |
| 55 | +| `attributes :title, :body`<br>`def body "Special #{object.body}" end` | `{ title: 'Some Title', body: 'Special Some Body' }` | |
| 56 | + |
| 57 | + |
| 58 | +#### ::attribute |
| 59 | + |
| 60 | +Serialization of the resource `title` |
| 61 | + |
| 62 | +| `attribute :title` | { title: 'Some Title' } ` |
| 63 | +| `attribute :title, key: :name` | { name: 'Some Title' } ` |
| 64 | +| `attribute :title { 'A Different Title'}` | { title: 'A Different Title' } ` |
| 65 | +| `attribute :title`<br>`def title 'A Different Title' end` | { title: 'A Different Title' } ` |
| 66 | + |
| 67 | + |
| 68 | +### Associations |
| 69 | + |
| 70 | +#### ::has_one |
| 71 | + |
| 72 | +e.g. |
| 73 | + |
| 74 | +```ruby |
| 75 | +has_one :bio |
| 76 | +has_one :blog, key: :site |
| 77 | +has_one :maker, virtual_value: { id: 1 } |
| 78 | +``` |
| 79 | + |
| 80 | +#### ::has_many |
| 81 | + |
| 82 | +e.g. |
| 83 | + |
| 84 | +```ruby |
| 85 | +has_many :comments |
| 86 | +has_many :comments, key: :reviews |
| 87 | +has_many :comments, serializer: CommentPreviewSerializer |
| 88 | +has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }] |
| 89 | +has_many :comments, key: :last_comments do |
| 90 | + last(1) |
| 91 | +end |
| 92 | +``` |
| 93 | + |
| 94 | +#### ::belongs_to |
| 95 | + |
| 96 | +e.g. |
| 97 | + |
| 98 | +```ruby |
| 99 | +belongs_to :author, serializer: AuthorPreviewSerializer |
| 100 | +belongs_to :author, key: :writer |
| 101 | +belongs_to :post |
| 102 | +belongs_to :blog |
| 103 | +def blog |
| 104 | + Blog.new(id: 999, name: 'Custom blog') |
| 105 | +end |
| 106 | +``` |
| 107 | + |
| 108 | +### Caching |
| 109 | + |
| 110 | +#### ::cache |
| 111 | + |
| 112 | +e.g. |
| 113 | + |
| 114 | +```ruby |
| 115 | +cache key: 'post', expires_in: 0.1, skip_digest: true |
| 116 | +cache expires_in: 1.day, skip_digest: true |
| 117 | +cache key: 'writer', skip_digest: true |
| 118 | +cache only: [:name], skip_digest: true |
| 119 | +cache except: [:content], skip_digest: true |
| 120 | +cache key: 'blog' |
| 121 | +cache only: [:id] |
| 122 | +``` |
| 123 | + |
| 124 | +#### #cache_key |
| 125 | + |
| 126 | +e.g. |
| 127 | + |
| 128 | +```ruby |
| 129 | +# Uses a custom non-time-based cache key |
| 130 | +def cache_key |
| 131 | + "#{self.class.name.downcase}/#{self.id}" |
| 132 | +end |
| 133 | +``` |
| 134 | + |
| 135 | +### Other |
| 136 | + |
| 137 | +### ::type |
| 138 | + |
| 139 | +e.g. |
| 140 | + |
| 141 | +```ruby |
| 142 | +class UserProfileSerializer < ActiveModel::Serializer |
| 143 | + type 'profile' |
| 144 | +end |
| 145 | +``` |
| 146 | + |
| 147 | +### ::link |
| 148 | + |
| 149 | +For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb) |
0 commit comments