active_model_serializers 0.8.3 → 0.10.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +82 -0
  4. data/.rubocop_todo.yml +315 -0
  5. data/.simplecov +99 -0
  6. data/.travis.yml +26 -20
  7. data/CHANGELOG.md +14 -67
  8. data/CONTRIBUTING.md +31 -0
  9. data/Gemfile +45 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +186 -488
  12. data/Rakefile +33 -12
  13. data/active_model_serializers.gemspec +49 -23
  14. data/appveyor.yml +25 -0
  15. data/docs/README.md +29 -0
  16. data/docs/general/adapters.md +110 -0
  17. data/docs/general/configuration_options.md +11 -0
  18. data/docs/general/getting_started.md +73 -0
  19. data/docs/howto/add_pagination_links.md +112 -0
  20. data/docs/howto/add_root_key.md +51 -0
  21. data/docs/howto/outside_controller_use.md +42 -0
  22. data/lib/action_controller/serialization.rb +31 -31
  23. data/lib/active_model/serializable_resource.rb +70 -0
  24. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  25. data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
  26. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
  27. data/lib/active_model/serializer/adapter/json.rb +47 -0
  28. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
  29. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
  30. data/lib/active_model/serializer/adapter/json_api.rb +158 -0
  31. data/lib/active_model/serializer/adapter/null.rb +5 -0
  32. data/lib/active_model/serializer/adapter.rb +159 -0
  33. data/lib/active_model/serializer/array_serializer.rb +40 -0
  34. data/lib/active_model/serializer/association.rb +20 -0
  35. data/lib/active_model/serializer/associations.rb +83 -219
  36. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  37. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  38. data/lib/active_model/serializer/configuration.rb +14 -0
  39. data/lib/active_model/serializer/fieldset.rb +40 -0
  40. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  41. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  42. data/lib/active_model/serializer/lint.rb +129 -0
  43. data/lib/active_model/serializer/railtie.rb +15 -0
  44. data/lib/active_model/serializer/reflection.rb +74 -0
  45. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  46. data/lib/active_model/serializer/utils.rb +35 -0
  47. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +121 -465
  49. data/lib/active_model_serializers.rb +26 -88
  50. data/lib/generators/serializer/USAGE +0 -3
  51. data/lib/generators/serializer/resource_override.rb +12 -0
  52. data/lib/generators/serializer/serializer_generator.rb +8 -13
  53. data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
  54. data/lib/tasks/rubocop.rake +0 -0
  55. data/test/action_controller/adapter_selector_test.rb +53 -0
  56. data/test/action_controller/explicit_serializer_test.rb +134 -0
  57. data/test/action_controller/json_api/linked_test.rb +179 -0
  58. data/test/action_controller/json_api/pagination_test.rb +116 -0
  59. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
  60. data/test/action_controller/serialization_test.rb +420 -0
  61. data/test/active_record_test.rb +9 -0
  62. data/test/adapter/fragment_cache_test.rb +37 -0
  63. data/test/adapter/json/belongs_to_test.rb +47 -0
  64. data/test/adapter/json/collection_test.rb +82 -0
  65. data/test/adapter/json/has_many_test.rb +47 -0
  66. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  67. data/test/adapter/json_api/collection_test.rb +95 -0
  68. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  69. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  70. data/test/adapter/json_api/has_many_test.rb +145 -0
  71. data/test/adapter/json_api/has_one_test.rb +81 -0
  72. data/test/adapter/json_api/json_api_test.rb +37 -0
  73. data/test/adapter/json_api/linked_test.rb +283 -0
  74. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  75. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  76. data/test/adapter/json_test.rb +47 -0
  77. data/test/adapter/null_test.rb +25 -0
  78. data/test/adapter_test.rb +42 -0
  79. data/test/array_serializer_test.rb +99 -73
  80. data/test/capture_warnings.rb +65 -0
  81. data/test/fixtures/active_record.rb +56 -0
  82. data/test/fixtures/poro.rb +261 -0
  83. data/test/generators/scaffold_controller_generator_test.rb +23 -0
  84. data/test/generators/serializer_generator_test.rb +56 -0
  85. data/test/lint_test.rb +37 -0
  86. data/test/logger_test.rb +18 -0
  87. data/test/poro_test.rb +9 -0
  88. data/test/serializable_resource_test.rb +27 -0
  89. data/test/serializers/adapter_for_test.rb +170 -0
  90. data/test/serializers/association_macros_test.rb +36 -0
  91. data/test/serializers/associations_test.rb +150 -0
  92. data/test/serializers/attribute_test.rb +62 -0
  93. data/test/serializers/attributes_test.rb +57 -0
  94. data/test/serializers/cache_test.rb +165 -0
  95. data/test/serializers/configuration_test.rb +15 -0
  96. data/test/serializers/fieldset_test.rb +25 -0
  97. data/test/serializers/meta_test.rb +121 -0
  98. data/test/serializers/options_test.rb +21 -0
  99. data/test/serializers/root_test.rb +21 -0
  100. data/test/serializers/serializer_for_test.rb +65 -0
  101. data/test/support/rails_app.rb +21 -0
  102. data/test/support/serialization_testing.rb +13 -0
  103. data/test/support/simplecov.rb +6 -0
  104. data/test/support/stream_capture.rb +50 -0
  105. data/test/support/test_case.rb +5 -0
  106. data/test/test_helper.rb +48 -24
  107. data/test/utils/include_args_to_hash_test.rb +79 -0
  108. metadata +219 -47
  109. data/DESIGN.textile +0 -586
  110. data/Gemfile.edge +0 -9
  111. data/bench/perf.rb +0 -43
  112. data/cruft.md +0 -19
  113. data/lib/active_model/array_serializer.rb +0 -104
  114. data/lib/active_record/serializer_override.rb +0 -16
  115. data/lib/generators/resource_override.rb +0 -13
  116. data/lib/generators/serializer/templates/serializer.rb +0 -19
  117. data/test/association_test.rb +0 -592
  118. data/test/caching_test.rb +0 -96
  119. data/test/generators_test.rb +0 -85
  120. data/test/no_serialization_scope_test.rb +0 -34
  121. data/test/serialization_test.rb +0 -392
  122. data/test/serializer_support_test.rb +0 -51
  123. data/test/serializer_test.rb +0 -1465
  124. data/test/test_fakes.rb +0 -217
@@ -1,515 +1,171 @@
1
- require "active_support/core_ext/class/attribute"
2
- require "active_support/core_ext/module/anonymous"
3
- require 'active_support/dependencies'
4
- require 'active_support/descendants_tracker'
1
+ require 'thread_safe'
5
2
 
6
3
  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
4
  class Serializer
41
- extend ActiveSupport::DescendantsTracker
42
-
43
- INCLUDE_METHODS = {}
44
- INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
45
-
46
- class IncludeError < StandardError
47
- attr_reader :source, :association
48
-
49
- def initialize(source, association)
50
- @source, @association = source, association
51
- end
52
-
53
- def to_s
54
- "Cannot serialize #{association} when #{source} does not have a root!"
55
- end
56
- end
57
-
58
- class_attribute :_attributes
59
- self._attributes = {}
60
-
61
- class_attribute :_associations
62
- self._associations = {}
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
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Configuration
8
+ autoload :ArraySerializer
9
+ autoload :Adapter
10
+ autoload :Lint
11
+ autoload :Associations
12
+ autoload :Fieldset
13
+ autoload :Utils
14
+ include Configuration
15
+ include Associations
16
+
17
+ # Matches
18
+ # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
19
+ # AND
20
+ # "/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
21
+ # AS
22
+ # c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb
23
+ CALLER_FILE = /
24
+ \A # start of string
25
+ \S+ # one or more non-spaces
26
+ (?= # stop previous match when
27
+ :\d+ # a colon is followed by one or more digits
28
+ :in # followed by a colon followed by in
29
+ )
30
+ /x
71
31
 
72
32
  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)
287
- end
33
+ attr_accessor :_attributes
34
+ attr_accessor :_attributes_keys
35
+ attr_accessor :_cache
36
+ attr_accessor :_fragmented
37
+ attr_accessor :_cache_key
38
+ attr_accessor :_cache_only
39
+ attr_accessor :_cache_except
40
+ attr_accessor :_cache_options
41
+ attr_accessor :_cache_digest
288
42
  end
289
43
 
290
- attr_reader :object, :options
44
+ def self.inherited(base)
45
+ base._attributes = self._attributes.try(:dup) || []
46
+ base._attributes_keys = self._attributes_keys.try(:dup) || {}
47
+ base._cache_digest = digest_caller_file(caller.first)
48
+ super
49
+ end
291
50
 
292
- def initialize(object, options={})
293
- @object, @options = object, options
51
+ def self.attributes(*attrs)
52
+ attrs = attrs.first if attrs.first.class == Array
53
+ @_attributes.concat attrs
54
+ @_attributes.uniq!
294
55
 
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
56
+ attrs.each do |attr|
57
+ define_method attr do
58
+ object && object.read_attribute_for_serialization(attr)
59
+ end unless method_defined?(attr) || _fragmented.respond_to?(attr)
300
60
  end
301
61
  end
302
62
 
303
- def root_name
304
- return false if self._root == false
305
-
306
- class_name = self.class.name.demodulize.underscore.sub(/_serializer$/, '').to_sym unless self.class.name.blank?
63
+ def self.attribute(attr, options = {})
64
+ key = options.fetch(:key, attr)
65
+ @_attributes_keys[attr] = { key: key } if key != attr
66
+ @_attributes << key unless @_attributes.include?(key)
307
67
 
308
- if self._root == true
309
- class_name
310
- else
311
- self._root || class_name
68
+ ActiveModelSerializers.silence_warnings do
69
+ define_method key do
70
+ object.read_attribute_for_serialization(attr)
71
+ end unless (key != :id && method_defined?(key)) || _fragmented.respond_to?(attr)
312
72
  end
313
73
  end
314
74
 
315
- def url_options
316
- @options[:url_options] || {}
317
- end
318
-
319
- def meta_key
320
- @options[:meta_key].try(:to_sym) || :meta
75
+ def self.fragmented(serializer)
76
+ @_fragmented = serializer
321
77
  end
322
78
 
323
- def include_meta(hash)
324
- hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
79
+ # Enables a serializer to be automatically cached
80
+ def self.cache(options = {})
81
+ @_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
82
+ @_cache_key = options.delete(:key)
83
+ @_cache_only = options.delete(:only)
84
+ @_cache_except = options.delete(:except)
85
+ @_cache_options = (options.empty?) ? nil : options
325
86
  end
326
87
 
327
- def to_json(*args)
328
- if perform_caching?
329
- cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
330
- super
331
- end
88
+ def self.serializer_for(resource, options = {})
89
+ if resource.respond_to?(:serializer_class)
90
+ resource.serializer_class
91
+ elsif resource.respond_to?(:to_ary)
92
+ config.array_serializer
332
93
  else
333
- super
94
+ options.fetch(:serializer, get_serializer_for(resource.class))
334
95
  end
335
96
  end
336
97
 
337
- # Returns a json representation of the serializable
338
- # object including the root.
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
98
+ # @see ActiveModel::Serializer::Adapter.lookup
99
+ def self.adapter
100
+ ActiveModel::Serializer::Adapter.lookup(config.adapter)
351
101
  end
352
102
 
353
- # Returns a hash representation of the serializable
354
- # object without the root.
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
103
+ def self.root_name
104
+ name.demodulize.underscore.sub(/_serializer$/, '') if name
363
105
  end
364
106
 
365
- def include_associations!
366
- _associations.each_key do |name|
367
- include!(name) if include?(name)
107
+ attr_accessor :object, :root, :meta, :meta_key, :scope
108
+
109
+ def initialize(object, options = {})
110
+ @object = object
111
+ @options = options
112
+ @root = options[:root]
113
+ @meta = options[:meta]
114
+ @meta_key = options[:meta_key]
115
+ @scope = options[:scope]
116
+
117
+ scope_name = options[:scope_name]
118
+ if scope_name && !respond_to?(scope_name)
119
+ self.class.class_eval do
120
+ define_method scope_name, lambda { scope }
121
+ end
368
122
  end
369
123
  end
370
124
 
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]
125
+ def json_key
126
+ @root || object.class.model_name.to_s.underscore
375
127
  end
376
128
 
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
129
+ def attributes(options = {})
130
+ attributes =
131
+ if options[:fields]
132
+ self.class._attributes & options[:fields]
391
133
  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)
134
+ self.class._attributes.dup
404
135
  end
405
- end
406
136
 
407
- association_class =
408
- if klass = _associations[name]
409
- klass
410
- elsif value.respond_to?(:to_ary)
411
- Associations::HasMany
137
+ attributes.each_with_object({}) do |name, hash|
138
+ unless self.class._fragmented
139
+ hash[name] = send(name)
412
140
  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
141
+ hash[name] = self.class._fragmented.public_send(name)
425
142
  end
426
- elsif association.embed_objects?
427
- node[association.key] = association.serialize
428
143
  end
429
144
  end
430
145
 
431
- # In some cases, an Array of associations is built by merging the associated
432
- # content for all of the children. For instance, if a Post has_many comments,
433
- # which has_many tags, the top-level :tags key will contain the merged list
434
- # of all tags for all comments of the post.
435
- #
436
- # In order to make this efficient, we store a :unique_values hash containing
437
- # a unique list of all of the objects that are already in the Array. This
438
- # avoids the need to scan through the Array looking for entries every time
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] ||= [])
443
-
444
- serializables.each do |serializable|
445
- unless already_serialized.include? serializable.object
446
- already_serialized[serializable.object] = true
447
- serializable_hashes << serializable.serializable_hash
448
- end
449
- end
146
+ def self.serializers_cache
147
+ @serializers_cache ||= ThreadSafe::Cache.new
450
148
  end
451
149
 
452
- # Returns a hash representation of the serializable
453
- # object attributes.
454
- def attributes
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
150
+ def self.digest_caller_file(caller_line)
151
+ serializer_file_path = caller_line[CALLER_FILE]
152
+ serializer_file_contents = IO.read(serializer_file_path)
153
+ Digest::MD5.hexdigest(serializer_file_contents)
468
154
  end
469
155
 
470
- # Returns options[:scope]
471
- def scope
472
- @options[:scope]
473
- end
156
+ attr_reader :options
474
157
 
475
- alias :read_attribute_for_serialization :send
158
+ def self.get_serializer_for(klass)
159
+ serializers_cache.fetch_or_store(klass) do
160
+ serializer_class_name = "#{klass.name}Serializer"
161
+ serializer_class = serializer_class_name.safe_constantize
476
162
 
477
- def _serializable_hash
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)
497
- end
498
- end
499
-
500
- # DefaultSerializer
501
- #
502
- # Provides a constant interface for all items, particularly
503
- # for ArraySerializer.
504
- class DefaultSerializer
505
- attr_reader :object, :options
506
-
507
- def initialize(object, options={})
508
- @object, @options = object, options
509
- end
510
-
511
- def serializable_hash
512
- @object.as_json(@options)
163
+ if serializer_class
164
+ serializer_class
165
+ elsif klass.superclass
166
+ get_serializer_for(klass.superclass)
167
+ end
168
+ end
513
169
  end
514
170
  end
515
171
  end