active_model_serializers 0.10.0 → 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -5
  3. data/.travis.yml +17 -5
  4. data/CHANGELOG.md +126 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/README.md +166 -26
  8. data/Rakefile +3 -32
  9. data/active_model_serializers.gemspec +22 -25
  10. data/appveyor.yml +9 -3
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +29 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +70 -1
  16. data/docs/general/deserialization.md +1 -1
  17. data/docs/general/fields.md +31 -0
  18. data/docs/general/getting_started.md +1 -1
  19. data/docs/general/logging.md +7 -0
  20. data/docs/general/rendering.md +62 -24
  21. data/docs/general/serializers.md +121 -13
  22. data/docs/howto/add_pagination_links.md +16 -17
  23. data/docs/howto/add_relationship_links.md +140 -0
  24. data/docs/howto/add_root_key.md +4 -0
  25. data/docs/howto/grape_integration.md +42 -0
  26. data/docs/howto/outside_controller_use.md +12 -4
  27. data/docs/howto/passing_arbitrary_options.md +2 -2
  28. data/docs/howto/serialize_poro.md +46 -5
  29. data/docs/howto/test.md +2 -0
  30. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  31. data/docs/integrations/ember-and-json-api.md +67 -32
  32. data/docs/jsonapi/schema.md +1 -1
  33. data/lib/action_controller/serialization.rb +13 -3
  34. data/lib/active_model/serializer/adapter/base.rb +2 -0
  35. data/lib/active_model/serializer/array_serializer.rb +8 -5
  36. data/lib/active_model/serializer/association.rb +62 -10
  37. data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
  38. data/lib/active_model/serializer/collection_serializer.rb +35 -12
  39. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
  40. data/lib/active_model/serializer/error_serializer.rb +11 -7
  41. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  42. data/lib/active_model/serializer/has_many_reflection.rb +3 -3
  43. data/lib/active_model/serializer/has_one_reflection.rb +1 -4
  44. data/lib/active_model/serializer/lazy_association.rb +95 -0
  45. data/lib/active_model/serializer/lint.rb +134 -130
  46. data/lib/active_model/serializer/reflection.rb +127 -67
  47. data/lib/active_model/serializer/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +296 -79
  49. data/lib/active_model_serializers/adapter/attributes.rb +3 -66
  50. data/lib/active_model_serializers/adapter/base.rb +39 -39
  51. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
  52. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  53. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  54. data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -23
  55. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +32 -9
  56. data/lib/active_model_serializers/adapter/json_api.rb +71 -57
  57. data/lib/active_model_serializers/adapter.rb +6 -0
  58. data/lib/active_model_serializers/deprecate.rb +1 -2
  59. data/lib/active_model_serializers/deserialization.rb +2 -0
  60. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  61. data/lib/active_model_serializers/model.rb +109 -28
  62. data/lib/active_model_serializers/railtie.rb +3 -1
  63. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  64. data/lib/active_model_serializers/serializable_resource.rb +6 -5
  65. data/lib/active_model_serializers/serialization_context.rb +10 -3
  66. data/lib/active_model_serializers/test/schema.rb +2 -2
  67. data/lib/active_model_serializers.rb +15 -0
  68. data/lib/generators/rails/resource_override.rb +1 -1
  69. data/lib/generators/rails/serializer_generator.rb +4 -4
  70. data/lib/grape/active_model_serializers.rb +7 -5
  71. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  72. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  73. data/lib/tasks/rubocop.rake +53 -0
  74. data/test/action_controller/adapter_selector_test.rb +14 -5
  75. data/test/action_controller/explicit_serializer_test.rb +5 -4
  76. data/test/action_controller/json/include_test.rb +106 -27
  77. data/test/action_controller/json_api/errors_test.rb +8 -9
  78. data/test/action_controller/json_api/fields_test.rb +66 -0
  79. data/test/action_controller/json_api/linked_test.rb +29 -24
  80. data/test/action_controller/json_api/pagination_test.rb +19 -19
  81. data/test/action_controller/json_api/transform_test.rb +11 -3
  82. data/test/action_controller/lookup_proc_test.rb +49 -0
  83. data/test/action_controller/namespace_lookup_test.rb +232 -0
  84. data/test/action_controller/serialization_scope_name_test.rb +12 -6
  85. data/test/action_controller/serialization_test.rb +12 -9
  86. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  87. data/test/active_model_serializers/model_test.rb +137 -4
  88. data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
  89. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
  90. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  91. data/test/active_model_serializers/test/schema_test.rb +3 -2
  92. data/test/adapter/attributes_test.rb +40 -0
  93. data/test/adapter/json/collection_test.rb +14 -0
  94. data/test/adapter/json/has_many_test.rb +10 -2
  95. data/test/adapter/json/transform_test.rb +15 -15
  96. data/test/adapter/json_api/collection_test.rb +4 -3
  97. data/test/adapter/json_api/errors_test.rb +17 -19
  98. data/test/adapter/json_api/fields_test.rb +12 -3
  99. data/test/adapter/json_api/has_many_test.rb +49 -20
  100. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
  101. data/test/adapter/json_api/json_api_test.rb +5 -7
  102. data/test/adapter/json_api/linked_test.rb +33 -12
  103. data/test/adapter/json_api/links_test.rb +4 -2
  104. data/test/adapter/json_api/pagination_links_test.rb +35 -8
  105. data/test/adapter/json_api/relationship_test.rb +309 -73
  106. data/test/adapter/json_api/resource_identifier_test.rb +27 -2
  107. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  108. data/test/adapter/json_api/transform_test.rb +263 -253
  109. data/test/adapter/json_api/type_test.rb +1 -1
  110. data/test/adapter/json_test.rb +8 -7
  111. data/test/adapter/null_test.rb +1 -2
  112. data/test/adapter/polymorphic_test.rb +5 -5
  113. data/test/adapter_test.rb +1 -1
  114. data/test/benchmark/app.rb +1 -1
  115. data/test/benchmark/benchmarking_support.rb +1 -1
  116. data/test/benchmark/bm_active_record.rb +81 -0
  117. data/test/benchmark/bm_adapter.rb +38 -0
  118. data/test/benchmark/bm_caching.rb +16 -16
  119. data/test/benchmark/bm_lookup_chain.rb +83 -0
  120. data/test/benchmark/bm_transform.rb +21 -10
  121. data/test/benchmark/controllers.rb +16 -17
  122. data/test/benchmark/fixtures.rb +72 -72
  123. data/test/cache_test.rb +235 -69
  124. data/test/collection_serializer_test.rb +25 -12
  125. data/test/fixtures/active_record.rb +45 -10
  126. data/test/fixtures/poro.rb +124 -181
  127. data/test/generators/serializer_generator_test.rb +23 -5
  128. data/test/grape_test.rb +170 -56
  129. data/test/lint_test.rb +1 -1
  130. data/test/logger_test.rb +13 -11
  131. data/test/serializable_resource_test.rb +18 -22
  132. data/test/serializers/association_macros_test.rb +3 -2
  133. data/test/serializers/associations_test.rb +178 -49
  134. data/test/serializers/attribute_test.rb +5 -3
  135. data/test/serializers/attributes_test.rb +1 -1
  136. data/test/serializers/caching_configuration_test_isolated.rb +6 -6
  137. data/test/serializers/fieldset_test.rb +1 -1
  138. data/test/serializers/meta_test.rb +12 -6
  139. data/test/serializers/options_test.rb +17 -6
  140. data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
  141. data/test/serializers/reflection_test.rb +427 -0
  142. data/test/serializers/root_test.rb +1 -1
  143. data/test/serializers/serialization_test.rb +2 -2
  144. data/test/serializers/serializer_for_test.rb +12 -10
  145. data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
  146. data/test/support/isolated_unit.rb +5 -2
  147. data/test/support/rails5_shims.rb +8 -2
  148. data/test/support/rails_app.rb +2 -9
  149. data/test/support/serialization_testing.rb +23 -5
  150. data/test/test_helper.rb +13 -0
  151. metadata +105 -42
  152. data/.rubocop_todo.yml +0 -167
  153. data/docs/ARCHITECTURE.md +0 -126
  154. data/lib/active_model/serializer/associations.rb +0 -100
  155. data/lib/active_model/serializer/attributes.rb +0 -82
  156. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  157. data/lib/active_model/serializer/configuration.rb +0 -35
  158. data/lib/active_model/serializer/include_tree.rb +0 -111
  159. data/lib/active_model/serializer/links.rb +0 -35
  160. data/lib/active_model/serializer/meta.rb +0 -29
  161. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  162. data/lib/active_model/serializer/type.rb +0 -25
  163. data/lib/active_model_serializers/key_transform.rb +0 -70
  164. data/test/active_model_serializers/key_transform_test.rb +0 -263
  165. data/test/adapter/json_api/relationships_test.rb +0 -199
  166. data/test/include_tree/from_include_args_test.rb +0 -26
  167. data/test/include_tree/from_string_test.rb +0 -94
  168. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,50 +1,51 @@
1
1
  require 'thread_safe'
2
+ require 'jsonapi/include_directive'
2
3
  require 'active_model/serializer/collection_serializer'
3
4
  require 'active_model/serializer/array_serializer'
4
5
  require 'active_model/serializer/error_serializer'
5
6
  require 'active_model/serializer/errors_serializer'
6
- require 'active_model/serializer/include_tree'
7
- require 'active_model/serializer/associations'
8
- require 'active_model/serializer/attributes'
9
- require 'active_model/serializer/caching'
10
- require 'active_model/serializer/configuration'
7
+ require 'active_model/serializer/concerns/caching'
11
8
  require 'active_model/serializer/fieldset'
12
9
  require 'active_model/serializer/lint'
13
- require 'active_model/serializer/links'
14
- require 'active_model/serializer/meta'
15
- require 'active_model/serializer/type'
16
10
 
17
11
  # ActiveModel::Serializer is an abstract class that is
18
12
  # reified when subclassed to decorate a resource.
19
13
  module ActiveModel
20
14
  class Serializer
15
+ undef_method :select, :display # These IO methods, which are mixed into Kernel,
16
+ # sometimes conflict with attribute names. We don't need these IO methods.
17
+
21
18
  # @see #serializable_hash for more details on these valid keys.
22
19
  SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
23
20
  extend ActiveSupport::Autoload
24
21
  autoload :Adapter
25
22
  autoload :Null
26
- include Configuration
27
- include Associations
28
- include Attributes
23
+ autoload :Attribute
24
+ autoload :Association
25
+ autoload :Reflection
26
+ autoload :SingularReflection
27
+ autoload :CollectionReflection
28
+ autoload :BelongsToReflection
29
+ autoload :HasOneReflection
30
+ autoload :HasManyReflection
31
+ include ActiveSupport::Configurable
29
32
  include Caching
30
- include Links
31
- include Meta
32
- include Type
33
33
 
34
34
  # @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
35
35
  # @return [ActiveModel::Serializer]
36
36
  # Preferentially returns
37
- # 1. resource.serializer
37
+ # 1. resource.serializer_class
38
38
  # 2. ArraySerializer when resource is a collection
39
39
  # 3. options[:serializer]
40
40
  # 4. lookup serializer when resource is a Class
41
- def self.serializer_for(resource, options = {})
42
- if resource.respond_to?(:serializer_class)
43
- resource.serializer_class
44
- elsif resource.respond_to?(:to_ary)
41
+ def self.serializer_for(resource_or_class, options = {})
42
+ if resource_or_class.respond_to?(:serializer_class)
43
+ resource_or_class.serializer_class
44
+ elsif resource_or_class.respond_to?(:to_ary)
45
45
  config.collection_serializer
46
46
  else
47
- options.fetch(:serializer) { get_serializer_for(resource.class) }
47
+ resource_class = resource_or_class.class == Class ? resource_or_class : resource_or_class.class
48
+ options.fetch(:serializer) { get_serializer_for(resource_class, options[:namespace]) }
48
49
  end
49
50
  end
50
51
 
@@ -59,17 +60,11 @@ module ActiveModel
59
60
  end
60
61
 
61
62
  # @api private
62
- def self.serializer_lookup_chain_for(klass)
63
- chain = []
64
-
65
- resource_class_name = klass.name.demodulize
66
- resource_namespace = klass.name.deconstantize
67
- serializer_class_name = "#{resource_class_name}Serializer"
68
-
69
- chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer
70
- chain.push("#{resource_namespace}::#{serializer_class_name}")
71
-
72
- chain
63
+ def self.serializer_lookup_chain_for(klass, namespace = nil)
64
+ lookups = ActiveModelSerializers.config.serializer_lookup_chain
65
+ Array[*lookups].flat_map do |lookup|
66
+ lookup.call(klass, self, namespace)
67
+ end.compact
73
68
  end
74
69
 
75
70
  # Used to cache serializer name => serializer class
@@ -84,20 +79,228 @@ module ActiveModel
84
79
  # 1. class name appended with "Serializer"
85
80
  # 2. try again with superclass, if present
86
81
  # 3. nil
87
- def self.get_serializer_for(klass)
82
+ def self.get_serializer_for(klass, namespace = nil)
88
83
  return nil unless config.serializer_lookup_enabled
89
- serializers_cache.fetch_or_store(klass) do
84
+
85
+ cache_key = ActiveSupport::Cache.expand_cache_key(klass, namespace)
86
+ serializers_cache.fetch_or_store(cache_key) do
90
87
  # NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
91
- serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
88
+ lookup_chain = serializer_lookup_chain_for(klass, namespace)
89
+ serializer_class = lookup_chain.map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
92
90
 
93
91
  if serializer_class
94
92
  serializer_class
95
93
  elsif klass.superclass
96
94
  get_serializer_for(klass.superclass)
95
+ else
96
+ nil # No serializer found
97
97
  end
98
98
  end
99
99
  end
100
100
 
101
+ # @api private
102
+ def self.include_directive_from_options(options)
103
+ if options[:include_directive]
104
+ options[:include_directive]
105
+ elsif options[:include]
106
+ JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
107
+ else
108
+ ActiveModelSerializers.default_include_directive
109
+ end
110
+ end
111
+
112
+ # @api private
113
+ def self.serialization_adapter_instance
114
+ @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
115
+ end
116
+
117
+ # Preferred interface is ActiveModelSerializers.config
118
+ # BEGIN DEFAULT CONFIGURATION
119
+ config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
120
+ config.serializer_lookup_enabled = true
121
+
122
+ # @deprecated Use {#config.collection_serializer=} instead of this. Is
123
+ # compatibility layer for ArraySerializer.
124
+ def config.array_serializer=(collection_serializer)
125
+ self.collection_serializer = collection_serializer
126
+ end
127
+
128
+ # @deprecated Use {#config.collection_serializer} instead of this. Is
129
+ # compatibility layer for ArraySerializer.
130
+ def config.array_serializer
131
+ collection_serializer
132
+ end
133
+
134
+ config.default_includes = '*'
135
+ config.adapter = :attributes
136
+ config.key_transform = nil
137
+ config.jsonapi_pagination_links_enabled = true
138
+ config.jsonapi_resource_type = :plural
139
+ config.jsonapi_namespace_separator = '-'.freeze
140
+ config.jsonapi_version = '1.0'
141
+ config.jsonapi_toplevel_meta = {}
142
+ # Make JSON API top-level jsonapi member opt-in
143
+ # ref: http://jsonapi.org/format/#document-top-level
144
+ config.jsonapi_include_toplevel_object = false
145
+ config.include_data_default = true
146
+
147
+ # For configuring how serializers are found.
148
+ # This should be an array of procs.
149
+ #
150
+ # The priority of the output is that the first item
151
+ # in the evaluated result array will take precedence
152
+ # over other possible serializer paths.
153
+ #
154
+ # i.e.: First match wins.
155
+ #
156
+ # @example output
157
+ # => [
158
+ # "CustomNamespace::ResourceSerializer",
159
+ # "ParentSerializer::ResourceSerializer",
160
+ # "ResourceNamespace::ResourceSerializer" ,
161
+ # "ResourceSerializer"]
162
+ #
163
+ # If CustomNamespace::ResourceSerializer exists, it will be used
164
+ # for serialization
165
+ config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
166
+
167
+ config.schema_path = 'test/support/schemas'
168
+ # END DEFAULT CONFIGURATION
169
+
170
+ with_options instance_writer: false, instance_reader: false do |serializer|
171
+ serializer.class_attribute :_attributes_data # @api private
172
+ self._attributes_data ||= {}
173
+ end
174
+ with_options instance_writer: false, instance_reader: true do |serializer|
175
+ serializer.class_attribute :_reflections
176
+ self._reflections ||= {}
177
+ serializer.class_attribute :_links # @api private
178
+ self._links ||= {}
179
+ serializer.class_attribute :_meta # @api private
180
+ serializer.class_attribute :_type # @api private
181
+ end
182
+
183
+ def self.inherited(base)
184
+ super
185
+ base._attributes_data = _attributes_data.dup
186
+ base._reflections = _reflections.dup
187
+ base._links = _links.dup
188
+ end
189
+
190
+ # @return [Array<Symbol>] Key names of declared attributes
191
+ # @see Serializer::attribute
192
+ def self._attributes
193
+ _attributes_data.keys
194
+ end
195
+
196
+ # BEGIN SERIALIZER MACROS
197
+
198
+ # @example
199
+ # class AdminAuthorSerializer < ActiveModel::Serializer
200
+ # attributes :id, :name, :recent_edits
201
+ def self.attributes(*attrs)
202
+ attrs = attrs.first if attrs.first.class == Array
203
+
204
+ attrs.each do |attr|
205
+ attribute(attr)
206
+ end
207
+ end
208
+
209
+ # @example
210
+ # class AdminAuthorSerializer < ActiveModel::Serializer
211
+ # attributes :id, :recent_edits
212
+ # attribute :name, key: :title
213
+ #
214
+ # attribute :full_name do
215
+ # "#{object.first_name} #{object.last_name}"
216
+ # end
217
+ #
218
+ # def recent_edits
219
+ # object.edits.last(5)
220
+ # end
221
+ def self.attribute(attr, options = {}, &block)
222
+ key = options.fetch(:key, attr)
223
+ _attributes_data[key] = Attribute.new(attr, options, block)
224
+ end
225
+
226
+ # @param [Symbol] name of the association
227
+ # @param [Hash<Symbol => any>] options for the reflection
228
+ # @return [void]
229
+ #
230
+ # @example
231
+ # has_many :comments, serializer: CommentSummarySerializer
232
+ #
233
+ def self.has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
234
+ associate(HasManyReflection.new(name, options, block))
235
+ end
236
+
237
+ # @param [Symbol] name of the association
238
+ # @param [Hash<Symbol => any>] options for the reflection
239
+ # @return [void]
240
+ #
241
+ # @example
242
+ # belongs_to :author, serializer: AuthorSerializer
243
+ #
244
+ def self.belongs_to(name, options = {}, &block)
245
+ associate(BelongsToReflection.new(name, options, block))
246
+ end
247
+
248
+ # @param [Symbol] name of the association
249
+ # @param [Hash<Symbol => any>] options for the reflection
250
+ # @return [void]
251
+ #
252
+ # @example
253
+ # has_one :author, serializer: AuthorSerializer
254
+ #
255
+ def self.has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
256
+ associate(HasOneReflection.new(name, options, block))
257
+ end
258
+
259
+ # Add reflection and define {name} accessor.
260
+ # @param [ActiveModel::Serializer::Reflection] reflection
261
+ # @return [void]
262
+ #
263
+ # @api private
264
+ def self.associate(reflection)
265
+ key = reflection.options[:key] || reflection.name
266
+ self._reflections[key] = reflection
267
+ end
268
+ private_class_method :associate
269
+
270
+ # Define a link on a serializer.
271
+ # @example
272
+ # link(:self) { resource_url(object) }
273
+ # @example
274
+ # link(:self) { "http://example.com/resource/#{object.id}" }
275
+ # @example
276
+ # link :resource, "http://example.com/resource"
277
+ #
278
+ def self.link(name, value = nil, &block)
279
+ _links[name] = block || value
280
+ end
281
+
282
+ # Set the JSON API meta attribute of a serializer.
283
+ # @example
284
+ # class AdminAuthorSerializer < ActiveModel::Serializer
285
+ # meta { stuff: 'value' }
286
+ # @example
287
+ # meta do
288
+ # { comment_count: object.comments.count }
289
+ # end
290
+ def self.meta(value = nil, &block)
291
+ self._meta = block || value
292
+ end
293
+
294
+ # Set the JSON API type of a serializer.
295
+ # @example
296
+ # class AdminAuthorSerializer < ActiveModel::Serializer
297
+ # type 'authors'
298
+ def self.type(type)
299
+ self._type = type && type.to_s
300
+ end
301
+
302
+ # END SERIALIZER MACROS
303
+
101
304
  attr_accessor :object, :root, :scope
102
305
 
103
306
  # `scope_name` is set as :current_user by default in the controller.
@@ -109,63 +312,58 @@ module ActiveModel
109
312
  self.root = instance_options[:root]
110
313
  self.scope = instance_options[:scope]
111
314
 
112
- scope_name = instance_options[:scope_name]
113
- if scope_name && !respond_to?(scope_name)
114
- define_singleton_method scope_name, lambda { scope }
115
- end
315
+ return if !(scope_name = instance_options[:scope_name]) || respond_to?(scope_name)
316
+
317
+ define_singleton_method scope_name, -> { scope }
116
318
  end
117
319
 
118
320
  def success?
119
321
  true
120
322
  end
121
323
 
324
+ # Return the +attributes+ of +object+ as presented
325
+ # by the serializer.
326
+ def attributes(requested_attrs = nil, reload = false)
327
+ @attributes = nil if reload
328
+ @attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
329
+ next if attr.excluded?(self)
330
+ next unless requested_attrs.nil? || requested_attrs.include?(key)
331
+ hash[key] = attr.value(self)
332
+ end
333
+ end
334
+
335
+ # @param [JSONAPI::IncludeDirective] include_directive (defaults to the
336
+ # +default_include_directive+ config value when not provided)
337
+ # @return [Enumerator<Association>]
338
+ def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
339
+ include_slice ||= include_directive
340
+ return Enumerator.new unless object
341
+
342
+ Enumerator.new do |y|
343
+ self.class._reflections.each do |key, reflection|
344
+ next if reflection.excluded?(self)
345
+ next unless include_directive.key?(key)
346
+
347
+ association = reflection.build_association(self, instance_options, include_slice)
348
+ y.yield association
349
+ end
350
+ end
351
+ end
352
+
122
353
  # @return [Hash] containing the attributes and first level
123
354
  # associations, similar to how ActiveModel::Serializers::JSON is used
124
355
  # in ActiveRecord::Base.
125
- #
126
- # TODO: Move to here the Attributes adapter logic for
127
- # +serializable_hash_for_single_resource(options)+
128
- # and include <tt>ActiveModel::Serializers::JSON</tt>.
129
- # So that the below is true:
130
- # @param options [nil, Hash] The same valid options passed to `serializable_hash`
131
- # (:only, :except, :methods, and :include).
132
- #
133
- # See
134
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serializers/json.rb#L17-L101
135
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serialization.rb#L85-L123
136
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activerecord/lib/active_record/serialization.rb#L11-L17
137
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activesupport/lib/active_support/core_ext/object/json.rb#L147-L162
138
- #
139
- # @example
140
- # # The :only and :except options can be used to limit the attributes included, and work
141
- # # similar to the attributes method.
142
- # serializer.as_json(only: [:id, :name])
143
- # serializer.as_json(except: [:id, :created_at, :age])
144
- #
145
- # # To include the result of some method calls on the model use :methods:
146
- # serializer.as_json(methods: :permalink)
147
- #
148
- # # To include associations use :include:
149
- # serializer.as_json(include: :posts)
150
- # # Second level and higher order associations work as well:
151
- # serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
152
- def serializable_hash(adapter_opts = nil)
153
- adapter_opts ||= {}
154
- adapter_opts = { include: '*', adapter: :attributes }.merge!(adapter_opts)
155
- adapter = ActiveModelSerializers::Adapter.create(self, adapter_opts)
156
- adapter.serializable_hash(adapter_opts)
356
+ def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
357
+ adapter_options ||= {}
358
+ options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
359
+ resource = attributes_hash(adapter_options, options, adapter_instance)
360
+ relationships = associations_hash(adapter_options, options, adapter_instance)
361
+ resource.merge(relationships)
157
362
  end
158
363
  alias to_hash serializable_hash
159
364
  alias to_h serializable_hash
160
365
 
161
366
  # @see #serializable_hash
162
- # TODO: When moving attributes adapter logic here, @see #serializable_hash
163
- # So that the below is true:
164
- # @param options [nil, Hash] The same valid options passed to `as_json`
165
- # (:root, :only, :except, :methods, and :include).
166
- # The default for `root` is nil.
167
- # The default value for include_root is false. You can change it to true if the given
168
- # JSON string includes a single root node.
169
367
  def as_json(adapter_opts = nil)
170
368
  serializable_hash(adapter_opts)
171
369
  end
@@ -178,13 +376,32 @@ module ActiveModel
178
376
  def read_attribute_for_serialization(attr)
179
377
  if respond_to?(attr)
180
378
  send(attr)
181
- elsif self.class._fragmented
182
- self.class._fragmented.read_attribute_for_serialization(attr)
183
379
  else
184
380
  object.read_attribute_for_serialization(attr)
185
381
  end
186
382
  end
187
383
 
384
+ # @api private
385
+ def attributes_hash(_adapter_options, options, adapter_instance)
386
+ if self.class.cache_enabled?
387
+ fetch_attributes(options[:fields], options[:cached_attributes] || {}, adapter_instance)
388
+ elsif self.class.fragment_cache_enabled?
389
+ fetch_attributes_fragment(adapter_instance, options[:cached_attributes] || {})
390
+ else
391
+ attributes(options[:fields], true)
392
+ end
393
+ end
394
+
395
+ # @api private
396
+ def associations_hash(adapter_options, options, adapter_instance)
397
+ include_directive = options.fetch(:include_directive)
398
+ include_slice = options[:include_slice]
399
+ associations(include_directive, include_slice).each_with_object({}) do |association, relationships|
400
+ adapter_opts = adapter_options.merge(include_directive: include_directive[association.key], adapter_instance: adapter_instance)
401
+ relationships[association.key] = association.serializable_hash(adapter_opts, adapter_instance)
402
+ end
403
+ end
404
+
188
405
  protected
189
406
 
190
407
  attr_accessor :instance_options
@@ -1,75 +1,12 @@
1
1
  module ActiveModelSerializers
2
2
  module Adapter
3
3
  class Attributes < Base
4
- def initialize(serializer, options = {})
5
- super
6
- @include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include] || '*')
7
- @cached_attributes = options[:cache_attributes] || {}
8
- end
9
-
10
4
  def serializable_hash(options = nil)
11
5
  options = serialization_options(options)
6
+ options[:fields] ||= instance_options[:fields]
7
+ serialized_hash = serializer.serializable_hash(instance_options, options, self)
12
8
 
13
- if serializer.respond_to?(:each)
14
- serializable_hash_for_collection(options)
15
- else
16
- serializable_hash_for_single_resource(options)
17
- end
18
- end
19
-
20
- private
21
-
22
- def serializable_hash_for_collection(options)
23
- cache_attributes
24
-
25
- serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
26
- end
27
-
28
- def serializable_hash_for_single_resource(options)
29
- resource = resource_object_for(options)
30
- relationships = resource_relationships(options)
31
- resource.merge(relationships)
32
- end
33
-
34
- def resource_relationships(options)
35
- relationships = {}
36
- serializer.associations(@include_tree).each do |association|
37
- relationships[association.key] ||= relationship_value_for(association, options)
38
- end
39
-
40
- relationships
41
- end
42
-
43
- def relationship_value_for(association, options)
44
- return association.options[:virtual_value] if association.options[:virtual_value]
45
- return unless association.serializer && association.serializer.object
46
-
47
- opts = instance_options.merge(include: @include_tree[association.key])
48
- relationship_value = Attributes.new(association.serializer, opts).serializable_hash(options)
49
-
50
- if association.options[:polymorphic] && relationship_value
51
- polymorphic_type = association.serializer.object.class.name.underscore
52
- relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
53
- end
54
-
55
- relationship_value
56
- end
57
-
58
- # Set @cached_attributes
59
- def cache_attributes
60
- return if @cached_attributes.present?
61
-
62
- @cached_attributes = ActiveModel::Serializer.cache_read_multi(serializer, self, @include_tree)
63
- end
64
-
65
- def resource_object_for(options)
66
- if serializer.class.cache_enabled?
67
- @cached_attributes.fetch(serializer.cache_key(self)) do
68
- serializer.cached_fields(options[:fields], self)
69
- end
70
- else
71
- serializer.cached_fields(options[:fields], self)
72
- end
9
+ self.class.transform_key_casing!(serialized_hash, instance_options)
73
10
  end
74
11
  end
75
12
  end
@@ -1,4 +1,4 @@
1
- require 'active_model_serializers/key_transform'
1
+ require 'case_transform'
2
2
 
3
3
  module ActiveModelSerializers
4
4
  module Adapter
@@ -8,6 +8,40 @@ module ActiveModelSerializers
8
8
  ActiveModelSerializers::Adapter.register(subclass)
9
9
  end
10
10
 
11
+ # Sets the default transform for the adapter.
12
+ #
13
+ # @return [Symbol] the default transform for the adapter
14
+ def self.default_key_transform
15
+ :unaltered
16
+ end
17
+
18
+ # Determines the transform to use in order of precedence:
19
+ # adapter option, global config, adapter default.
20
+ #
21
+ # @param options [Object]
22
+ # @return [Symbol] the transform to use
23
+ def self.transform(options)
24
+ return options[:key_transform] if options && options[:key_transform]
25
+ ActiveModelSerializers.config.key_transform || default_key_transform
26
+ end
27
+
28
+ # Transforms the casing of the supplied value.
29
+ #
30
+ # @param value [Object] the value to be transformed
31
+ # @param options [Object] serializable resource options
32
+ # @return [Symbol] the default transform for the adapter
33
+ def self.transform_key_casing!(value, options)
34
+ CaseTransform.send(transform(options), value)
35
+ end
36
+
37
+ def self.cache_key
38
+ @cache_key ||= ActiveModelSerializers::Adapter.registered_name(self)
39
+ end
40
+
41
+ def self.fragment_cache(cached_hash, non_cached_hash)
42
+ non_cached_hash.merge cached_hash
43
+ end
44
+
11
45
  attr_reader :serializer, :instance_options
12
46
 
13
47
  def initialize(serializer, options = {})
@@ -15,10 +49,6 @@ module ActiveModelSerializers
15
49
  @instance_options = options
16
50
  end
17
51
 
18
- def cached_name
19
- @cached_name ||= self.class.name.demodulize.underscore
20
- end
21
-
22
52
  # Subclasses that implement this method must first call
23
53
  # options = serialization_options(options)
24
54
  def serializable_hash(_options = nil)
@@ -29,14 +59,12 @@ module ActiveModelSerializers
29
59
  serializable_hash(options)
30
60
  end
31
61
 
32
- def fragment_cache(cached_hash, non_cached_hash)
33
- non_cached_hash.merge cached_hash
62
+ def cache_key
63
+ self.class.cache_key
34
64
  end
35
65
 
36
- def cache_check(serializer)
37
- serializer.cache_check(self) do
38
- yield
39
- end
66
+ def fragment_cache(cached_hash, non_cached_hash)
67
+ self.class.fragment_cache(cached_hash, non_cached_hash)
40
68
  end
41
69
 
42
70
  private
@@ -50,34 +78,6 @@ module ActiveModelSerializers
50
78
  def root
51
79
  serializer.json_key.to_sym if serializer.json_key
52
80
  end
53
-
54
- class << self
55
- # Sets the default transform for the adapter.
56
- #
57
- # @return [Symbol] the default transform for the adapter
58
- def default_key_transform
59
- :unaltered
60
- end
61
-
62
- # Determines the transform to use in order of precedence:
63
- # adapter option, global config, adapter default.
64
- #
65
- # @param options [Object]
66
- # @return [Symbol] the transform to use
67
- def transform(options)
68
- return options[:key_transform] if options && options[:key_transform]
69
- ActiveModelSerializers.config.key_transform || default_key_transform
70
- end
71
-
72
- # Transforms the casing of the supplied value.
73
- #
74
- # @param value [Object] the value to be transformed
75
- # @param options [Object] serializable resource options
76
- # @return [Symbol] the default transform for the adapter
77
- def transform_key_casing!(value, options)
78
- KeyTransform.send(transform(options), value)
79
- end
80
- end
81
81
  end
82
82
  end
83
83
  end
@@ -205,7 +205,7 @@ module ActiveModelSerializers
205
205
  # @api private
206
206
  def transform_keys(hash, options)
207
207
  transform = options[:key_transform] || :underscore
208
- KeyTransform.send(transform, hash)
208
+ CaseTransform.send(transform, hash)
209
209
  end
210
210
  end
211
211
  end
@@ -71,7 +71,7 @@ module ActiveModelSerializers
71
71
  hash[:href] = @href if defined?(@href)
72
72
  hash[:meta] = @meta if defined?(@meta)
73
73
 
74
- hash
74
+ hash.any? ? hash : nil
75
75
  end
76
76
 
77
77
  protected
@@ -2,6 +2,7 @@ module ActiveModelSerializers
2
2
  module Adapter
3
3
  class JsonApi < Base
4
4
  class PaginationLinks
5
+ MissingSerializationContextError = Class.new(KeyError)
5
6
  FIRST_PAGE = 1
6
7
 
7
8
  attr_reader :collection, :context
@@ -9,7 +10,13 @@ module ActiveModelSerializers
9
10
  def initialize(collection, adapter_options)
10
11
  @collection = collection
11
12
  @adapter_options = adapter_options
12
- @context = adapter_options.fetch(:serialization_context)
13
+ @context = adapter_options.fetch(:serialization_context) do
14
+ fail MissingSerializationContextError, <<-EOF.freeze
15
+ JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
16
+ Please pass a ':serialization_context' option or
17
+ override CollectionSerializer#paginated? to return 'false'.
18
+ EOF
19
+ end
13
20
  end
14
21
 
15
22
  def as_json