active_model_serializers 0.8.3 → 0.10.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/.rubocop.yml +86 -0
- data/.rubocop_todo.yml +240 -0
- data/.simplecov +111 -0
- data/.travis.yml +33 -22
- data/CHANGELOG.md +358 -6
- data/CONTRIBUTING.md +220 -0
- data/Gemfile +46 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +81 -591
- data/Rakefile +68 -11
- data/active_model_serializers.gemspec +57 -23
- data/appveyor.yml +27 -0
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +35 -0
- data/docs/general/adapters.md +162 -0
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +27 -0
- data/docs/general/getting_started.md +98 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +121 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +58 -0
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +140 -0
- data/lib/action_controller/serialization.rb +41 -37
- data/lib/active_model/serializable_resource.rb +72 -0
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +223 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +91 -0
- data/lib/active_model/serializer/array_serializer.rb +9 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +87 -220
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +28 -0
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +142 -0
- data/lib/active_model/serializer/reflection.rb +91 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +99 -479
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +20 -92
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +196 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
- data/test/action_controller/serialization_test.rb +435 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +38 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +92 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +97 -0
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +394 -0
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +71 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +36 -73
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +229 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +57 -0
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +40 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +166 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +267 -0
- data/test/serializers/attribute_test.rb +123 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/cache_test.rb +209 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +130 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +134 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +25 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +53 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +19 -0
- data/test/test_helper.rb +55 -24
- metadata +358 -42
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -1,515 +1,135 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
|
1
|
+
require 'thread_safe'
|
2
|
+
require 'active_model/serializer/collection_serializer'
|
3
|
+
require 'active_model/serializer/array_serializer'
|
4
|
+
require 'active_model/serializer/include_tree'
|
5
|
+
require 'active_model/serializer/associations'
|
6
|
+
require 'active_model/serializer/attributes'
|
7
|
+
require 'active_model/serializer/caching'
|
8
|
+
require 'active_model/serializer/configuration'
|
9
|
+
require 'active_model/serializer/fieldset'
|
10
|
+
require 'active_model/serializer/lint'
|
11
|
+
require 'active_model/serializer/links'
|
12
|
+
require 'active_model/serializer/type'
|
13
|
+
|
14
|
+
# ActiveModel::Serializer is an abstract class that is
|
15
|
+
# reified when subclassed to decorate a resource.
|
6
16
|
module ActiveModel
|
7
|
-
# Active Model Serializer
|
8
|
-
#
|
9
|
-
# Provides a basic serializer implementation that allows you to easily
|
10
|
-
# control how a given object is going to be serialized. On initialization,
|
11
|
-
# it expects two objects as arguments, a resource and options. For example,
|
12
|
-
# one may do in a controller:
|
13
|
-
#
|
14
|
-
# PostSerializer.new(@post, :scope => current_user).to_json
|
15
|
-
#
|
16
|
-
# The object to be serialized is the +@post+ and the current user is passed
|
17
|
-
# in for authorization purposes.
|
18
|
-
#
|
19
|
-
# We use the scope to check if a given attribute should be serialized or not.
|
20
|
-
# For example, some attributes may only be returned if +current_user+ is the
|
21
|
-
# author of the post:
|
22
|
-
#
|
23
|
-
# class PostSerializer < ActiveModel::Serializer
|
24
|
-
# attributes :title, :body
|
25
|
-
# has_many :comments
|
26
|
-
#
|
27
|
-
# private
|
28
|
-
#
|
29
|
-
# def attributes
|
30
|
-
# hash = super
|
31
|
-
# hash.merge!(:email => post.email) if author?
|
32
|
-
# hash
|
33
|
-
# end
|
34
|
-
#
|
35
|
-
# def author?
|
36
|
-
# post.author == scope
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
#
|
40
17
|
class Serializer
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
class_attribute :_root
|
65
|
-
class_attribute :_embed
|
66
|
-
self._embed = :objects
|
67
|
-
class_attribute :_root_embed
|
68
|
-
|
69
|
-
class_attribute :cache
|
70
|
-
class_attribute :perform_caching
|
71
|
-
|
72
|
-
class << self
|
73
|
-
# set perform caching like root
|
74
|
-
def cached(value = true)
|
75
|
-
self.perform_caching = value
|
76
|
-
end
|
77
|
-
|
78
|
-
# Define attributes to be used in the serialization.
|
79
|
-
def attributes(*attrs)
|
80
|
-
|
81
|
-
self._attributes = _attributes.dup
|
82
|
-
|
83
|
-
attrs.each do |attr|
|
84
|
-
if Hash === attr
|
85
|
-
attr.each {|attr_real, key| attribute attr_real, :key => key }
|
86
|
-
else
|
87
|
-
attribute attr
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def attribute(attr, options={})
|
93
|
-
self._attributes = _attributes.merge(attr.is_a?(Hash) ? attr : {attr => options[:key] || attr.to_s.gsub(/\?$/, '').to_sym})
|
94
|
-
|
95
|
-
attr = attr.keys[0] if attr.is_a? Hash
|
96
|
-
|
97
|
-
unless method_defined?(attr)
|
98
|
-
define_method attr do
|
99
|
-
object.read_attribute_for_serialization(attr.to_sym)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
define_include_method attr
|
104
|
-
|
105
|
-
# protect inheritance chains and open classes
|
106
|
-
# if a serializer inherits from another OR
|
107
|
-
# attributes are added later in a classes lifecycle
|
108
|
-
# poison the cache
|
109
|
-
define_method :_fast_attributes do
|
110
|
-
raise NameError
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
def associate(klass, attrs) #:nodoc:
|
116
|
-
options = attrs.extract_options!
|
117
|
-
self._associations = _associations.dup
|
118
|
-
|
119
|
-
attrs.each do |attr|
|
120
|
-
unless method_defined?(attr)
|
121
|
-
define_method attr do
|
122
|
-
object.send attr
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
define_include_method attr
|
127
|
-
|
128
|
-
self._associations[attr] = klass.refine(attr, options)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def define_include_method(name)
|
133
|
-
method = "include_#{name}?".to_sym
|
134
|
-
|
135
|
-
INCLUDE_METHODS[name] = method
|
136
|
-
|
137
|
-
unless method_defined?(method)
|
138
|
-
define_method method do
|
139
|
-
true
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# Defines an association in the object should be rendered.
|
145
|
-
#
|
146
|
-
# The serializer object should implement the association name
|
147
|
-
# as a method which should return an array when invoked. If a method
|
148
|
-
# with the association name does not exist, the association name is
|
149
|
-
# dispatched to the serialized object.
|
150
|
-
def has_many(*attrs)
|
151
|
-
associate(Associations::HasMany, attrs)
|
152
|
-
end
|
153
|
-
|
154
|
-
# Defines an association in the object should be rendered.
|
155
|
-
#
|
156
|
-
# The serializer object should implement the association name
|
157
|
-
# as a method which should return an object when invoked. If a method
|
158
|
-
# with the association name does not exist, the association name is
|
159
|
-
# dispatched to the serialized object.
|
160
|
-
def has_one(*attrs)
|
161
|
-
associate(Associations::HasOne, attrs)
|
162
|
-
end
|
163
|
-
|
164
|
-
# Return a schema hash for the current serializer. This information
|
165
|
-
# can be used to generate clients for the serialized output.
|
166
|
-
#
|
167
|
-
# The schema hash has two keys: +attributes+ and +associations+.
|
168
|
-
#
|
169
|
-
# The +attributes+ hash looks like this:
|
170
|
-
#
|
171
|
-
# { :name => :string, :age => :integer }
|
172
|
-
#
|
173
|
-
# The +associations+ hash looks like this:
|
174
|
-
# { :posts => { :has_many => :posts } }
|
175
|
-
#
|
176
|
-
# If :key is used:
|
177
|
-
#
|
178
|
-
# class PostsSerializer < ActiveModel::Serializer
|
179
|
-
# has_many :posts, :key => :my_posts
|
180
|
-
# end
|
181
|
-
#
|
182
|
-
# the hash looks like this:
|
183
|
-
#
|
184
|
-
# { :my_posts => { :has_many => :posts }
|
185
|
-
#
|
186
|
-
# This information is extracted from the serializer's model class,
|
187
|
-
# which is provided by +SerializerClass.model_class+.
|
188
|
-
#
|
189
|
-
# The schema method uses the +columns_hash+ and +reflect_on_association+
|
190
|
-
# methods, provided by default by ActiveRecord. You can implement these
|
191
|
-
# methods on your custom models if you want the serializer's schema method
|
192
|
-
# to work.
|
193
|
-
#
|
194
|
-
# TODO: This is currently coupled to Active Record. We need to
|
195
|
-
# figure out a way to decouple those two.
|
196
|
-
def schema
|
197
|
-
klass = model_class
|
198
|
-
columns = klass.columns_hash
|
199
|
-
|
200
|
-
attrs = {}
|
201
|
-
_attributes.each do |name, key|
|
202
|
-
if column = columns[name.to_s]
|
203
|
-
attrs[key] = column.type
|
204
|
-
else
|
205
|
-
# Computed attribute (method on serializer or model). We cannot
|
206
|
-
# infer the type, so we put nil, unless specified in the attribute declaration
|
207
|
-
if name != key
|
208
|
-
attrs[name] = key
|
209
|
-
else
|
210
|
-
attrs[key] = nil
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
associations = {}
|
216
|
-
_associations.each do |attr, association_class|
|
217
|
-
association = association_class.new(attr, self)
|
218
|
-
|
219
|
-
if model_association = klass.reflect_on_association(association.name)
|
220
|
-
# Real association.
|
221
|
-
associations[association.key] = { model_association.macro => model_association.name }
|
222
|
-
else
|
223
|
-
# Computed association. We could infer has_many vs. has_one from
|
224
|
-
# the association class, but that would make it different from
|
225
|
-
# real associations, which read has_one vs. belongs_to from the
|
226
|
-
# model.
|
227
|
-
associations[association.key] = nil
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
{ :attributes => attrs, :associations => associations }
|
232
|
-
end
|
233
|
-
|
234
|
-
# The model class associated with this serializer.
|
235
|
-
def model_class
|
236
|
-
name.sub(/Serializer$/, '').constantize
|
237
|
-
end
|
238
|
-
|
239
|
-
# Define how associations should be embedded.
|
240
|
-
#
|
241
|
-
# embed :objects # Embed associations as full objects
|
242
|
-
# embed :ids # Embed only the association ids
|
243
|
-
# embed :ids, :include => true # Embed the association ids and include objects in the root
|
244
|
-
#
|
245
|
-
def embed(type, options={})
|
246
|
-
self._embed = type
|
247
|
-
self._root_embed = true if options[:include]
|
248
|
-
end
|
249
|
-
|
250
|
-
# Defines the root used on serialization. If false, disables the root.
|
251
|
-
def root(name)
|
252
|
-
self._root = name
|
253
|
-
end
|
254
|
-
alias_method :root=, :root
|
255
|
-
|
256
|
-
# Used internally to create a new serializer object based on controller
|
257
|
-
# settings and options for a given resource. These settings are typically
|
258
|
-
# set during the request lifecycle or by the controller class, and should
|
259
|
-
# not be manually defined for this method.
|
260
|
-
def build_json(controller, resource, options)
|
261
|
-
default_options = controller.send(:default_serializer_options) || {}
|
262
|
-
options = default_options.merge(options || {})
|
263
|
-
|
264
|
-
serializer = options.delete(:serializer) ||
|
265
|
-
(resource.respond_to?(:active_model_serializer) &&
|
266
|
-
resource.active_model_serializer)
|
267
|
-
|
268
|
-
return serializer unless serializer
|
269
|
-
|
270
|
-
if resource.respond_to?(:to_ary)
|
271
|
-
unless serializer <= ActiveModel::ArraySerializer
|
272
|
-
raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " +
|
273
|
-
"You may want to use the :each_serializer option instead.")
|
274
|
-
end
|
275
|
-
|
276
|
-
if options[:root] != false && serializer.root != false
|
277
|
-
# the serializer for an Array is ActiveModel::ArraySerializer
|
278
|
-
options[:root] ||= serializer.root || controller.controller_name
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
options[:scope] = controller.serialization_scope unless options.has_key?(:scope)
|
283
|
-
options[:scope_name] = controller._serialization_scope
|
284
|
-
options[:url_options] = controller.url_options
|
285
|
-
|
286
|
-
serializer.new(resource, options)
|
18
|
+
include Configuration
|
19
|
+
include Associations
|
20
|
+
include Attributes
|
21
|
+
include Caching
|
22
|
+
include Links
|
23
|
+
include Type
|
24
|
+
require 'active_model/serializer/adapter'
|
25
|
+
|
26
|
+
# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
|
27
|
+
# @return [ActiveModel::Serializer]
|
28
|
+
# Preferentially returns
|
29
|
+
# 1. resource.serializer
|
30
|
+
# 2. ArraySerializer when resource is a collection
|
31
|
+
# 3. options[:serializer]
|
32
|
+
# 4. lookup serializer when resource is a Class
|
33
|
+
def self.serializer_for(resource, options = {})
|
34
|
+
if resource.respond_to?(:serializer_class)
|
35
|
+
resource.serializer_class
|
36
|
+
elsif resource.respond_to?(:to_ary)
|
37
|
+
config.collection_serializer
|
38
|
+
else
|
39
|
+
options.fetch(:serializer) { get_serializer_for(resource.class) }
|
287
40
|
end
|
288
41
|
end
|
289
42
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
@object, @options = object, options
|
294
|
-
|
295
|
-
scope_name = @options[:scope_name]
|
296
|
-
if scope_name && !respond_to?(scope_name)
|
297
|
-
self.class.class_eval do
|
298
|
-
define_method scope_name, lambda { scope }
|
299
|
-
end
|
300
|
-
end
|
43
|
+
# @see ActiveModel::Serializer::Adapter.lookup
|
44
|
+
def self.adapter
|
45
|
+
ActiveModel::Serializer::Adapter.lookup(config.adapter)
|
301
46
|
end
|
302
47
|
|
303
|
-
|
304
|
-
|
48
|
+
# @api private
|
49
|
+
def self.serializer_lookup_chain_for(klass)
|
50
|
+
chain = []
|
305
51
|
|
306
|
-
|
52
|
+
resource_class_name = klass.name.demodulize
|
53
|
+
resource_namespace = klass.name.deconstantize
|
54
|
+
serializer_class_name = "#{resource_class_name}Serializer"
|
307
55
|
|
308
|
-
if self
|
309
|
-
|
310
|
-
else
|
311
|
-
self._root || class_name
|
312
|
-
end
|
313
|
-
end
|
56
|
+
chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer
|
57
|
+
chain.push("#{resource_namespace}::#{serializer_class_name}")
|
314
58
|
|
315
|
-
|
316
|
-
@options[:url_options] || {}
|
59
|
+
chain
|
317
60
|
end
|
318
61
|
|
319
|
-
|
320
|
-
|
62
|
+
# Used to cache serializer name => serializer class
|
63
|
+
# when looked up by Serializer.get_serializer_for.
|
64
|
+
def self.serializers_cache
|
65
|
+
@serializers_cache ||= ThreadSafe::Cache.new
|
321
66
|
end
|
322
67
|
|
323
|
-
|
324
|
-
|
325
|
-
|
68
|
+
# @api private
|
69
|
+
# Find a serializer from a class and caches the lookup.
|
70
|
+
# Preferentially retuns:
|
71
|
+
# 1. class name appended with "Serializer"
|
72
|
+
# 2. try again with superclass, if present
|
73
|
+
# 3. nil
|
74
|
+
def self.get_serializer_for(klass)
|
75
|
+
return nil unless config.serializer_lookup_enabled
|
76
|
+
serializers_cache.fetch_or_store(klass) do
|
77
|
+
# NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
|
78
|
+
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
|
326
79
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
80
|
+
if serializer_class
|
81
|
+
serializer_class
|
82
|
+
elsif klass.superclass
|
83
|
+
get_serializer_for(klass.superclass)
|
331
84
|
end
|
332
|
-
else
|
333
|
-
super
|
334
85
|
end
|
335
86
|
end
|
336
87
|
|
337
|
-
|
338
|
-
|
339
|
-
def as_json(options={})
|
340
|
-
options ||= {}
|
341
|
-
if root = options.fetch(:root, @options.fetch(:root, root_name))
|
342
|
-
@options[:hash] = hash = {}
|
343
|
-
@options[:unique_values] = {}
|
344
|
-
|
345
|
-
hash.merge!(root => serializable_hash)
|
346
|
-
include_meta hash
|
347
|
-
hash
|
348
|
-
else
|
349
|
-
serializable_hash
|
350
|
-
end
|
88
|
+
def self._serializer_instance_method_defined?(name)
|
89
|
+
_serializer_instance_methods.include?(name)
|
351
90
|
end
|
352
91
|
|
353
|
-
|
354
|
-
|
355
|
-
def serializable_hash
|
356
|
-
if perform_caching?
|
357
|
-
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do
|
358
|
-
_serializable_hash
|
359
|
-
end
|
360
|
-
else
|
361
|
-
_serializable_hash
|
362
|
-
end
|
92
|
+
def self._serializer_instance_methods
|
93
|
+
@_serializer_instance_methods ||= (public_instance_methods - Object.public_instance_methods).to_set
|
363
94
|
end
|
95
|
+
private_class_method :_serializer_instance_methods
|
364
96
|
|
365
|
-
|
366
|
-
_associations.each_key do |name|
|
367
|
-
include!(name) if include?(name)
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
def include?(name)
|
372
|
-
return false if @options.key?(:only) && !Array(@options[:only]).include?(name)
|
373
|
-
return false if @options.key?(:except) && Array(@options[:except]).include?(name)
|
374
|
-
send INCLUDE_METHODS[name]
|
375
|
-
end
|
376
|
-
|
377
|
-
def include!(name, options={})
|
378
|
-
# Make sure that if a special options[:hash] was passed in, we generate
|
379
|
-
# a new unique values hash and don't clobber the original. If the hash
|
380
|
-
# passed in is the same as the current options hash, use the current
|
381
|
-
# unique values.
|
382
|
-
#
|
383
|
-
# TODO: Should passing in a Hash even be public API here?
|
384
|
-
unique_values =
|
385
|
-
if hash = options[:hash]
|
386
|
-
if @options[:hash] == hash
|
387
|
-
@options[:unique_values] ||= {}
|
388
|
-
else
|
389
|
-
{}
|
390
|
-
end
|
391
|
-
else
|
392
|
-
hash = @options[:hash]
|
393
|
-
@options[:unique_values] ||= {}
|
394
|
-
end
|
395
|
-
|
396
|
-
node = options[:node] ||= @node
|
397
|
-
value = options[:value]
|
398
|
-
|
399
|
-
if options[:include] == nil
|
400
|
-
if @options.key?(:include)
|
401
|
-
options[:include] = @options[:include].include?(name)
|
402
|
-
elsif @options.include?(:exclude)
|
403
|
-
options[:include] = !@options[:exclude].include?(name)
|
404
|
-
end
|
405
|
-
end
|
406
|
-
|
407
|
-
association_class =
|
408
|
-
if klass = _associations[name]
|
409
|
-
klass
|
410
|
-
elsif value.respond_to?(:to_ary)
|
411
|
-
Associations::HasMany
|
412
|
-
else
|
413
|
-
Associations::HasOne
|
414
|
-
end
|
415
|
-
|
416
|
-
association = association_class.new(name, self, options)
|
417
|
-
|
418
|
-
if association.embed_ids?
|
419
|
-
node[association.key] = association.serialize_ids
|
420
|
-
|
421
|
-
if association.embed_in_root? && hash.nil?
|
422
|
-
raise IncludeError.new(self.class, association.name)
|
423
|
-
elsif association.embed_in_root? && association.embeddable?
|
424
|
-
merge_association hash, association.root, association.serializables, unique_values
|
425
|
-
end
|
426
|
-
elsif association.embed_objects?
|
427
|
-
node[association.key] = association.serialize
|
428
|
-
end
|
429
|
-
end
|
97
|
+
attr_accessor :object, :root, :scope
|
430
98
|
|
431
|
-
#
|
432
|
-
#
|
433
|
-
#
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
# we want to merge a new list of values.
|
440
|
-
def merge_association(hash, key, serializables, unique_values)
|
441
|
-
already_serialized = (unique_values[key] ||= {})
|
442
|
-
serializable_hashes = (hash[key] ||= [])
|
99
|
+
# `scope_name` is set as :current_user by default in the controller.
|
100
|
+
# If the instance does not have a method named `scope_name`, it
|
101
|
+
# defines the method so that it calls the +scope+.
|
102
|
+
def initialize(object, options = {})
|
103
|
+
self.object = object
|
104
|
+
self.instance_options = options
|
105
|
+
self.root = instance_options[:root]
|
106
|
+
self.scope = instance_options[:scope]
|
443
107
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
108
|
+
scope_name = instance_options[:scope_name]
|
109
|
+
if scope_name && !respond_to?(scope_name)
|
110
|
+
self.class.class_eval do
|
111
|
+
define_method scope_name, lambda { scope }
|
448
112
|
end
|
449
113
|
end
|
450
114
|
end
|
451
115
|
|
452
|
-
#
|
453
|
-
|
454
|
-
|
455
|
-
_fast_attributes
|
456
|
-
rescue NameError
|
457
|
-
method = "def _fast_attributes\n"
|
458
|
-
|
459
|
-
method << " h = {}\n"
|
460
|
-
|
461
|
-
_attributes.each do |name,key|
|
462
|
-
method << " h[:\"#{key}\"] = read_attribute_for_serialization(:\"#{name}\") if include?(:\"#{name}\")\n"
|
463
|
-
end
|
464
|
-
method << " h\nend"
|
465
|
-
|
466
|
-
self.class.class_eval method
|
467
|
-
_fast_attributes
|
116
|
+
# Used by adapter as resource root.
|
117
|
+
def json_key
|
118
|
+
root || object.class.model_name.to_s.underscore
|
468
119
|
end
|
469
120
|
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
return nil if @object.nil?
|
479
|
-
@node = attributes
|
480
|
-
include_associations! if _embed
|
481
|
-
@node
|
482
|
-
end
|
483
|
-
|
484
|
-
def perform_caching?
|
485
|
-
perform_caching && cache && respond_to?(:cache_key)
|
486
|
-
end
|
487
|
-
|
488
|
-
def expand_cache_key(*args)
|
489
|
-
ActiveSupport::Cache.expand_cache_key(args)
|
490
|
-
end
|
491
|
-
|
492
|
-
# Use ActiveSupport::Notifications to send events to external systems.
|
493
|
-
# The event name is: name.class_name.serializer
|
494
|
-
def instrument(name, payload = {}, &block)
|
495
|
-
event_name = INSTRUMENT[name]
|
496
|
-
ActiveSupport::Notifications.instrument(event_name, payload, &block)
|
121
|
+
def read_attribute_for_serialization(attr)
|
122
|
+
if self.class._serializer_instance_method_defined?(attr)
|
123
|
+
send(attr)
|
124
|
+
elsif self.class._fragmented
|
125
|
+
self.class._fragmented.read_attribute_for_serialization(attr)
|
126
|
+
else
|
127
|
+
object.read_attribute_for_serialization(attr)
|
128
|
+
end
|
497
129
|
end
|
498
|
-
end
|
499
130
|
|
500
|
-
|
501
|
-
#
|
502
|
-
# Provides a constant interface for all items, particularly
|
503
|
-
# for ArraySerializer.
|
504
|
-
class DefaultSerializer
|
505
|
-
attr_reader :object, :options
|
131
|
+
protected
|
506
132
|
|
507
|
-
|
508
|
-
@object, @options = object, options
|
509
|
-
end
|
510
|
-
|
511
|
-
def serializable_hash
|
512
|
-
@object.as_json(@options)
|
513
|
-
end
|
133
|
+
attr_accessor :instance_options
|
514
134
|
end
|
515
135
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Adapted from
|
2
|
+
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
|
3
|
+
require 'active_support/callbacks'
|
4
|
+
|
5
|
+
module ActiveModelSerializers
|
6
|
+
# = ActiveModelSerializers Callbacks
|
7
|
+
#
|
8
|
+
# ActiveModelSerializers provides hooks during the life cycle of serialization and
|
9
|
+
# allow you to trigger logic. Available callbacks are:
|
10
|
+
#
|
11
|
+
# * <tt>around_render</tt>
|
12
|
+
#
|
13
|
+
module Callbacks
|
14
|
+
extend ActiveSupport::Concern
|
15
|
+
include ActiveSupport::Callbacks
|
16
|
+
|
17
|
+
included do
|
18
|
+
define_callbacks :render
|
19
|
+
end
|
20
|
+
|
21
|
+
# These methods will be included into any ActiveModelSerializers object, adding
|
22
|
+
# callbacks for +render+.
|
23
|
+
module ClassMethods
|
24
|
+
# Defines a callback that will get called around the render method,
|
25
|
+
# whether it is as_json, to_json, or serializable_hash
|
26
|
+
#
|
27
|
+
# class ActiveModel::SerializableResource
|
28
|
+
# include ActiveModelSerializers::Callbacks
|
29
|
+
#
|
30
|
+
# around_render do |args, block|
|
31
|
+
# tag_logger do
|
32
|
+
# notify_render do
|
33
|
+
# block.call(args)
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# def as_json
|
39
|
+
# run_callbacks :render do
|
40
|
+
# adapter.as_json
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
# # Note: So that we can re-use the instrumenter for as_json, to_json, and
|
44
|
+
# # serializable_hash, we aren't using the usual format, which would be:
|
45
|
+
# # def render(args)
|
46
|
+
# # adapter.as_json
|
47
|
+
# # end
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
def around_render(*filters, &blk)
|
51
|
+
set_callback(:render, :around, *filters, &blk)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActiveModelSerializers
|
2
|
+
module Deserialization
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def jsonapi_parse(*args)
|
6
|
+
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse(*args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def jsonapi_parse!(*args)
|
10
|
+
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(*args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|