active_model_serializers 0.10.6 → 0.10.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +114 -1
  3. data/README.md +21 -24
  4. data/lib/action_controller/serialization.rb +11 -1
  5. data/lib/active_model/serializable_resource.rb +2 -0
  6. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  7. data/lib/active_model/serializer/adapter/base.rb +2 -0
  8. data/lib/active_model/serializer/adapter/json.rb +2 -0
  9. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  10. data/lib/active_model/serializer/adapter/null.rb +2 -0
  11. data/lib/active_model/serializer/adapter.rb +2 -0
  12. data/lib/active_model/serializer/array_serializer.rb +2 -0
  13. data/lib/active_model/serializer/association.rb +2 -0
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
  16. data/lib/active_model/serializer/collection_serializer.rb +17 -5
  17. data/lib/active_model/serializer/concerns/caching.rb +7 -2
  18. data/lib/active_model/serializer/error_serializer.rb +2 -0
  19. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  20. data/lib/active_model/serializer/field.rb +2 -0
  21. data/lib/active_model/serializer/fieldset.rb +3 -1
  22. data/lib/active_model/serializer/has_many_reflection.rb +2 -0
  23. data/lib/active_model/serializer/has_one_reflection.rb +2 -0
  24. data/lib/active_model/serializer/lazy_association.rb +6 -2
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +2 -0
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +10 -5
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +37 -19
  31. data/lib/active_model_serializers/adapter/attributes.rb +23 -0
  32. data/lib/active_model_serializers/adapter/base.rb +2 -0
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +3 -1
  35. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  36. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  37. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  38. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +45 -20
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +18 -4
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +33 -25
  42. data/lib/active_model_serializers/adapter/json_api.rb +14 -9
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +3 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +2 -0
  47. data/lib/active_model_serializers/deserialization.rb +2 -0
  48. data/lib/active_model_serializers/json_pointer.rb +2 -0
  49. data/lib/active_model_serializers/logging.rb +2 -0
  50. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  51. data/lib/active_model_serializers/model/caching.rb +26 -0
  52. data/lib/active_model_serializers/model.rb +2 -0
  53. data/lib/active_model_serializers/railtie.rb +4 -0
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  55. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  56. data/lib/active_model_serializers/serialization_context.rb +2 -0
  57. data/lib/active_model_serializers/test/schema.rb +2 -0
  58. data/lib/active_model_serializers/test/serializer.rb +2 -0
  59. data/lib/active_model_serializers/test.rb +2 -0
  60. data/lib/active_model_serializers.rb +21 -11
  61. data/lib/generators/rails/resource_override.rb +2 -0
  62. data/lib/generators/rails/serializer_generator.rb +2 -0
  63. data/lib/grape/active_model_serializers.rb +2 -0
  64. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  65. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  66. data/lib/tasks/rubocop.rake +2 -0
  67. metadata +43 -298
  68. data/.github/ISSUE_TEMPLATE.md +0 -29
  69. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  70. data/.gitignore +0 -35
  71. data/.rubocop.yml +0 -105
  72. data/.simplecov +0 -110
  73. data/.travis.yml +0 -55
  74. data/CODE_OF_CONDUCT.md +0 -74
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -56
  77. data/Rakefile +0 -74
  78. data/active_model_serializers.gemspec +0 -63
  79. data/appveyor.yml +0 -30
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/rubocop +0 -38
  83. data/bin/serve_benchmark +0 -39
  84. data/docs/README.md +0 -41
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -263
  87. data/docs/general/caching.md +0 -58
  88. data/docs/general/configuration_options.md +0 -169
  89. data/docs/general/deserialization.md +0 -100
  90. data/docs/general/fields.md +0 -31
  91. data/docs/general/getting_started.md +0 -133
  92. data/docs/general/instrumentation.md +0 -40
  93. data/docs/general/key_transforms.md +0 -40
  94. data/docs/general/logging.md +0 -21
  95. data/docs/general/rendering.md +0 -293
  96. data/docs/general/serializers.md +0 -480
  97. data/docs/how-open-source-maintained.jpg +0 -0
  98. data/docs/howto/add_pagination_links.md +0 -138
  99. data/docs/howto/add_relationship_links.md +0 -140
  100. data/docs/howto/add_root_key.md +0 -55
  101. data/docs/howto/grape_integration.md +0 -42
  102. data/docs/howto/outside_controller_use.md +0 -66
  103. data/docs/howto/passing_arbitrary_options.md +0 -27
  104. data/docs/howto/serialize_poro.md +0 -73
  105. data/docs/howto/test.md +0 -154
  106. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  107. data/docs/integrations/ember-and-json-api.md +0 -147
  108. data/docs/integrations/grape.md +0 -19
  109. data/docs/jsonapi/errors.md +0 -56
  110. data/docs/jsonapi/schema/schema.json +0 -366
  111. data/docs/jsonapi/schema.md +0 -151
  112. data/docs/rfcs/0000-namespace.md +0 -106
  113. data/docs/rfcs/template.md +0 -15
  114. data/test/action_controller/adapter_selector_test.rb +0 -62
  115. data/test/action_controller/explicit_serializer_test.rb +0 -135
  116. data/test/action_controller/json/include_test.rb +0 -246
  117. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  118. data/test/action_controller/json_api/errors_test.rb +0 -40
  119. data/test/action_controller/json_api/fields_test.rb +0 -66
  120. data/test/action_controller/json_api/linked_test.rb +0 -202
  121. data/test/action_controller/json_api/pagination_test.rb +0 -116
  122. data/test/action_controller/json_api/transform_test.rb +0 -189
  123. data/test/action_controller/lookup_proc_test.rb +0 -49
  124. data/test/action_controller/namespace_lookup_test.rb +0 -232
  125. data/test/action_controller/serialization_scope_name_test.rb +0 -235
  126. data/test/action_controller/serialization_test.rb +0 -472
  127. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  128. data/test/active_model_serializers/json_pointer_test.rb +0 -22
  129. data/test/active_model_serializers/logging_test.rb +0 -77
  130. data/test/active_model_serializers/model_test.rb +0 -142
  131. data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
  132. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
  133. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
  134. data/test/active_model_serializers/test/schema_test.rb +0 -131
  135. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  136. data/test/active_record_test.rb +0 -9
  137. data/test/adapter/attributes_test.rb +0 -40
  138. data/test/adapter/deprecation_test.rb +0 -100
  139. data/test/adapter/json/belongs_to_test.rb +0 -45
  140. data/test/adapter/json/collection_test.rb +0 -104
  141. data/test/adapter/json/has_many_test.rb +0 -53
  142. data/test/adapter/json/transform_test.rb +0 -93
  143. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  144. data/test/adapter/json_api/collection_test.rb +0 -96
  145. data/test/adapter/json_api/errors_test.rb +0 -76
  146. data/test/adapter/json_api/fields_test.rb +0 -96
  147. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  148. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  149. data/test/adapter/json_api/has_many_test.rb +0 -173
  150. data/test/adapter/json_api/has_one_test.rb +0 -80
  151. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -183
  152. data/test/adapter/json_api/json_api_test.rb +0 -33
  153. data/test/adapter/json_api/linked_test.rb +0 -413
  154. data/test/adapter/json_api/links_test.rb +0 -95
  155. data/test/adapter/json_api/pagination_links_test.rb +0 -193
  156. data/test/adapter/json_api/parse_test.rb +0 -137
  157. data/test/adapter/json_api/relationship_test.rb +0 -397
  158. data/test/adapter/json_api/resource_identifier_test.rb +0 -110
  159. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  160. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  161. data/test/adapter/json_api/transform_test.rb +0 -512
  162. data/test/adapter/json_api/type_test.rb +0 -61
  163. data/test/adapter/json_test.rb +0 -46
  164. data/test/adapter/null_test.rb +0 -22
  165. data/test/adapter/polymorphic_test.rb +0 -171
  166. data/test/adapter_test.rb +0 -67
  167. data/test/array_serializer_test.rb +0 -22
  168. data/test/benchmark/app.rb +0 -65
  169. data/test/benchmark/benchmarking_support.rb +0 -67
  170. data/test/benchmark/bm_active_record.rb +0 -81
  171. data/test/benchmark/bm_adapter.rb +0 -38
  172. data/test/benchmark/bm_caching.rb +0 -119
  173. data/test/benchmark/bm_lookup_chain.rb +0 -83
  174. data/test/benchmark/bm_transform.rb +0 -45
  175. data/test/benchmark/config.ru +0 -3
  176. data/test/benchmark/controllers.rb +0 -83
  177. data/test/benchmark/fixtures.rb +0 -219
  178. data/test/cache_test.rb +0 -651
  179. data/test/collection_serializer_test.rb +0 -123
  180. data/test/fixtures/active_record.rb +0 -113
  181. data/test/fixtures/poro.rb +0 -225
  182. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  183. data/test/generators/serializer_generator_test.rb +0 -75
  184. data/test/grape_test.rb +0 -196
  185. data/test/lint_test.rb +0 -49
  186. data/test/logger_test.rb +0 -20
  187. data/test/poro_test.rb +0 -9
  188. data/test/serializable_resource_test.rb +0 -79
  189. data/test/serializers/association_macros_test.rb +0 -37
  190. data/test/serializers/associations_test.rb +0 -424
  191. data/test/serializers/attribute_test.rb +0 -153
  192. data/test/serializers/attributes_test.rb +0 -52
  193. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  194. data/test/serializers/configuration_test.rb +0 -32
  195. data/test/serializers/fieldset_test.rb +0 -14
  196. data/test/serializers/meta_test.rb +0 -202
  197. data/test/serializers/options_test.rb +0 -32
  198. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  199. data/test/serializers/reflection_test.rb +0 -427
  200. data/test/serializers/root_test.rb +0 -21
  201. data/test/serializers/serialization_test.rb +0 -55
  202. data/test/serializers/serializer_for_test.rb +0 -136
  203. data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
  204. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  205. data/test/support/isolated_unit.rb +0 -82
  206. data/test/support/rails5_shims.rb +0 -53
  207. data/test/support/rails_app.rb +0 -38
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  209. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  210. data/test/support/schemas/custom/show.json +0 -7
  211. data/test/support/schemas/hyper_schema.json +0 -93
  212. data/test/support/schemas/render_using_json_api.json +0 -43
  213. data/test/support/schemas/simple_json_pointers.json +0 -10
  214. data/test/support/serialization_testing.rb +0 -71
  215. data/test/test_helper.rb +0 -70
@@ -1,4 +1,5 @@
1
- require 'thread_safe'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'jsonapi/include_directive'
3
4
  require 'active_model/serializer/collection_serializer'
4
5
  require 'active_model/serializer/array_serializer'
@@ -18,16 +19,17 @@ module ActiveModel
18
19
  # @see #serializable_hash for more details on these valid keys.
19
20
  SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
20
21
  extend ActiveSupport::Autoload
21
- autoload :Adapter
22
- autoload :Null
23
- autoload :Attribute
24
- autoload :Association
25
- autoload :Reflection
26
- autoload :SingularReflection
27
- autoload :CollectionReflection
28
- autoload :BelongsToReflection
29
- autoload :HasOneReflection
30
- autoload :HasManyReflection
22
+ eager_autoload do
23
+ autoload :Adapter
24
+ autoload :Null
25
+ autoload :Attribute
26
+ autoload :Link
27
+ autoload :Association
28
+ autoload :Reflection
29
+ autoload :BelongsToReflection
30
+ autoload :HasOneReflection
31
+ autoload :HasManyReflection
32
+ end
31
33
  include ActiveSupport::Configurable
32
34
  include Caching
33
35
 
@@ -70,7 +72,7 @@ module ActiveModel
70
72
  # Used to cache serializer name => serializer class
71
73
  # when looked up by Serializer.get_serializer_for.
72
74
  def self.serializers_cache
73
- @serializers_cache ||= ThreadSafe::Cache.new
75
+ @serializers_cache ||= Concurrent::Map.new
74
76
  end
75
77
 
76
78
  # @api private
@@ -91,7 +93,7 @@ module ActiveModel
91
93
  if serializer_class
92
94
  serializer_class
93
95
  elsif klass.superclass
94
- get_serializer_for(klass.superclass)
96
+ get_serializer_for(klass.superclass, namespace)
95
97
  else
96
98
  nil # No serializer found
97
99
  end
@@ -142,7 +144,10 @@ module ActiveModel
142
144
  # Make JSON API top-level jsonapi member opt-in
143
145
  # ref: http://jsonapi.org/format/#document-top-level
144
146
  config.jsonapi_include_toplevel_object = false
147
+ config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
145
148
  config.include_data_default = true
149
+ # Raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError when cannot infer root key from collection type
150
+ config.raise_cannot_infer_root_key_error = true
146
151
 
147
152
  # For configuring how serializers are found.
148
153
  # This should be an array of procs.
@@ -274,9 +279,14 @@ module ActiveModel
274
279
  # link(:self) { "http://example.com/resource/#{object.id}" }
275
280
  # @example
276
281
  # link :resource, "http://example.com/resource"
282
+ # @example
283
+ # link(:callback, if: :internal?), { "http://example.com/callback" }
277
284
  #
278
- def self.link(name, value = nil, &block)
279
- _links[name] = block || value
285
+ def self.link(name, *args, &block)
286
+ options = args.extract_options!
287
+ # For compatibility with the use case of passing link directly as string argument
288
+ # without block, we are creating a wrapping block
289
+ _links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
280
290
  end
281
291
 
282
292
  # Set the JSON API meta attribute of a serializer.
@@ -337,10 +347,10 @@ module ActiveModel
337
347
  # @return [Enumerator<Association>]
338
348
  def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
339
349
  include_slice ||= include_directive
340
- return Enumerator.new unless object
350
+ return Enumerator.new {} unless object
341
351
 
342
352
  Enumerator.new do |y|
343
- self.class._reflections.each do |key, reflection|
353
+ (self.instance_reflections ||= self.class._reflections.deep_dup).each do |key, reflection|
344
354
  next if reflection.excluded?(self)
345
355
  next unless include_directive.key?(key)
346
356
 
@@ -356,6 +366,9 @@ module ActiveModel
356
366
  def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
357
367
  adapter_options ||= {}
358
368
  options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
369
+ if (fieldset = adapter_options[:fieldset])
370
+ options[:fields] = fieldset.fields_for(json_key)
371
+ end
359
372
  resource = attributes_hash(adapter_options, options, adapter_instance)
360
373
  relationships = associations_hash(adapter_options, options, adapter_instance)
361
374
  resource.merge(relationships)
@@ -370,7 +383,12 @@ module ActiveModel
370
383
 
371
384
  # Used by adapter as resource root.
372
385
  def json_key
373
- root || _type || object.class.model_name.to_s.underscore
386
+ root || _type ||
387
+ begin
388
+ object.class.model_name.to_s.underscore
389
+ rescue ArgumentError
390
+ 'anonymous_object'
391
+ end
374
392
  end
375
393
 
376
394
  def read_attribute_for_serialization(attr)
@@ -404,6 +422,6 @@ module ActiveModel
404
422
 
405
423
  protected
406
424
 
407
- attr_accessor :instance_options
425
+ attr_accessor :instance_options, :instance_reflections
408
426
  end
409
427
  end
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Attributes < Base
6
+ def initialize(*)
7
+ super
8
+ instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
9
+ end
10
+
4
11
  def serializable_hash(options = nil)
5
12
  options = serialization_options(options)
6
13
  options[:fields] ||= instance_options[:fields]
@@ -8,6 +15,22 @@ module ActiveModelSerializers
8
15
 
9
16
  self.class.transform_key_casing!(serialized_hash, instance_options)
10
17
  end
18
+
19
+ private
20
+
21
+ def fields_to_fieldset(fields)
22
+ return fields if fields.nil?
23
+ resource_fields = []
24
+ relationship_fields = {}
25
+ fields.each do |field|
26
+ case field
27
+ when Symbol, String then resource_fields << field
28
+ when Hash then relationship_fields.merge!(field)
29
+ else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
30
+ end
31
+ end
32
+ relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
33
+ end
11
34
  end
12
35
  end
13
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'case_transform'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Json < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -189,7 +191,7 @@ module ActiveModelSerializers
189
191
 
190
192
  polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
191
193
  if polymorphic
192
- hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'] : nil
194
+ hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil
193
195
  end
194
196
 
195
197
  hash
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base
@@ -15,17 +17,18 @@ module ActiveModelSerializers
15
17
  JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
16
18
  Please pass a ':serialization_context' option or
17
19
  override CollectionSerializer#paginated? to return 'false'.
18
- EOF
20
+ EOF
19
21
  end
20
22
  end
21
23
 
22
24
  def as_json
23
- per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
24
- pages_from.each_with_object({}) do |(key, value), hash|
25
- params = query_parameters.merge(page: { number: value, size: per_page }).to_query
26
-
27
- hash[key] = "#{url(adapter_options)}?#{params}"
28
- end
25
+ {
26
+ self: location_url,
27
+ first: first_page_url,
28
+ prev: prev_page_url,
29
+ next: next_page_url,
30
+ last: last_page_url
31
+ }
29
32
  end
30
33
 
31
34
  protected
@@ -34,25 +37,43 @@ module ActiveModelSerializers
34
37
 
35
38
  private
36
39
 
37
- def pages_from
38
- return {} if collection.total_pages <= FIRST_PAGE
40
+ def location_url
41
+ url_for_page(collection.current_page)
42
+ end
39
43
 
40
- {}.tap do |pages|
41
- pages[:self] = collection.current_page
44
+ def first_page_url
45
+ url_for_page(1)
46
+ end
42
47
 
43
- unless collection.current_page == FIRST_PAGE
44
- pages[:first] = FIRST_PAGE
45
- pages[:prev] = collection.current_page - FIRST_PAGE
46
- end
48
+ def last_page_url
49
+ if collection.total_pages == 0
50
+ url_for_page(FIRST_PAGE)
51
+ else
52
+ url_for_page(collection.total_pages)
53
+ end
54
+ end
47
55
 
48
- unless collection.current_page == collection.total_pages
49
- pages[:next] = collection.current_page + FIRST_PAGE
50
- pages[:last] = collection.total_pages
51
- end
56
+ def prev_page_url
57
+ return nil if collection.current_page == FIRST_PAGE
58
+ if collection.current_page > collection.total_pages
59
+ return url_for_page(collection.total_pages)
52
60
  end
61
+ url_for_page(collection.current_page - FIRST_PAGE)
53
62
  end
54
63
 
55
- def url(options)
64
+ def next_page_url
65
+ return nil if collection.total_pages == 0 ||
66
+ collection.current_page >= collection.total_pages
67
+ url_for_page(collection.next_page)
68
+ end
69
+
70
+ def url_for_page(number)
71
+ params = query_parameters.dup
72
+ params[:page] = { size: per_page, number: number }
73
+ "#{url(adapter_options)}?#{params.to_query}"
74
+ end
75
+
76
+ def url(options = {})
56
77
  @url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
57
78
  end
58
79
 
@@ -63,6 +84,10 @@ module ActiveModelSerializers
63
84
  def query_parameters
64
85
  @query_parameters ||= context.query_parameters
65
86
  end
87
+
88
+ def per_page
89
+ @per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
90
+ end
66
91
  end
67
92
  end
68
93
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -43,10 +45,16 @@ module ActiveModelSerializers
43
45
  end
44
46
 
45
47
  def data_for_one(association)
46
- if association.belongs_to? &&
47
- parent_serializer.object.respond_to?(association.reflection.foreign_key)
48
- id = parent_serializer.object.send(association.reflection.foreign_key)
49
- type = association.reflection.type.to_s
48
+ if belongs_to_id_on_self?(association)
49
+ id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
50
+ type =
51
+ if association.polymorphic?
52
+ # We can't infer resource type for polymorphic relationships from the serializer.
53
+ # We can ONLY know a polymorphic resource type by inspecting each resource.
54
+ association.lazy_association.serializer.json_key
55
+ else
56
+ association.reflection.type.to_s
57
+ end
50
58
  ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
51
59
  else
52
60
  # TODO(BF): Process relationship without evaluating lazy_association
@@ -86,6 +94,12 @@ module ActiveModelSerializers
86
94
  meta = association.meta
87
95
  meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
88
96
  end
97
+
98
+ def belongs_to_id_on_self?(association)
99
+ parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
100
+ association.belongs_to? &&
101
+ parent_serializer.object.respond_to?(association.reflection.foreign_key)
102
+ end
89
103
  end
90
104
  end
91
105
  end
@@ -1,33 +1,37 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
4
6
  class ResourceIdentifier
5
- def self.type_for(class_name, serializer_type = nil, transform_options = {})
6
- if serializer_type
7
- raw_type = serializer_type
8
- else
9
- inflection =
10
- if ActiveModelSerializers.config.jsonapi_resource_type == :singular
11
- :singularize
12
- else
13
- :pluralize
14
- end
15
-
16
- raw_type = class_name.underscore
17
- raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
18
- raw_type
19
- .gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
20
- raw_type
21
- end
7
+ def self.type_for(serializer, serializer_type = nil, transform_options = {})
8
+ raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
22
9
  JsonApi.send(:transform_key_casing!, raw_type, transform_options)
23
10
  end
24
11
 
25
12
  def self.for_type_with_id(type, id, options)
26
- return nil if id.blank?
27
- {
28
- id: id.to_s,
29
- type: type_for(:no_class_needed, type, options)
30
- }
13
+ type = inflect_type(type)
14
+ type = type_for(:no_class_needed, type, options)
15
+ if id.blank?
16
+ nil
17
+ else
18
+ { id: id.to_s, type: type }
19
+ end
20
+ end
21
+
22
+ def self.raw_type_from_serializer_object(object)
23
+ class_name = object.class.name # should use model_name
24
+ raw_type = class_name.underscore
25
+ raw_type = inflect_type(raw_type)
26
+ raw_type
27
+ .gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
28
+ raw_type
29
+ end
30
+
31
+ def self.inflect_type(type)
32
+ singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
33
+ inflection = singularize ? :singularize : :pluralize
34
+ ActiveSupport::Inflector.public_send(inflection, type)
31
35
  end
32
36
 
33
37
  # {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
@@ -37,8 +41,11 @@ module ActiveModelSerializers
37
41
  end
38
42
 
39
43
  def as_json
40
- return nil if id.blank?
41
- { id: id, type: type }
44
+ if id.blank?
45
+ { type: type }
46
+ else
47
+ { id: id.to_s, type: type }
48
+ end
42
49
  end
43
50
 
44
51
  protected
@@ -48,7 +55,8 @@ module ActiveModelSerializers
48
55
  private
49
56
 
50
57
  def type_for(serializer, transform_options)
51
- self.class.type_for(serializer.object.class.name, serializer._type, transform_options)
58
+ serializer_type = serializer._type
59
+ self.class.type_for(serializer, serializer_type, transform_options)
52
60
  end
53
61
 
54
62
  def id_for(serializer)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # {http://jsonapi.org/format/ JSON API specification}
2
4
  # rubocop:disable Style/AsciiComments
3
5
  # TODO: implement!
@@ -22,14 +24,16 @@ module ActiveModelSerializers
22
24
  module Adapter
23
25
  class JsonApi < Base
24
26
  extend ActiveSupport::Autoload
25
- autoload :Jsonapi
26
- autoload :ResourceIdentifier
27
- autoload :Relationship
28
- autoload :Link
29
- autoload :PaginationLinks
30
- autoload :Meta
31
- autoload :Error
32
- autoload :Deserialization
27
+ eager_autoload do
28
+ autoload :Jsonapi
29
+ autoload :ResourceIdentifier
30
+ autoload :Link
31
+ autoload :PaginationLinks
32
+ autoload :Meta
33
+ autoload :Error
34
+ autoload :Deserialization
35
+ autoload :Relationship
36
+ end
33
37
 
34
38
  def self.default_key_transform
35
39
  :dash
@@ -480,7 +484,8 @@ module ActiveModelSerializers
480
484
  # }.reject! {|_,v| v.nil? }
481
485
  def links_for(serializer)
482
486
  serializer._links.each_with_object({}) do |(name, value), hash|
483
- result = Link.new(serializer, value).as_json
487
+ next if value.excluded?(serializer)
488
+ result = Link.new(serializer, value.block).as_json
484
489
  hash[name] = result if result
485
490
  end
486
491
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Null < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  UnknownAdapterError = Class.new(ArgumentError)
@@ -35,7 +37,7 @@ module ActiveModelSerializers
35
37
 
36
38
  # @return [Array<Symbol>] list of adapter names
37
39
  def adapters
38
- adapter_map.keys.sort
40
+ adapter_map.keys.sort!
39
41
  end
40
42
 
41
43
  # Adds an adapter 'klass' with 'name' to the 'adapter_map'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Adapted from
2
4
  # https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
3
5
  require 'active_support/callbacks'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # Provides a single method +deprecate+ to be used to declare when
3
5
  # something is going away.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Deserialization
3
5
  module_function
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module JsonPointer
3
5
  module_function
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # ActiveModelSerializers::Logging
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module LookupChain
3
5
  # Standard appending of Serializer to the resource name.
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ module ActiveModelSerializers
3
+ class Model
4
+ module Caching
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attr_writer :updated_at
9
+ attributes :id
10
+ end
11
+
12
+ # Defaults to the downcased model name and updated_at
13
+ def cache_key
14
+ ActiveSupport::Cache.expand_cache_key([
15
+ self.class.model_name.name.downcase,
16
+ "#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
17
+ ].compact)
18
+ end
19
+
20
+ # Defaults to the time the serializer file was modified.
21
+ def updated_at
22
+ defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # ActiveModelSerializers::Model is a convenient superclass for making your models
2
4
  # from Plain-Old Ruby Objects (PORO). It also serves as a reference implementation
3
5
  # that satisfies ActiveModel::Serializer::Lint::Tests.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/railtie'
2
4
  require 'action_controller'
3
5
  require 'action_controller/railtie'
@@ -5,6 +7,8 @@ require 'action_controller/serialization'
5
7
 
6
8
  module ActiveModelSerializers
7
9
  class Railtie < Rails::Railtie
10
+ config.eager_load_namespaces << ActiveModelSerializers
11
+
8
12
  config.to_prepare do
9
13
  ActiveModel::Serializer.serializers_cache.clear
10
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Based on discussion in https://github.com/rails/rails/pull/23712#issuecomment-184977238,
2
4
  # the JSON API media type will have its own format/renderer.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
4
 
3
5
  module ActiveModelSerializers
@@ -14,8 +16,8 @@ module ActiveModelSerializers
14
16
  # @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
15
17
  def initialize(resource, options = {})
16
18
  @resource = resource
17
- @adapter_opts, @serializer_opts =
18
- options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
19
+ @adapter_opts = options.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
20
+ @serializer_opts = options.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
19
21
  end
20
22
 
21
23
  def serialization_scope=(scope)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/array/extract_options'
2
4
  module ActiveModelSerializers
3
5
  class SerializationContext
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Test
3
5
  module Schema
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
4
  module ActiveModelSerializers
3
5
  module Test
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Test
3
5
  extend ActiveSupport::Autoload