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
|
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/module/anonymous"
require "active_support/core_ext/module/introspection"
require "set"
module ActiveModel
# Active Model Array Serializer
class ArraySerializer
attr_reader :object, :scope
def initialize(object, scope)
@object, @scope = object, scope
end
def serializable_array
@object.map do |item|
if serializer = Serializer::Finder.find(item, scope)
serializer.new(item, scope)
else
item
end
end
end
def as_json(*args)
serializable_array.as_json(*args)
end
end
# Active Model Serializer
class Serializer
module Finder
mattr_accessor :constantizer
@@constantizer = ActiveSupport::Inflector
# Finds a serializer for the given object in the given scope.
# If the object implements a +model_serializer+ method, it does
# not do a scope lookup but uses the model_serializer method instead.
def self.find(object, scope)
if object.respond_to?(:model_serializer)
object.model_serializer
else
scope = scope.class unless scope.respond_to?(:const_defined?)
object = object.class unless object.respond_to?(:name)
serializer = "#{object.name.demodulize}Serializer"
begin
scope.const_get serializer
rescue NameError => e
raise unless e.message =~ /uninitialized constant ([\w_]+::)*#{serializer}$/
scope.parents.each do |parent|
return parent.const_get(serializer) if parent.const_defined?(serializer)
end
nil
end
end
end
end
# Defines the serialization scope. Core extension serializers
# are defined in this module so a scoped lookup is able to find
# core extension serializers.
module Scope
ArraySerializer = ::ActiveModel::ArraySerializer
end
module Associations
class Config < Struct.new(:name, :options)
def serializer
options[:serializer]
end
end
class HasMany < Config
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
def serialize(object, scope)
object && serializer.new(object, scope).serializable_hash
end
def serialize_ids(object, scope)
object && object.read_attribute_for_serialization(:id)
end
end
end
class_attribute :_attributes
self._attributes = Set.new
class_attribute :_associations
self._associations = []
class_attribute :_root
class_attribute :_embed
self._embed = :objects
class_attribute :_root_embed
class << self
def attributes(*attrs)
self._attributes += attrs
end
def associate(klass, attrs)
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] ||= const_get("#{attr.to_s.camelize}Serializer")
klass.new(attr, options)
end
end
def has_many(*attrs)
associate(Associations::HasMany, attrs)
end
def has_one(*attrs)
associate(Associations::HasOne, attrs)
end
def embed(type, options={})
self._embed = type
self._root_embed = true if options[:include]
end
def root(name)
self._root = name
end
def inherited(klass)
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
def as_json(*)
if _root
hash = { _root => serializable_hash }
hash.merge!(associations) if _root_embed
hash
else
serializable_hash
end
end
def serializable_hash
if _embed == :ids
attributes.merge(association_ids)
elsif _embed == :objects
attributes.merge(associations)
else
attributes
end
end
def associations
hash = {}
_associations.each do |association|
associated_object = send(association.name)
hash[association.name] = association.serialize(associated_object, scope)
end
hash
end
def association_ids
hash = {}
_associations.each do |association|
associated_object = send(association.name)
hash[association.name] = association.serialize_ids(associated_object, scope)
end
hash
end
def attributes
hash = {}
_attributes.each do |name|
hash[name] = @object.read_attribute_for_serialization(name)
end
hash
end
end
end
|