active_model_serializers 0.8.3 → 0.10.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +17 -0
  5. data/.rubocop.yml +105 -0
  6. data/.simplecov +110 -0
  7. data/.travis.yml +50 -24
  8. data/CHANGELOG.md +650 -6
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/CONTRIBUTING.md +105 -0
  11. data/Gemfile +69 -1
  12. data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
  13. data/README.md +195 -545
  14. data/Rakefile +64 -8
  15. data/active_model_serializers.gemspec +62 -23
  16. data/appveyor.yml +28 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/rubocop +38 -0
  20. data/bin/serve_benchmark +39 -0
  21. data/docs/README.md +41 -0
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +269 -0
  24. data/docs/general/caching.md +58 -0
  25. data/docs/general/configuration_options.md +185 -0
  26. data/docs/general/deserialization.md +100 -0
  27. data/docs/general/fields.md +31 -0
  28. data/docs/general/getting_started.md +133 -0
  29. data/docs/general/instrumentation.md +40 -0
  30. data/docs/general/key_transforms.md +40 -0
  31. data/docs/general/logging.md +21 -0
  32. data/docs/general/rendering.md +293 -0
  33. data/docs/general/serializers.md +495 -0
  34. data/docs/how-open-source-maintained.jpg +0 -0
  35. data/docs/howto/add_pagination_links.md +138 -0
  36. data/docs/howto/add_relationship_links.md +140 -0
  37. data/docs/howto/add_root_key.md +62 -0
  38. data/docs/howto/grape_integration.md +42 -0
  39. data/docs/howto/outside_controller_use.md +66 -0
  40. data/docs/howto/passing_arbitrary_options.md +27 -0
  41. data/docs/howto/serialize_poro.md +73 -0
  42. data/docs/howto/test.md +154 -0
  43. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  44. data/docs/integrations/ember-and-json-api.md +147 -0
  45. data/docs/integrations/grape.md +19 -0
  46. data/docs/jsonapi/errors.md +56 -0
  47. data/docs/jsonapi/schema/schema.json +366 -0
  48. data/docs/jsonapi/schema.md +151 -0
  49. data/docs/rfcs/0000-namespace.md +106 -0
  50. data/docs/rfcs/template.md +15 -0
  51. data/lib/action_controller/serialization.rb +43 -38
  52. data/lib/active_model/serializable_resource.rb +11 -0
  53. data/lib/active_model/serializer/adapter/attributes.rb +15 -0
  54. data/lib/active_model/serializer/adapter/base.rb +18 -0
  55. data/lib/active_model/serializer/adapter/json.rb +15 -0
  56. data/lib/active_model/serializer/adapter/json_api.rb +15 -0
  57. data/lib/active_model/serializer/adapter/null.rb +15 -0
  58. data/lib/active_model/serializer/adapter.rb +24 -0
  59. data/lib/active_model/serializer/array_serializer.rb +12 -0
  60. data/lib/active_model/serializer/association.rb +71 -0
  61. data/lib/active_model/serializer/attribute.rb +25 -0
  62. data/lib/active_model/serializer/belongs_to_reflection.rb +11 -0
  63. data/lib/active_model/serializer/collection_serializer.rb +88 -0
  64. data/lib/active_model/serializer/concerns/caching.rb +300 -0
  65. data/lib/active_model/serializer/error_serializer.rb +14 -0
  66. data/lib/active_model/serializer/errors_serializer.rb +32 -0
  67. data/lib/active_model/serializer/field.rb +90 -0
  68. data/lib/active_model/serializer/fieldset.rb +31 -0
  69. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  70. data/lib/active_model/serializer/has_one_reflection.rb +7 -0
  71. data/lib/active_model/serializer/lazy_association.rb +96 -0
  72. data/lib/active_model/serializer/link.rb +21 -0
  73. data/lib/active_model/serializer/lint.rb +150 -0
  74. data/lib/active_model/serializer/null.rb +17 -0
  75. data/lib/active_model/serializer/reflection.rb +210 -0
  76. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  77. data/lib/active_model/serializer.rb +343 -442
  78. data/lib/active_model_serializers/adapter/attributes.rb +13 -0
  79. data/lib/active_model_serializers/adapter/base.rb +83 -0
  80. data/lib/active_model_serializers/adapter/json.rb +21 -0
  81. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  82. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  83. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  84. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  85. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  86. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +88 -0
  87. data/lib/active_model_serializers/adapter/json_api/relationship.rb +104 -0
  88. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +66 -0
  89. data/lib/active_model_serializers/adapter/json_api.rb +533 -0
  90. data/lib/active_model_serializers/adapter/null.rb +9 -0
  91. data/lib/active_model_serializers/adapter.rb +98 -0
  92. data/lib/active_model_serializers/callbacks.rb +55 -0
  93. data/lib/active_model_serializers/deprecate.rb +54 -0
  94. data/lib/active_model_serializers/deserialization.rb +15 -0
  95. data/lib/active_model_serializers/json_pointer.rb +14 -0
  96. data/lib/active_model_serializers/logging.rb +122 -0
  97. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  98. data/lib/active_model_serializers/model.rb +130 -0
  99. data/lib/active_model_serializers/railtie.rb +50 -0
  100. data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
  101. data/lib/active_model_serializers/serializable_resource.rb +82 -0
  102. data/lib/active_model_serializers/serialization_context.rb +39 -0
  103. data/lib/active_model_serializers/test/schema.rb +138 -0
  104. data/lib/active_model_serializers/test/serializer.rb +125 -0
  105. data/lib/active_model_serializers/test.rb +7 -0
  106. data/lib/active_model_serializers.rb +47 -81
  107. data/lib/generators/rails/USAGE +6 -0
  108. data/lib/generators/rails/resource_override.rb +10 -0
  109. data/lib/generators/rails/serializer_generator.rb +36 -0
  110. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  111. data/lib/grape/active_model_serializers.rb +16 -0
  112. data/lib/grape/formatters/active_model_serializers.rb +32 -0
  113. data/lib/grape/helpers/active_model_serializers.rb +17 -0
  114. data/lib/tasks/rubocop.rake +53 -0
  115. data/test/action_controller/adapter_selector_test.rb +62 -0
  116. data/test/action_controller/explicit_serializer_test.rb +135 -0
  117. data/test/action_controller/json/include_test.rb +246 -0
  118. data/test/action_controller/json_api/deserialization_test.rb +112 -0
  119. data/test/action_controller/json_api/errors_test.rb +40 -0
  120. data/test/action_controller/json_api/fields_test.rb +66 -0
  121. data/test/action_controller/json_api/linked_test.rb +202 -0
  122. data/test/action_controller/json_api/pagination_test.rb +124 -0
  123. data/test/action_controller/json_api/transform_test.rb +189 -0
  124. data/test/action_controller/lookup_proc_test.rb +49 -0
  125. data/test/action_controller/namespace_lookup_test.rb +232 -0
  126. data/test/action_controller/serialization_scope_name_test.rb +235 -0
  127. data/test/action_controller/serialization_test.rb +478 -0
  128. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  129. data/test/active_model_serializers/json_pointer_test.rb +22 -0
  130. data/test/active_model_serializers/logging_test.rb +77 -0
  131. data/test/active_model_serializers/model_test.rb +142 -0
  132. data/test/active_model_serializers/railtie_test_isolated.rb +68 -0
  133. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
  134. data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
  135. data/test/active_model_serializers/test/schema_test.rb +131 -0
  136. data/test/active_model_serializers/test/serializer_test.rb +62 -0
  137. data/test/active_record_test.rb +9 -0
  138. data/test/adapter/attributes_test.rb +40 -0
  139. data/test/adapter/deprecation_test.rb +100 -0
  140. data/test/adapter/json/belongs_to_test.rb +45 -0
  141. data/test/adapter/json/collection_test.rb +104 -0
  142. data/test/adapter/json/has_many_test.rb +53 -0
  143. data/test/adapter/json/transform_test.rb +93 -0
  144. data/test/adapter/json_api/belongs_to_test.rb +155 -0
  145. data/test/adapter/json_api/collection_test.rb +96 -0
  146. data/test/adapter/json_api/errors_test.rb +76 -0
  147. data/test/adapter/json_api/fields_test.rb +96 -0
  148. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
  149. data/test/adapter/json_api/has_many_test.rb +173 -0
  150. data/test/adapter/json_api/has_one_test.rb +80 -0
  151. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +213 -0
  152. data/test/adapter/json_api/json_api_test.rb +33 -0
  153. data/test/adapter/json_api/linked_test.rb +413 -0
  154. data/test/adapter/json_api/links_test.rb +110 -0
  155. data/test/adapter/json_api/pagination_links_test.rb +206 -0
  156. data/test/adapter/json_api/parse_test.rb +137 -0
  157. data/test/adapter/json_api/relationship_test.rb +397 -0
  158. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  159. data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
  160. data/test/adapter/json_api/transform_test.rb +512 -0
  161. data/test/adapter/json_api/type_test.rb +193 -0
  162. data/test/adapter/json_test.rb +46 -0
  163. data/test/adapter/null_test.rb +22 -0
  164. data/test/adapter/polymorphic_test.rb +218 -0
  165. data/test/adapter_test.rb +67 -0
  166. data/test/array_serializer_test.rb +20 -73
  167. data/test/benchmark/app.rb +65 -0
  168. data/test/benchmark/benchmarking_support.rb +67 -0
  169. data/test/benchmark/bm_active_record.rb +81 -0
  170. data/test/benchmark/bm_adapter.rb +38 -0
  171. data/test/benchmark/bm_caching.rb +119 -0
  172. data/test/benchmark/bm_lookup_chain.rb +83 -0
  173. data/test/benchmark/bm_transform.rb +45 -0
  174. data/test/benchmark/config.ru +3 -0
  175. data/test/benchmark/controllers.rb +83 -0
  176. data/test/benchmark/fixtures.rb +219 -0
  177. data/test/cache_test.rb +651 -0
  178. data/test/collection_serializer_test.rb +127 -0
  179. data/test/fixtures/active_record.rb +113 -0
  180. data/test/fixtures/poro.rb +225 -0
  181. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  182. data/test/generators/serializer_generator_test.rb +75 -0
  183. data/test/grape_test.rb +196 -0
  184. data/test/lint_test.rb +49 -0
  185. data/test/logger_test.rb +20 -0
  186. data/test/poro_test.rb +9 -0
  187. data/test/serializable_resource_test.rb +79 -0
  188. data/test/serializers/association_macros_test.rb +37 -0
  189. data/test/serializers/associations_test.rb +518 -0
  190. data/test/serializers/attribute_test.rb +153 -0
  191. data/test/serializers/attributes_test.rb +52 -0
  192. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  193. data/test/serializers/configuration_test.rb +32 -0
  194. data/test/serializers/fieldset_test.rb +14 -0
  195. data/test/serializers/meta_test.rb +202 -0
  196. data/test/serializers/options_test.rb +32 -0
  197. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  198. data/test/serializers/reflection_test.rb +479 -0
  199. data/test/serializers/root_test.rb +21 -0
  200. data/test/serializers/serialization_test.rb +55 -0
  201. data/test/serializers/serializer_for_test.rb +136 -0
  202. data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
  203. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  204. data/test/support/isolated_unit.rb +84 -0
  205. data/test/support/rails5_shims.rb +53 -0
  206. data/test/support/rails_app.rb +38 -0
  207. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  209. data/test/support/schemas/custom/show.json +7 -0
  210. data/test/support/schemas/hyper_schema.json +93 -0
  211. data/test/support/schemas/render_using_json_api.json +43 -0
  212. data/test/support/schemas/simple_json_pointers.json +10 -0
  213. data/test/support/serialization_testing.rb +79 -0
  214. data/test/test_helper.rb +59 -21
  215. metadata +529 -43
  216. data/DESIGN.textile +0 -586
  217. data/Gemfile.edge +0 -9
  218. data/bench/perf.rb +0 -43
  219. data/cruft.md +0 -19
  220. data/lib/active_model/array_serializer.rb +0 -104
  221. data/lib/active_model/serializer/associations.rb +0 -233
  222. data/lib/active_record/serializer_override.rb +0 -16
  223. data/lib/generators/resource_override.rb +0 -13
  224. data/lib/generators/serializer/USAGE +0 -9
  225. data/lib/generators/serializer/serializer_generator.rb +0 -42
  226. data/lib/generators/serializer/templates/serializer.rb +0 -19
  227. data/test/association_test.rb +0 -592
  228. data/test/caching_test.rb +0 -96
  229. data/test/generators_test.rb +0 -85
  230. data/test/no_serialization_scope_test.rb +0 -34
  231. data/test/serialization_scope_name_test.rb +0 -67
  232. data/test/serialization_test.rb +0 -392
  233. data/test/serializer_support_test.rb +0 -51
  234. data/test/serializer_test.rb +0 -1465
  235. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,495 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # Serializers
4
+
5
+ Given a serializer class:
6
+
7
+ ```ruby
8
+ class SomeSerializer < ActiveModel::Serializer
9
+ end
10
+ ```
11
+
12
+ The following methods may be defined in it:
13
+
14
+ ### Attributes
15
+
16
+ #### ::attributes
17
+
18
+ Serialization of the resource `title` and `body`
19
+
20
+ | In Serializer | #attributes |
21
+ |---------------------------- |-------------|
22
+ | `attributes :title, :body` | `{ title: 'Some Title', body: 'Some Body' }`
23
+ | `attributes :title, :body`<br>`def body "Special #{object.body}" end` | `{ title: 'Some Title', body: 'Special Some Body' }`
24
+
25
+
26
+ #### ::attribute
27
+
28
+ Serialization of the resource `title`
29
+
30
+ | In Serializer | #attributes |
31
+ |---------------------------- |-------------|
32
+ | `attribute :title` | `{ title: 'Some Title' } `
33
+ | `attribute :title, key: :name` | `{ name: 'Some Title' } `
34
+ | `attribute(:title) { 'A Different Title'}` | `{ title: 'A Different Title' } `
35
+ | `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
36
+
37
+ An `if` or `unless` option can make an attribute conditional. It takes a symbol of a method name on the serializer, or a lambda literal.
38
+
39
+ e.g.
40
+
41
+ ```ruby
42
+ attribute :private_data, if: :is_current_user?
43
+ attribute :another_private_data, if: -> { scope.admin? }
44
+
45
+ def is_current_user?
46
+ object.id == current_user.id
47
+ end
48
+ ```
49
+
50
+ ### Associations
51
+
52
+ The interface for associations is, generically:
53
+
54
+ > `association_type(association_name, options, &block)`
55
+
56
+ Where:
57
+
58
+ - `association_type` may be `has_one`, `has_many`, `belongs_to`.
59
+ - `association_name` is a method name the serializer calls.
60
+ - optional: `options` may be:
61
+ - `key:` The name used for the serialized association.
62
+ - `serializer:`
63
+ - `if:`
64
+ - `unless:`
65
+ - `virtual_value:`
66
+ - `polymorphic:` defines if polymorphic relation type should be nested in serialized association.
67
+ - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship.
68
+ - `class_name:` the (String) model name used to determine `type`, when `type` is not given. e.g. `class_name: "Comment"` would imply the type `comments`
69
+ - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object.
70
+ - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details.
71
+ - optional: `&block` is a context that returns the association's attributes.
72
+ - prevents `association_name` method from being called.
73
+ - return value of block is used as the association value.
74
+ - yields the `serializer` to the block.
75
+ - `include_data false` prevents the `data` key from being rendered in the JSON API relationship.
76
+
77
+ #### ::has_one
78
+
79
+ e.g.
80
+
81
+ ```ruby
82
+ has_one :bio
83
+ has_one :blog, key: :site
84
+ has_one :blog, class_name: "Blog"
85
+ has_one :maker, virtual_value: { id: 1 }
86
+
87
+ has_one :blog do |serializer|
88
+ serializer.cached_blog
89
+ end
90
+
91
+ def cached_blog
92
+ cache_store.fetch("cached_blog:#{object.updated_at}") do
93
+ Blog.find(object.blog_id)
94
+ end
95
+ end
96
+ ```
97
+
98
+ ```ruby
99
+ has_one :blog, if: :show_blog?
100
+ # you can also use a string or lambda
101
+ # has_one :blog, if: 'scope.admin?'
102
+ # has_one :blog, if: -> (serializer) { serializer.scope.admin? }
103
+ # has_one :blog, if: -> { scope.admin? }
104
+
105
+ def show_blog?
106
+ scope.admin?
107
+ end
108
+ ```
109
+
110
+ #### ::has_many
111
+
112
+ e.g.
113
+
114
+ ```ruby
115
+ has_many :comments
116
+ has_many :comments, key: :reviews
117
+ has_many :comments, serializer: CommentPreviewSerializer
118
+ has_many :comments, class_name: "Comment"
119
+ has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
120
+ has_many :comments, key: :last_comments do
121
+ last(1)
122
+ end
123
+ ```
124
+
125
+ #### ::belongs_to
126
+
127
+ e.g.
128
+
129
+ ```ruby
130
+ belongs_to :author, serializer: AuthorPreviewSerializer
131
+ belongs_to :author, key: :writer
132
+ belongs_to :author, class_name: "Author"
133
+ belongs_to :post
134
+ belongs_to :blog
135
+ def blog
136
+ Blog.new(id: 999, name: 'Custom blog')
137
+ end
138
+ ```
139
+
140
+ ### Polymorphic Relationships
141
+
142
+ Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
143
+
144
+ ```ruby
145
+ class PictureSerializer < ActiveModel::Serializer
146
+ has_one :imageable
147
+ end
148
+ ```
149
+
150
+ You can specify the serializers by [overriding serializer_for](serializers.md#overriding-association-serializer-lookup). For more context about polymorphic relationships, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
151
+
152
+ ### Caching
153
+
154
+ #### ::cache
155
+
156
+ e.g.
157
+
158
+ ```ruby
159
+ cache key: 'post', expires_in: 0.1, skip_digest: true
160
+ cache expires_in: 1.day, skip_digest: true
161
+ cache key: 'writer', skip_digest: true
162
+ cache only: [:name], skip_digest: true
163
+ cache except: [:content], skip_digest: true
164
+ cache key: 'blog'
165
+ cache only: [:id]
166
+ ```
167
+
168
+ #### #cache_key
169
+
170
+ e.g.
171
+
172
+ ```ruby
173
+ # Uses a custom non-time-based cache key
174
+ def cache_key
175
+ "#{self.class.name.downcase}/#{self.id}"
176
+ end
177
+ ```
178
+
179
+ ### Other
180
+
181
+ #### ::type
182
+
183
+ When using the `:json_api` adapter, the `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
184
+
185
+ When using the `:json` adapter, the `::type` method defines the name of the root element.
186
+
187
+ It either takes a `String` or `Symbol` as parameter.
188
+
189
+ Note: This method is useful only when using the `:json_api` or `:json` adapter.
190
+
191
+ Examples:
192
+ ```ruby
193
+ class UserProfileSerializer < ActiveModel::Serializer
194
+ type 'profile'
195
+
196
+ attribute :name
197
+ end
198
+ class AuthorProfileSerializer < ActiveModel::Serializer
199
+ type :profile
200
+
201
+ attribute :name
202
+ end
203
+ ```
204
+
205
+ With the `:json_api` adapter, the previous serializers would be rendered as:
206
+
207
+ ``` json
208
+ {
209
+ "data": {
210
+ "id": "1",
211
+ "type": "profile",
212
+ "attributes": {
213
+ "name": "Julia"
214
+ }
215
+ }
216
+ }
217
+ ```
218
+
219
+ With the `:json` adapter, the previous serializer would be rendered as:
220
+
221
+ ``` json
222
+ {
223
+ "profile": {
224
+ "name": "Julia"
225
+ }
226
+ }
227
+ ```
228
+
229
+ #### ::link
230
+
231
+ ```ruby
232
+ link :self do
233
+ href "https://example.com/link_author/#{object.id}"
234
+ end
235
+ link(:author) { link_author_url(object) }
236
+ link(:link_authors) { link_authors_url }
237
+ link :other, 'https://example.com/resource'
238
+ link(:posts) { link_author_posts_url(object) }
239
+ ```
240
+
241
+ Just like attributes, links also support conditions in options
242
+ ```ruby
243
+ link(:secret, if: :internal?) { object.secret_link }
244
+
245
+ def internal?
246
+ instance_options[:context] == :internal
247
+ end
248
+ ```
249
+
250
+ #### #object
251
+
252
+ The object being serialized.
253
+
254
+ #### #root
255
+
256
+ Resource root which is included in `JSON` adapter. As you can see at [Adapters Document](adapters.md), `Attribute` adapter (default) and `JSON API` adapter does not include root at top level.
257
+ By default, the resource root comes from the `model_name` of the serialized object's class.
258
+
259
+ There are several ways to specify root:
260
+ * [Overriding the root key](rendering.md#overriding-the-root-key)
261
+ * [Setting `type`](serializers.md#type)
262
+ * Specifying the `root` option, e.g. `root: 'specific_name'`, during the serializer's initialization:
263
+
264
+ ```ruby
265
+ ActiveModelSerializers::SerializableResource.new(foo, root: 'bar')
266
+ ```
267
+
268
+ #### #scope
269
+
270
+ Allows you to include in the serializer access to an external method.
271
+
272
+ It's intended to provide an authorization context to the serializer, so that
273
+ you may e.g. show an admin all comments on a post, else only published comments.
274
+
275
+ - `scope` is a method on the serializer instance that comes from `options[:scope]`. It may be nil.
276
+ - `scope_name` is an option passed to the new serializer (`options[:scope_name]`). The serializer
277
+ defines a method with that name that calls the `scope`, e.g. `def current_user; scope; end`.
278
+ Note: it does not define the method if the serializer instance responds to it.
279
+
280
+ That's a lot of words, so here's some examples:
281
+
282
+ First, let's assume the serializer is instantiated in the controller, since that's the usual scenario.
283
+ We'll refer to the serialization context as `controller`.
284
+
285
+ | options | `Serializer#scope` | method definition |
286
+ |-------- | ------------------|--------------------|
287
+ | `scope: current_user, scope_name: :current_user` | `current_user` | `Serializer#current_user` calls `controller.current_user`
288
+ | `scope: view_context, scope_name: :view_context` | `view_context` | `Serializer#view_context` calls `controller.view_context`
289
+
290
+ We can take advantage of the scope to customize the objects returned based
291
+ on the current user (scope).
292
+
293
+ For example, we can limit the posts the current user sees to those they created:
294
+
295
+ ```ruby
296
+ class PostSerializer < ActiveModel::Serializer
297
+ attributes :id, :title, :body
298
+
299
+ # scope comments to those created_by the current user
300
+ has_many :comments do
301
+ object.comments.where(created_by: current_user)
302
+ end
303
+ end
304
+ ```
305
+
306
+ Whether you write the method as above or as `object.comments.where(created_by: scope)`
307
+ is a matter of preference (assuming `scope_name` has been set).
308
+
309
+ Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
310
+
311
+ ##### Controller Authorization Context
312
+
313
+ In the controller, the scope/scope_name options are equal to
314
+ the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
315
+ which is `:current_user`, by default.
316
+
317
+ Specifically, the `scope_name` is defaulted to `:current_user`, and may be set as
318
+ `serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
319
+ present and the controller responds to `scope_name`.
320
+
321
+ Thus, in a serializer, the controller provides `current_user` as the
322
+ current authorization scope when you call `render :json`.
323
+
324
+ **IMPORTANT**: Since the scope is set at render, you may want to customize it so that `current_user` isn't
325
+ called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
326
+ in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
327
+
328
+ We can change the scope from `current_user` to `view_context`, which is included in subclasses of `ActionController::Base`.
329
+
330
+ ```diff
331
+ class SomeController < ActionController::Base
332
+ + serialization_scope :view_context
333
+
334
+ def current_user
335
+ User.new(id: 2, name: 'Bob', admin: true)
336
+ end
337
+
338
+ def edit
339
+ user = User.new(id: 1, name: 'Pete')
340
+ render json: user, serializer: AdminUserSerializer, adapter: :json_api
341
+ end
342
+ end
343
+ ```
344
+
345
+ We could then use the controller method `view_context` in our serializer, like so:
346
+
347
+ ```diff
348
+ class AdminUserSerializer < ActiveModel::Serializer
349
+ attributes :id, :name, :can_edit
350
+
351
+ def can_edit?
352
+ + view_context.current_user.admin?
353
+ end
354
+ end
355
+ ```
356
+
357
+ So that when we render the `#edit` action, we'll get
358
+
359
+ ```json
360
+ {"data":{"id":"1","type":"users","attributes":{"name":"Pete","can_edit":true}}}
361
+ ```
362
+
363
+ Where `can_edit` is `view_context.current_user.admin?` (true).
364
+
365
+ You can also tell what to set as `serialization_scope` for specific actions.
366
+
367
+ For example, use `admin_user` only for `Admin::PostSerializer` and `current_user` for rest.
368
+
369
+ ```ruby
370
+ class PostsController < ActionController::Base
371
+
372
+ before_action only: :edit do
373
+ self.class.serialization_scope :admin_user
374
+ end
375
+
376
+ def show
377
+ render json: @post, serializer: PostSerializer
378
+ end
379
+
380
+ def edit
381
+ @post.save
382
+ render json: @post, serializer: Admin::PostSerializer
383
+ end
384
+
385
+ private
386
+
387
+ def admin_user
388
+ User.new(id: 2, name: 'Bob', admin: true)
389
+ end
390
+
391
+ def current_user
392
+ User.new(id: 2, name: 'Bob', admin: false)
393
+ end
394
+ end
395
+ ```
396
+ Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
397
+
398
+ #### #read_attribute_for_serialization(key)
399
+
400
+ The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
401
+
402
+ #### #links
403
+
404
+ Allows you to modify the `links` node. By default, this node will be populated with the attributes set using the [::link](#link) method. Using `links: nil` will remove the `links` node.
405
+
406
+ ```ruby
407
+ ActiveModelSerializers::SerializableResource.new(
408
+ @post,
409
+ adapter: :json_api,
410
+ links: {
411
+ self: {
412
+ href: 'http://example.com/posts',
413
+ meta: {
414
+ stuff: 'value'
415
+ }
416
+ }
417
+ }
418
+ )
419
+ ```
420
+
421
+ #### #json_key
422
+
423
+ Returns the key used by the adapter as the resource root. See [root](#root) for more information.
424
+
425
+ ## Examples
426
+
427
+ Given two models, a `Post(title: string, body: text)` and a
428
+ `Comment(name: string, body: text, post_id: integer)`, you will have two
429
+ serializers:
430
+
431
+ ```ruby
432
+ class PostSerializer < ActiveModel::Serializer
433
+ cache key: 'posts', expires_in: 3.hours
434
+ attributes :title, :body
435
+
436
+ has_many :comments
437
+ end
438
+ ```
439
+
440
+ and
441
+
442
+ ```ruby
443
+ class CommentSerializer < ActiveModel::Serializer
444
+ attributes :name, :body
445
+
446
+ belongs_to :post
447
+ end
448
+ ```
449
+
450
+ Generally speaking, you, as a user of ActiveModelSerializers, will write (or generate) these
451
+ serializer classes.
452
+
453
+ ## More Info
454
+
455
+ For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb)
456
+
457
+ ## Overriding association methods
458
+
459
+ To override an association, call `has_many`, `has_one` or `belongs_to` with a block:
460
+
461
+ ```ruby
462
+ class PostSerializer < ActiveModel::Serializer
463
+ has_many :comments do
464
+ object.comments.active
465
+ end
466
+ end
467
+ ```
468
+
469
+ ## Overriding attribute methods
470
+
471
+ To override an attribute, call `attribute` with a block:
472
+
473
+ ```ruby
474
+ class PostSerializer < ActiveModel::Serializer
475
+ attribute :body do
476
+ object.body.downcase
477
+ end
478
+ end
479
+ ```
480
+
481
+ ## Overriding association serializer lookup
482
+
483
+ If you want to define a specific serializer lookup for your associations, you can override
484
+ the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
485
+
486
+ ```ruby
487
+ class MySerializer < ActiveModel::Serializer
488
+ def self.serializer_for(model, options)
489
+ return SparseAdminSerializer if model.class.name == 'Admin'
490
+ super
491
+ end
492
+
493
+ # the rest of the serializer
494
+ end
495
+ ```
Binary file
@@ -0,0 +1,138 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # How to add pagination links
4
+
5
+ ### JSON API adapter
6
+
7
+ Pagination links will be included in your response automatically as long as
8
+ the resource is paginated and if you are using the ```JsonApi``` adapter.
9
+
10
+ If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari)
11
+ or [WillPaginate](https://github.com/mislav/will_paginate).
12
+
13
+ Although the other adapters do not have this feature, it is possible to
14
+ implement pagination links to `JSON` adapter. For more information about it,
15
+ please check our docs.
16
+
17
+ ###### Kaminari examples
18
+
19
+ ```ruby
20
+ #array
21
+ @posts = Kaminari.paginate_array([1, 2, 3]).page(3).per(1)
22
+ render json: @posts
23
+
24
+ #active_record
25
+ @posts = Post.page(3).per(1)
26
+ render json: @posts
27
+ ```
28
+
29
+ ###### WillPaginate examples
30
+
31
+ ```ruby
32
+ #array
33
+ @posts = [1,2,3].paginate(page: 3, per_page: 1)
34
+ render json: @posts
35
+
36
+ #active_record
37
+ @posts = Post.page(3).per_page(1)
38
+ render json: @posts
39
+ ```
40
+
41
+ ```ruby
42
+ ActiveModelSerializers.config.adapter = :json_api
43
+ ```
44
+
45
+ ex:
46
+ ```json
47
+ {
48
+ "data": [
49
+ {
50
+ "type": "articles",
51
+ "id": "3",
52
+ "attributes": {
53
+ "title": "JSON API paints my bikeshed!",
54
+ "body": "The shortest article. Ever.",
55
+ "created": "2015-05-22T14:56:29.000Z",
56
+ "updated": "2015-05-22T14:56:28.000Z"
57
+ }
58
+ }
59
+ ],
60
+ "links": {
61
+ "self": "http://example.com/articles?page[number]=3&page[size]=1",
62
+ "first": "http://example.com/articles?page[number]=1&page[size]=1",
63
+ "prev": "http://example.com/articles?page[number]=2&page[size]=1",
64
+ "next": "http://example.com/articles?page[number]=4&page[size]=1",
65
+ "last": "http://example.com/articles?page[number]=13&page[size]=1"
66
+ }
67
+ }
68
+ ```
69
+
70
+ ActiveModelSerializers pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
71
+
72
+
73
+ ### JSON adapter
74
+
75
+ If you are not using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
76
+
77
+ Add this method to your base API controller.
78
+
79
+ ```ruby
80
+ def pagination_dict(collection)
81
+ {
82
+ current_page: collection.current_page,
83
+ next_page: collection.next_page,
84
+ prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
85
+ total_pages: collection.total_pages,
86
+ total_count: collection.total_count
87
+ }
88
+ end
89
+ ```
90
+
91
+ Then, use it on your render method.
92
+
93
+ ```ruby
94
+ render json: posts, meta: pagination_dict(posts)
95
+ ```
96
+
97
+ ex.
98
+ ```json
99
+ {
100
+ "posts": [
101
+ {
102
+ "id": 2,
103
+ "title": "JSON API paints my bikeshed!",
104
+ "body": "The shortest article. Ever."
105
+ }
106
+ ],
107
+ "meta": {
108
+ "current_page": 3,
109
+ "next_page": 4,
110
+ "prev_page": 2,
111
+ "total_pages": 10,
112
+ "total_count": 10
113
+ }
114
+ }
115
+ ```
116
+
117
+ You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer.
118
+
119
+ ```ruby
120
+ render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@posts)
121
+ ```
122
+
123
+ ```ruby
124
+ #expects pagination!
125
+ def meta_attributes(collection, extra_meta = {})
126
+ {
127
+ current_page: collection.current_page,
128
+ next_page: collection.next_page,
129
+ prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
130
+ total_pages: collection.total_pages,
131
+ total_count: collection.total_count
132
+ }.merge(extra_meta)
133
+ end
134
+ ```
135
+
136
+ ### Attributes adapter
137
+
138
+ This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.