forked from rails-api/active_model_serializers
/
serializer.rb
348 lines (297 loc) · 9.8 KB
/
serializer.rb
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/module/anonymous"
require "set"
module ActiveModel
# Active Model Array Serializer
#
# It serializes an array checking if each element that implements
# the +active_model_serializer+ method passing down the current scope.
class ArraySerializer
attr_reader :object, :scope
def initialize(object, scope)
@object, @scope = object, scope
end
def serializable_array
@object.map do |item|
if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
serializer.new(item, scope)
else
item
end
end
end
def as_json(*args)
serializable_array.as_json(*args)
end
end
# Active Model Serializer
#
# Provides a basic serializer implementation that allows you to easily
# control how a given object is going to be serialized. On initialization,
# it expects to object as arguments, a resource and a scope. For example,
# one may do in a controller:
#
# PostSerializer.new(@post, current_user).to_json
#
# The object to be serialized is the +@post+ and the scope is +current_user+.
#
# We use the scope to check if a given attribute should be serialized or not.
# For example, some attributes maybe only be returned if +current_user+ is the
# author of the post:
#
# class PostSerializer < ActiveModel::Serializer
# attributes :title, :body
# has_many :comments
#
# private
#
# def attributes
# hash = super
# hash.merge!(:email => post.email) if author?
# hash
# end
#
# def author?
# post.author == scope
# end
# end
#
class Serializer
module Associations #:nodoc:
class Config < Struct.new(:name, :options) #:nodoc:
def serializer
options[:serializer]
end
def key
options[:key] || name
end
def polymorphic?
options[:polymorphic]
end
end
class HasMany < Config #:nodoc:
def serialize(collection, scope)
collection.map do |item|
serializer.new(item, scope).serializable_hash
end
end
def serialize_ids(collection, scope)
# use named scopes if they are present
# return collection.ids if collection.respond_to?(:ids)
collection.map do |item|
item.read_attribute_for_serialization(:id)
end
end
end
class HasOne < Config #:nodoc:
def serialize(object, scope)
return unless object
if polymorphic?
polymorphic_type = object.class.to_s.split('::').last
serializer_class = "#{object.class.to_s}Serializer".constantize
serializer_class.new(object, scope).serializable_hash.merge({
"#{name}_type".to_sym => polymorphic_type
})
else
serializer.new(object, scope).serializable_hash
end
end
def serialize_ids(object, scope)
return unless object
if polymorphic?
{
:id => object.read_attribute_for_serialization(:id),
"#{name}_type".to_sym => object.class.to_s.split('::').last
}
else
object.read_attribute_for_serialization(:id)
end
end
end
end
class_attribute :_attributes
self._attributes = {}
class_attribute :_associations
self._associations = []
class_attribute :_root
class_attribute :_embed
self._embed = :objects
class_attribute :_root_embed
class << self
# Define attributes to be used in the serialization.
def attributes(*attrs)
self._attributes = _attributes.dup
attrs.each do |attr|
self._attributes[attr] = attr
end
end
def attribute(attr, options={})
self._attributes = _attributes.merge(attr => options[:key] || attr)
end
def associate(klass, attrs) #:nodoc:
options = attrs.extract_options!
self._associations += attrs.map do |attr|
unless method_defined?(attr)
class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__
end
options[:serializer] ||= options[:polymorphic] || begin
serializer_class = (options[:key] || attr).to_s.classify
const_get("#{serializer_class}Serializer")
end
klass.new(attr, options)
end
end
# Defines an association in the object should be rendered.
#
# The serializer object should implement the association name
# as a method which should return an array when invoked. If a method
# with the association name does not exist, the association name is
# dispatched to the serialized object.
def has_many(*attrs)
associate(Associations::HasMany, attrs)
end
# Defines an association in the object should be rendered.
#
# The serializer object should implement the association name
# as a method which should return an object when invoked. If a method
# with the association name does not exist, the association name is
# dispatched to the serialized object.
def has_one(*attrs)
associate(Associations::HasOne, attrs)
end
# Return a schema hash for the current serializer. This information
# can be used to generate clients for the serialized output.
#
# The schema hash has two keys: +attributes+ and +associations+.
#
# The +attributes+ hash looks like this:
#
# { :name => :string, :age => :integer }
#
# The +associations+ hash looks like this:
#
# { :posts => { :has_many => :posts } }
#
# If :key is used:
#
# class PostsSerializer < ActiveModel::Serializer
# has_many :posts, :key => :my_posts
# end
#
# the hash looks like this:
#
# { :my_posts => { :has_many => :posts }
#
# This information is extracted from the serializer's model class,
# which is provided by +SerializerClass.model_class+.
#
# The schema method uses the +columns_hash+ and +reflect_on_association+
# methods, provided by default by ActiveRecord. You can implement these
# methods on your custom models if you want the serializer's schema method
# to work.
def schema
klass = model_class
columns = klass.columns_hash
attrs = _attributes.inject({}) do |hash, (name,key)|
column = columns[name]
hash.merge key => column[:type]
end
associations = _associations.inject({}) do |hash, association|
model_association = klass.reflect_on_association(association.name)
hash.merge association.key => { model_association.macro => model_association.name }
end
{ :attributes => attrs, :associations => associations }
end
# The model class associated with this serializer.
def model_class
name.sub(/Serializer$/, '')
end
# Define how associations should be embedded.
#
# embed :objects # Embed associations as full objects
# embed :ids # Embed only the association ids
# embed :ids, :include => true # Embed the association ids and include objects in the root
#
def embed(type, options={})
self._embed = type
self._root_embed = true if options[:include]
end
# Defines the root used on serialization. If false, disables the root.
def root(name)
self._root = name
end
def inherited(klass) #:nodoc:
return if klass.anonymous?
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
klass.class_eval do
alias_method name.to_sym, :object
root name.to_sym unless self._root == false
end
end
end
attr_reader :object, :scope
def initialize(object, scope)
@object, @scope = object, scope
end
# Returns a json representation of the serializable
# object including the root.
def as_json(*)
if _root
hash = { _root => serializable_hash }
hash.merge!(associations) if _root_embed
hash
else
serializable_hash
end
end
# Returns a hash representation of the serializable
# object without the root.
def serializable_hash
if _embed == :ids
attributes.merge(association_ids)
elsif _embed == :objects
attributes.merge(associations)
else
attributes
end
end
# Returns a hash representation of the serializable
# object associations.
def associations
hash = {}
_associations.each do |association|
associated_object = send(association.name)
hash[association.key] = association.serialize(associated_object, scope)
if association.polymorphic? && associated_object
end
end
hash
end
# Returns a hash representation of the serializable
# object associations ids.
def association_ids
hash = {}
_associations.each do |association|
associated_object = send(association.name)
hash[association.key] = association.serialize_ids(associated_object, scope)
end
hash
end
# Returns a hash representation of the serializable
# object attributes.
def attributes
hash = {}
_attributes.each do |name,key|
hash[key] = @object.read_attribute_for_serialization(name)
end
hash
end
end
end
class Array
# Array uses ActiveModel::ArraySerializer.
def active_model_serializer
ActiveModel::ArraySerializer
end
end