Skip to content

Commit ed3c510

Browse files
committed
Merge pull request #1 from rafaelfranca/patch-1
Now @josevalim can stop to fight with textile
2 parents 5a230e8 + 4900432 commit ed3c510

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

README.textile

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ h3. The Most Basic Serializer
3535

3636
A basic serializer is a simple Ruby object named after the model class it is serializing.
3737

38-
<pre><code>
38+
<pre lang="ruby">
3939
class PostSerializer
4040
def initialize(post, scope)
4141
@post, @scope = post, scope
@@ -45,22 +45,22 @@ class PostSerializer
4545
{ post: { title: @post.name, body: @post.body } }
4646
end
4747
end
48-
</code></pre>
48+
</pre>
4949

5050
A serializer is initialized with two parameters: the model object it should serialize and an authorization scope. By default, the
5151
authorization scope is the current user (+current_user+) but you can use a different object if you want. The serializer also
5252
implements an +as_json+ method, which returns a Hash that will be sent to the JSON encoder.
5353

5454
Rails will transparently use your serializer when you use +render :json+ in your controller.
5555

56-
<pre><code>
56+
<pre lang="ruby">
5757
class PostsController < ApplicationController
5858
def show
5959
@post = Post.find(params[:id])
6060
render json: @post
6161
end
6262
end
63-
</code></pre>
63+
</pre>
6464

6565
Because +respond_with+ uses +render :json+ under the hood for JSON requests, Rails will automatically use your serializer when
6666
you use +respond_with+ as well.
@@ -70,7 +70,7 @@ h4. +serializable_hash+
7070
In general, you will want to implement +serializable_hash+ and +as_json+ to allow serializers to embed associated content
7171
directly. The easiest way to implement these two methods is to have +as_json+ call +serializable_hash+ and insert the root.
7272

73-
<pre><code>
73+
<pre lang="ruby">
7474
class PostSerializer
7575
def initialize(post, scope)
7676
@post, @scope = post, scope
@@ -84,14 +84,14 @@ class PostSerializer
8484
{ post: serializable_hash }
8585
end
8686
end
87-
</code></pre>
87+
</pre>
8888

8989
h4. Authorization
9090

9191
Let's update our serializer to include the email address of the author of the post, but only if the current user has superuser
9292
access.
9393

94-
<pre><code>
94+
<pre lang="ruby">
9595
class PostSerializer
9696
def initialize(post, scope)
9797
@post, @scope = post, scope
@@ -120,14 +120,14 @@ private
120120
@scope.superuser?
121121
end
122122
end
123-
</code></pre>
123+
</pre>
124124

125125
h4. Testing
126126

127127
One benefit of encapsulating our objects this way is that it becomes extremely straight-forward to test the serialization
128128
logic in isolation.
129129

130-
<pre><code>
130+
<pre lang="ruby">
131131
require "ostruct"
132132

133133
class PostSerializerTest < ActiveSupport::TestCase
@@ -157,7 +157,7 @@ class PostSerializerTest < ActiveSupport::TestCase
157157
assert_empty hash
158158
end
159159
end
160-
</code></pre>
160+
</pre>
161161

162162
It's important to note that serializer objects define a clear interface specifically for serializing an existing object.
163163
In this case, the serializer expects to receive a post object with +name+, +body+ and +email+ attributes and an authorization
@@ -168,7 +168,7 @@ the serializer doesn't need to concern itself with how the authorization scope d
168168
whether it is set. In general, you should document these requirements in your serializer files and programatically via tests.
169169
The documentation library +YARD+ provides excellent tools for describing this kind of requirement:
170170

171-
<pre><code>
171+
<pre lang="ruby">
172172
class PostSerializer
173173
# @param [~body, ~title, ~email] post the post to serialize
174174
# @param [~super] scope the authorization scope for this serializer
@@ -178,7 +178,7 @@ class PostSerializer
178178

179179
# ...
180180
end
181-
</code></pre>
181+
</pre>
182182

183183
h3. Attribute Sugar
184184

@@ -189,7 +189,7 @@ For example, you will sometimes want to simply include a number of existing attr
189189
JSON. In the above example, the +title+ and +body+ attributes were always included in the JSON. Let's see how to use
190190
+ActiveModel::Serializer+ to simplify our post serializer.
191191

192-
<pre><code>
192+
<pre lang="ruby">
193193
class PostSerializer < ActiveModel::Serializer
194194
attributes :title, :body
195195

@@ -212,7 +212,7 @@ private
212212
@scope.superuser?
213213
end
214214
end
215-
</code></pre>
215+
</pre>
216216

217217
First, we specified the list of included attributes at the top of the class. This will create an instance method called
218218
+attributes+ that extracts those attributes from the post model.
@@ -223,7 +223,7 @@ Next, we use the attributes methood in our +serializable_hash+ method, which all
223223
earlier. We could also eliminate the +as_json+ method, as +ActiveModel::Serializer+ provides a default +as_json+ method for
224224
us that calls our +serializable_hash+ method and inserts a root. But we can go a step further!
225225

226-
<pre><code>
226+
<pre lang="ruby">
227227
class PostSerializer < ActiveModel::Serializer
228228
attributes :title, :body
229229

@@ -238,7 +238,7 @@ private
238238
@scope.superuser?
239239
end
240240
end
241-
</code></pre>
241+
</pre>
242242

243243
The superclass provides a default +initialize+ method as well as a default +serializable_hash+ method, which uses
244244
+attributes+. We can call +super+ to get the hash based on the attributes we declared, and then add in any additional
@@ -251,7 +251,7 @@ h3. Associations
251251
In most JSON APIs, you will want to include associated objects with your serialized object. In this case, let's include
252252
the comments with the current post.
253253

254-
<pre><code>
254+
<pre lang="ruby">
255255
class PostSerializer < ActiveModel::Serializer
256256
attributes :title, :body
257257
has_many :comments
@@ -267,11 +267,11 @@ private
267267
@scope.superuser?
268268
end
269269
end
270-
</code></pre>
270+
</pre>
271271

272272
The default +serializable_hash+ method will include the comments as embedded objects inside the post.
273273

274-
<pre><code>
274+
<pre lang="json">
275275
{
276276
post: {
277277
title: "Hello Blog!",
@@ -284,14 +284,14 @@ The default +serializable_hash+ method will include the comments as embedded obj
284284
]
285285
}
286286
}
287-
</code></pre>
287+
</pre>
288288

289289
Rails uses the same logic to generate embedded serializations as it does when you use +render :json+. In this case,
290290
because you didn't define a +CommentSerializer+, Rails used the default +as_json+ on your comment object.
291291

292292
If you define a serializer, Rails will automatically instantiate it with the existing authorization scope.
293293

294-
<pre><code>
294+
<pre lang="ruby">
295295
class CommentSerializer
296296
def initialize(comment, scope)
297297
@comment, @scope = comment, scope
@@ -305,26 +305,26 @@ class CommentSerializer
305305
{ comment: serializable_hash }
306306
end
307307
end
308-
</code></pre>
308+
</pre>
309309

310310
If we define the above comment serializer, the outputted JSON will change to:
311311

312-
<pre><code>
312+
<pre lang="json">
313313
{
314314
post: {
315315
title: "Hello Blog!",
316316
body: "This is my first post. Isn't it fabulous!",
317317
comments: [{ title: "Awesome" }]
318318
}
319319
}
320-
</code></pre>
320+
</pre>
321321

322322
Let's imagine that our comment system allows an administrator to kill a comment, and we only want to allow
323323
users to see the comments they're entitled to see. By default, +has_many :comments+ will simply use the
324324
+comments+ accessor on the post object. We can override the +comments+ accessor to limit the comments used
325325
to just the comments we want to allow for the current user.
326326

327-
<pre><code>
327+
<pre lang="ruby">
328328
class PostSerializer < ActiveModel::Serializer
329329
attributes :title. :body
330330
has_many :comments
@@ -344,7 +344,7 @@ private
344344
@scope.superuser?
345345
end
346346
end
347-
</code></pre>
347+
</pre>
348348

349349
+ActiveModel::Serializer+ will still embed the comments, but this time it will use just the comments
350350
for the current user.
@@ -359,7 +359,7 @@ build up the hash manually.
359359

360360
For example, let's say our front-end expects the posts and comments in the following format:
361361

362-
<pre><code>
362+
<pre lang="json">
363363
{
364364
post: {
365365
id: 1
@@ -380,11 +380,11 @@ For example, let's say our front-end expects the posts and comments in the follo
380380
}
381381
]
382382
}
383-
</code></pre>
383+
</pre>
384384

385385
We could achieve this with a custom +as_json+ method. We will also need to define a serializer for comments.
386386

387-
<pre><code>
387+
<pre lang="ruby">
388388
class CommentSerializer < ActiveModel::Serializer
389389
attributes :id, :title, :body
390390

@@ -420,7 +420,7 @@ private
420420
@scope.superuser?
421421
end
422422
end
423-
</code></pre>
423+
</pre>
424424

425425
Here, we used two convenience methods: +associations+ and +association_ids+. The first,
426426
+associations+, creates a hash of all of the define associations, using their defined
@@ -442,7 +442,7 @@ For instance, we might want to provide the full comment when it is requested dir
442442
but only its title when requested as part of the post. To achieve this, you can define
443443
a serializer for associated objects nested inside the main serializer.
444444

445-
<pre><code>
445+
<pre lang="ruby">
446446
class PostSerializer < ActiveModel::Serializer
447447
class CommentSerializer < ActiveModel::Serializer
448448
attributes :id, :title
@@ -451,7 +451,7 @@ class PostSerializer < ActiveModel::Serializer
451451
# same as before
452452
# ...
453453
end
454-
</code></pre>
454+
</pre>
455455

456456
In other words, if a +PostSerializer+ is trying to serialize comments, it will first
457457
look for +PostSerializer::CommentSerializer+ before falling back to +CommentSerializer+
@@ -468,11 +468,11 @@ its +current_user+ method and pass that along to the serializer's initializer.
468468
If you want to change that behavior, simply use the +serialization_scope+ class
469469
method.
470470

471-
<pre><code>
471+
<pre lang="ruby">
472472
class PostsController < ApplicationController
473473
serialization_scope :current_app
474474
end
475-
</code></pre>
475+
</pre>
476476

477477
You can also implement an instance method called (no surprise) +serialization_scope+,
478478
which allows you to define a dynamic authorization scope based on the current request.
@@ -489,19 +489,19 @@ outside a request.
489489
For instance, if you want to generate the JSON representation of a post for a user outside
490490
of a request:
491491

492-
<pre><code>
492+
<pre lang="ruby">
493493
user = get_user # some logic to get the user in question
494494
PostSerializer.new(post, user).to_json # reliably generate JSON output
495-
</code></pre>
495+
</pre>
496496

497497
If you want to generate JSON for an anonymous user, you should be able to use whatever
498498
technique you use in your application to generate anonymous users outside of a request.
499499
Typically, that means creating a new user and not saving it to the database:
500500

501-
<pre><code>
501+
<pre lang="ruby">
502502
user = User.new # create a new anonymous user
503503
PostSerializer.new(post, user).to_json
504-
</code></pre>
504+
</pre>
505505

506506
In general, the better you encapsulate your authorization logic, the more easily you
507507
will be able to use the serializer outside of the context of a request. For instance,
@@ -519,7 +519,7 @@ as the root).
519519

520520
For example, an Array of post objects would serialize as:
521521

522-
<pre><code>
522+
<pre lang="json">
523523
{
524524
posts: [
525525
{
@@ -531,12 +531,12 @@ For example, an Array of post objects would serialize as:
531531
}
532532
]
533533
}
534-
</code></pre>
534+
</pre>
535535

536536
If you want to change the behavior of serialized Arrays, you need to create
537537
a custom Array serializer.
538538

539-
<pre><code>
539+
<pre lang="ruby">
540540
class ArraySerializer < ActiveModel::ArraySerializer
541541
def serializable_array
542542
serializers.map do |serializer|
@@ -550,7 +550,7 @@ class ArraySerializer < ActiveModel::ArraySerializer
550550
hash
551551
end
552552
end
553-
</code></pre>
553+
</pre>
554554

555555
When generating embedded associations using the +associations+ helper inside a
556556
regular serializer, it will create a new <code>ArraySerializer</code> with the

0 commit comments

Comments
 (0)