Skip to content

Commit 696d01f

Browse files
committed
Add docs to serializers. Update CHANGELOGs.
1 parent 6d9f9b3 commit 696d01f

File tree

4 files changed

+114
-13
lines changed

4 files changed

+114
-13
lines changed

actionpack/lib/action_controller/metal/serialization.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
module ActionController
2+
# Action Controller Serialization
3+
#
4+
# Overrides render :json to check if the given object implements +active_model_serializer+
5+
# as a method. If so, use the returned serializer instead of calling +to_json+ in the object.
6+
#
7+
# This module also provides a serialization_scope method that allows you to configure the
8+
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
9+
# to the current user:
10+
#
11+
# class ApplicationController < ActionController::Base
12+
# serialization_scope :current_user
13+
# end
14+
#
15+
# If you need more complex scope rules, you can simply override the serialization_scope:
16+
#
17+
# class ApplicationController < ActionController::Base
18+
# private
19+
#
20+
# def serialization_scope
21+
# current_user
22+
# end
23+
# end
24+
#
225
module Serialization
326
extend ActiveSupport::Concern
427

activemodel/CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
* Added ActiveModel::Errors#added? to check if a specific error has been added *Martin Svalin*
1+
## Rails 3.2.0 (unreleased) ##
2+
3+
* Add ActiveModel::Serializer that encapsulates an ActiveModel object serialization *José Valim*
4+
5+
* Renamed (with a deprecation the following constants):
6+
7+
ActiveModel::Serialization => ActiveModel::Serializable
8+
ActiveModel::Serializers::JSON => ActiveModel::Serializable::JSON
9+
ActiveModel::Serializers::Xml => ActiveModel::Serializable::XML
10+
11+
*José Valim*
12+
13+
* Add ActiveModel::Errors#added? to check if a specific error has been added *Martin Svalin*
214

315
* Add ability to define strict validation(with :strict => true option) that always raises exception when fails *Bogdan Gusiev*
416

activemodel/lib/active_model/serializer.rb

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
module ActiveModel
77
# Active Model Array Serializer
8+
#
9+
# It serializes an array checking if each element that implements
10+
# the +active_model_serializer+ method passing down the current scope.
811
class ArraySerializer
912
attr_reader :object, :scope
1013

@@ -28,15 +31,46 @@ def as_json(*args)
2831
end
2932

3033
# Active Model Serializer
34+
#
35+
# Provides a basic serializer implementation that allows you to easily
36+
# control how a given object is going to be serialized. On initialization,
37+
# it expects to object as arguments, a resource and a scope. For example,
38+
# one may do in a controller:
39+
#
40+
# PostSerializer.new(@post, current_user).to_json
41+
#
42+
# The object to be serialized is the +@post+ and the scope is +current_user+.
43+
#
44+
# We use the scope to check if a given attribute should be serialized or not.
45+
# For example, some attributes maybe only be returned if +current_user+ is the
46+
# author of the post:
47+
#
48+
# class PostSerializer < ActiveModel::Serializer
49+
# attributes :title, :body
50+
# has_many :comments
51+
#
52+
# private
53+
#
54+
# def attributes
55+
# hash = super
56+
# hash.merge!(:email => post.email) if author?
57+
# hash
58+
# end
59+
#
60+
# def author?
61+
# post.author == scope
62+
# end
63+
# end
64+
#
3165
class Serializer
32-
module Associations
33-
class Config < Struct.new(:name, :options)
66+
module Associations #:nodoc:
67+
class Config < Struct.new(:name, :options) #:nodoc:
3468
def serializer
3569
options[:serializer]
3670
end
3771
end
3872

39-
class HasMany < Config
73+
class HasMany < Config #:nodoc:
4074
def serialize(collection, scope)
4175
collection.map do |item|
4276
serializer.new(item, scope).serializable_hash
@@ -45,15 +79,15 @@ def serialize(collection, scope)
4579

4680
def serialize_ids(collection, scope)
4781
# use named scopes if they are present
48-
#return collection.ids if collection.respond_to?(:ids)
82+
# return collection.ids if collection.respond_to?(:ids)
4983

5084
collection.map do |item|
5185
item.read_attribute_for_serialization(:id)
5286
end
5387
end
5488
end
5589

56-
class HasOne < Config
90+
class HasOne < Config #:nodoc:
5791
def serialize(object, scope)
5892
object && serializer.new(object, scope).serializable_hash
5993
end
@@ -76,11 +110,12 @@ def serialize_ids(object, scope)
76110
class_attribute :_root_embed
77111

78112
class << self
113+
# Define attributes to be used in the serialization.
79114
def attributes(*attrs)
80115
self._attributes += attrs
81116
end
82117

83-
def associate(klass, attrs)
118+
def associate(klass, attrs) #:nodoc:
84119
options = attrs.extract_options!
85120
self._associations += attrs.map do |attr|
86121
unless method_defined?(attr)
@@ -92,24 +127,43 @@ def associate(klass, attrs)
92127
end
93128
end
94129

130+
# Defines an association in the object should be rendered.
131+
#
132+
# The serializer object should implement the association name
133+
# as a method which should return an array when invoked. If a method
134+
# with the association name does not exist, the association name is
135+
# dispatched to the serialized object.
95136
def has_many(*attrs)
96137
associate(Associations::HasMany, attrs)
97138
end
98139

140+
# Defines an association in the object should be rendered.
141+
#
142+
# The serializer object should implement the association name
143+
# as a method which should return an object when invoked. If a method
144+
# with the association name does not exist, the association name is
145+
# dispatched to the serialized object.
99146
def has_one(*attrs)
100147
associate(Associations::HasOne, attrs)
101148
end
102149

150+
# Define how associations should be embedded.
151+
#
152+
# embed :objects # Embed associations as full objects
153+
# embed :ids # Embed only the association ids
154+
# embed :ids, :include => true # Embed the association ids and include objects in the root
155+
#
103156
def embed(type, options={})
104157
self._embed = type
105158
self._root_embed = true if options[:include]
106159
end
107160

161+
# Defines the root used on serialization. If false, disables the root.
108162
def root(name)
109163
self._root = name
110164
end
111165

112-
def inherited(klass)
166+
def inherited(klass) #:nodoc:
113167
return if klass.anonymous?
114168

115169
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
@@ -127,6 +181,8 @@ def initialize(object, scope)
127181
@object, @scope = object, scope
128182
end
129183

184+
# Returns a json representation of the serializable
185+
# object including the root.
130186
def as_json(*)
131187
if _root
132188
hash = { _root => serializable_hash }
@@ -137,6 +193,8 @@ def as_json(*)
137193
end
138194
end
139195

196+
# Returns a hash representation of the serializable
197+
# object without the root.
140198
def serializable_hash
141199
if _embed == :ids
142200
attributes.merge(association_ids)
@@ -147,6 +205,8 @@ def serializable_hash
147205
end
148206
end
149207

208+
# Returns a hash representation of the serializable
209+
# object associations.
150210
def associations
151211
hash = {}
152212

@@ -158,6 +218,8 @@ def associations
158218
hash
159219
end
160220

221+
# Returns a hash representation of the serializable
222+
# object associations ids.
161223
def association_ids
162224
hash = {}
163225

@@ -169,6 +231,8 @@ def association_ids
169231
hash
170232
end
171233

234+
# Returns a hash representation of the serializable
235+
# object attributes.
172236
def attributes
173237
hash = {}
174238

railties/CHANGELOG.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
## Rails 3.2.0 (unreleased) ##
22

3+
* Add a serializer generator and add a hook for it in the scaffold generators *José Valim*
4+
35
* Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box. *José Valim*
46

5-
* Updated Rails::Rack::Logger middleware to apply any tags set in config.log_tags to the newly ActiveSupport::TaggedLogging Rails.logger. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications *DHH*
7+
* Update Rails::Rack::Logger middleware to apply any tags set in config.log_tags to the newly ActiveSupport::TaggedLogging Rails.logger. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications *DHH*
68

79
* Default options to `rails new` can be set in ~/.railsrc *Guillermo Iguaran*
810

9-
* Added destroy alias to Rails engines. *Guillermo Iguaran*
11+
* Add destroy alias to Rails engines *Guillermo Iguaran*
1012

11-
* Added destroy alias for Rails command line. This allows the following: `rails d model post`. *Andrey Ognevsky*
13+
* Add destroy alias for Rails command line. This allows the following: `rails d model post` *Andrey Ognevsky*
1214

1315
* Attributes on scaffold and model generators default to string. This allows the following: "rails g scaffold Post title body:text author" *José Valim*
1416

15-
* Removed old plugin generator (`rails generate plugin`) in favor of `rails plugin new` command. *Guillermo Iguaran*
17+
* Remove old plugin generator (`rails generate plugin`) in favor of `rails plugin new` command *Guillermo Iguaran*
1618

17-
* Removed old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API. *Guillermo Iguaran*
19+
* Remove old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API *Guillermo Iguaran*
1820

1921

2022
* Rails 3.1.1

0 commit comments

Comments
 (0)