active_model_serializers 0.10.7 → 0.10.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -1
  3. data/README.md +2 -2
  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 +6 -6
  17. data/lib/active_model/serializer/concerns/caching.rb +5 -1
  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 +2 -0
  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 +5 -0
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +32 -16
  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 +2 -0
  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 +2 -0
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +2 -0
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +3 -1
  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.rb +2 -0
  52. data/lib/active_model_serializers/railtie.rb +4 -0
  53. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  54. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  55. data/lib/active_model_serializers/serialization_context.rb +2 -0
  56. data/lib/active_model_serializers/test/schema.rb +2 -0
  57. data/lib/active_model_serializers/test/serializer.rb +2 -0
  58. data/lib/active_model_serializers/test.rb +2 -0
  59. data/lib/active_model_serializers.rb +20 -10
  60. data/lib/generators/rails/resource_override.rb +2 -0
  61. data/lib/generators/rails/serializer_generator.rb +2 -0
  62. data/lib/grape/active_model_serializers.rb +2 -0
  63. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  64. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  65. data/lib/tasks/rubocop.rake +2 -0
  66. metadata +64 -290
  67. data/.github/ISSUE_TEMPLATE.md +0 -29
  68. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  69. data/.gitignore +0 -35
  70. data/.rubocop.yml +0 -105
  71. data/.simplecov +0 -110
  72. data/.travis.yml +0 -52
  73. data/CODE_OF_CONDUCT.md +0 -74
  74. data/CONTRIBUTING.md +0 -105
  75. data/Gemfile +0 -72
  76. data/Rakefile +0 -74
  77. data/active_model_serializers.gemspec +0 -63
  78. data/appveyor.yml +0 -28
  79. data/bin/bench +0 -171
  80. data/bin/bench_regression +0 -316
  81. data/bin/rubocop +0 -38
  82. data/bin/serve_benchmark +0 -39
  83. data/docs/README.md +0 -41
  84. data/docs/STYLE.md +0 -58
  85. data/docs/general/adapters.md +0 -269
  86. data/docs/general/caching.md +0 -58
  87. data/docs/general/configuration_options.md +0 -185
  88. data/docs/general/deserialization.md +0 -100
  89. data/docs/general/fields.md +0 -31
  90. data/docs/general/getting_started.md +0 -133
  91. data/docs/general/instrumentation.md +0 -40
  92. data/docs/general/key_transforms.md +0 -40
  93. data/docs/general/logging.md +0 -21
  94. data/docs/general/rendering.md +0 -293
  95. data/docs/general/serializers.md +0 -483
  96. data/docs/how-open-source-maintained.jpg +0 -0
  97. data/docs/howto/add_pagination_links.md +0 -138
  98. data/docs/howto/add_relationship_links.md +0 -140
  99. data/docs/howto/add_root_key.md +0 -62
  100. data/docs/howto/grape_integration.md +0 -42
  101. data/docs/howto/outside_controller_use.md +0 -66
  102. data/docs/howto/passing_arbitrary_options.md +0 -27
  103. data/docs/howto/serialize_poro.md +0 -73
  104. data/docs/howto/test.md +0 -154
  105. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  106. data/docs/integrations/ember-and-json-api.md +0 -147
  107. data/docs/integrations/grape.md +0 -19
  108. data/docs/jsonapi/errors.md +0 -56
  109. data/docs/jsonapi/schema/schema.json +0 -366
  110. data/docs/jsonapi/schema.md +0 -151
  111. data/docs/rfcs/0000-namespace.md +0 -106
  112. data/docs/rfcs/template.md +0 -15
  113. data/test/action_controller/adapter_selector_test.rb +0 -62
  114. data/test/action_controller/explicit_serializer_test.rb +0 -135
  115. data/test/action_controller/json/include_test.rb +0 -246
  116. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  117. data/test/action_controller/json_api/errors_test.rb +0 -40
  118. data/test/action_controller/json_api/fields_test.rb +0 -66
  119. data/test/action_controller/json_api/linked_test.rb +0 -202
  120. data/test/action_controller/json_api/pagination_test.rb +0 -124
  121. data/test/action_controller/json_api/transform_test.rb +0 -189
  122. data/test/action_controller/lookup_proc_test.rb +0 -49
  123. data/test/action_controller/namespace_lookup_test.rb +0 -232
  124. data/test/action_controller/serialization_scope_name_test.rb +0 -235
  125. data/test/action_controller/serialization_test.rb +0 -472
  126. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  127. data/test/active_model_serializers/json_pointer_test.rb +0 -22
  128. data/test/active_model_serializers/logging_test.rb +0 -77
  129. data/test/active_model_serializers/model_test.rb +0 -142
  130. data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
  131. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
  132. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
  133. data/test/active_model_serializers/test/schema_test.rb +0 -131
  134. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  135. data/test/active_record_test.rb +0 -9
  136. data/test/adapter/attributes_test.rb +0 -40
  137. data/test/adapter/deprecation_test.rb +0 -100
  138. data/test/adapter/json/belongs_to_test.rb +0 -45
  139. data/test/adapter/json/collection_test.rb +0 -104
  140. data/test/adapter/json/has_many_test.rb +0 -53
  141. data/test/adapter/json/transform_test.rb +0 -93
  142. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  143. data/test/adapter/json_api/collection_test.rb +0 -96
  144. data/test/adapter/json_api/errors_test.rb +0 -76
  145. data/test/adapter/json_api/fields_test.rb +0 -96
  146. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  147. data/test/adapter/json_api/has_many_test.rb +0 -173
  148. data/test/adapter/json_api/has_one_test.rb +0 -80
  149. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -213
  150. data/test/adapter/json_api/json_api_test.rb +0 -33
  151. data/test/adapter/json_api/linked_test.rb +0 -413
  152. data/test/adapter/json_api/links_test.rb +0 -95
  153. data/test/adapter/json_api/pagination_links_test.rb +0 -206
  154. data/test/adapter/json_api/parse_test.rb +0 -137
  155. data/test/adapter/json_api/relationship_test.rb +0 -397
  156. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  157. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  158. data/test/adapter/json_api/transform_test.rb +0 -512
  159. data/test/adapter/json_api/type_test.rb +0 -193
  160. data/test/adapter/json_test.rb +0 -46
  161. data/test/adapter/null_test.rb +0 -22
  162. data/test/adapter/polymorphic_test.rb +0 -218
  163. data/test/adapter_test.rb +0 -67
  164. data/test/array_serializer_test.rb +0 -22
  165. data/test/benchmark/app.rb +0 -65
  166. data/test/benchmark/benchmarking_support.rb +0 -67
  167. data/test/benchmark/bm_active_record.rb +0 -81
  168. data/test/benchmark/bm_adapter.rb +0 -38
  169. data/test/benchmark/bm_caching.rb +0 -119
  170. data/test/benchmark/bm_lookup_chain.rb +0 -83
  171. data/test/benchmark/bm_transform.rb +0 -45
  172. data/test/benchmark/config.ru +0 -3
  173. data/test/benchmark/controllers.rb +0 -83
  174. data/test/benchmark/fixtures.rb +0 -219
  175. data/test/cache_test.rb +0 -651
  176. data/test/collection_serializer_test.rb +0 -127
  177. data/test/fixtures/active_record.rb +0 -113
  178. data/test/fixtures/poro.rb +0 -225
  179. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  180. data/test/generators/serializer_generator_test.rb +0 -75
  181. data/test/grape_test.rb +0 -196
  182. data/test/lint_test.rb +0 -49
  183. data/test/logger_test.rb +0 -20
  184. data/test/poro_test.rb +0 -9
  185. data/test/serializable_resource_test.rb +0 -79
  186. data/test/serializers/association_macros_test.rb +0 -37
  187. data/test/serializers/associations_test.rb +0 -468
  188. data/test/serializers/attribute_test.rb +0 -153
  189. data/test/serializers/attributes_test.rb +0 -52
  190. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  191. data/test/serializers/configuration_test.rb +0 -32
  192. data/test/serializers/fieldset_test.rb +0 -14
  193. data/test/serializers/meta_test.rb +0 -202
  194. data/test/serializers/options_test.rb +0 -32
  195. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  196. data/test/serializers/reflection_test.rb +0 -427
  197. data/test/serializers/root_test.rb +0 -21
  198. data/test/serializers/serialization_test.rb +0 -55
  199. data/test/serializers/serializer_for_test.rb +0 -136
  200. data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
  201. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  202. data/test/support/isolated_unit.rb +0 -84
  203. data/test/support/rails5_shims.rb +0 -53
  204. data/test/support/rails_app.rb +0 -38
  205. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  206. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  207. data/test/support/schemas/custom/show.json +0 -7
  208. data/test/support/schemas/hyper_schema.json +0 -93
  209. data/test/support/schemas/render_using_json_api.json +0 -43
  210. data/test/support/schemas/simple_json_pointers.json +0 -10
  211. data/test/support/serialization_testing.rb +0 -79
  212. data/test/test_helper.rb +0 -70
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c3a5900ed1c671e57b90ea00e3130ad3a5fe4939a6a1c36e1481dbdeeef3199
4
- data.tar.gz: 59ed63aa89861c650f87ba5bfeb5dfc90c997cd8fe4542129f28d1194d3c24ff
3
+ metadata.gz: 20ead782c7fc4fcfbfd9739a0fcb1957f65284c65c53b619b3b2ac5ed7339bc3
4
+ data.tar.gz: d65bf0ce1f442f19679c36f624fde661f6ce4d97db1f849ac948d104ac3d88d2
5
5
  SHA512:
6
- metadata.gz: 0ee2569a69953e286d63861bb01a37836ee9b25463980da1dc561815e0d7e7902aa1b1d344b265bb50f3c41b1e93ea6cfec2ba241c5314511d4a55a5082f5ce0
7
- data.tar.gz: d011e2d950d8b7869d4f49139d23cf994f2b1f6e8319aa2a3ff018d4f1c578d24bb64cfe632c912e91edd92e0f5945fa58a1a7742b611ef33c5a4d0a14dfbc70
6
+ metadata.gz: ff70ab49d8bc826f3247ffefe845a169411a2a0767a92fa5de560894f70914c99bdad75d17cf2403c7a1eeff68a1611dcfa35da175d2f2cc711c5f7961ac522f
7
+ data.tar.gz: d85f6549fb002b01220c0710f3928489d747792b4cd9bf65fef2b712dd3c70a6e628a6e33f306927886e85a0f50d32d195c988c8cb7d6045c30f235d5410f6c6
data/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## 0.10.x
2
2
 
3
- ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...0-10-stable)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...0-10-stable)
4
4
 
5
5
  Breaking changes:
6
6
 
@@ -10,6 +10,50 @@ Fixes:
10
10
 
11
11
  Misc:
12
12
 
13
+ ### [v0.10.10 (2019-07-13)](https://github.com/rails-api/active_model_serializers/compare/v0.10.9...v0.10.10)
14
+
15
+ Fixes:
16
+
17
+ - [#2319](https://github.com/rails-api/active_model_serializers/pull/2319) Fixes #2316. (@kylekeesling)
18
+ - Fix Rails 6.0 deprication warnings
19
+ - update test fixture schema to use `timestamps` instead of `timestamp`
20
+ - [#2223](https://github.com/rails-api/active_model_serializers/pull/2223) Support Fieldset in Attributes/JSON adapters documented in [docs/general/fields.md](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md) that worked partially before (@bf4)
21
+ - [#2337](https://github.com/rails-api/active_model_serializers/pull/2337) fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank (@InteNs)
22
+ - Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
23
+ - [#2172](https://github.com/rails-api/active_model_serializers/pull/2172) Preserve the namespace when falling back to a superclass serializer
24
+
25
+ Misc:
26
+
27
+ - [#2327](https://github.com/rails-api/active_model_serializers/pull/2327) Add support for Ruby 2.6 on Travis CI (@wasifhossain)
28
+ - [#2304](https://github.com/rails-api/active_model_serializers/pull/2304) Slim down bundled gem by excluding test files and docs (@greysteil)
29
+
30
+ ### [v0.10.9 (2019-02-08)](https://github.com/rails-api/active_model_serializers/compare/v0.10.8...v0.10.9)
31
+
32
+ Fixes:
33
+
34
+ - [#2288](https://github.com/rails-api/active_model_serializers/pull/2288)
35
+ Change the fetch method to deal with recyclable key cache strategy.
36
+ Fixes #2287. (@cintamani, @wasifhossain)
37
+ - [#2307](https://github.com/rails-api/active_model_serializers/pull/2307) Falsey attribute values should not be reevaluated.
38
+
39
+ Misc:
40
+
41
+ - [#2309](https://github.com/rails-api/active_model_serializers/pull/2309) Performance and memory usage fixes
42
+
43
+ ### [v0.10.8 (2018-11-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...v0.10.8)
44
+
45
+ Features:
46
+ - [#2279](https://github.com/rails-api/active_model_serializers/pull/2279) Support condition options in serializer link statements
47
+
48
+ Fixes:
49
+
50
+ - [#2296](https://github.com/rails-api/active_model_serializers/pull/2296) Fixes #2295 (@Hirurg103)
51
+ - Fix finding of namespaced serializer and non-namespaced model.
52
+ - [#2289](https://github.com/rails-api/active_model_serializers/pull/2289) Fixes #2255 (@f-mer)
53
+ - Fix autoloading race condition, especially in Rails 5.
54
+ - [#2299](https://github.com/rails-api/active_model_serializers/pull/2299) Fixes #2270 (@chau-bao-long via #2276)
55
+ - Fix reflection thread-safety bug
56
+
13
57
  ### [v0.10.7 (2017-11-14)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...v0.10.7)
14
58
 
15
59
  Regressions Fixed From v0.10.6:
data/README.md CHANGED
@@ -266,8 +266,8 @@ to know about, but not part of ActiveModelSerializers.)
266
266
 
267
267
  - An `ActiveRecord::Base` object.
268
268
  - Any Ruby object that passes the
269
- [Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
270
- [code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb).
269
+ [Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
270
+ [(code)](lib/active_model/serializer/lint.rb).
271
271
 
272
272
  ActiveModelSerializers provides a
273
273
  [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
  require 'active_model_serializers/serialization_context'
3
5
 
@@ -21,7 +23,15 @@ module ActionController
21
23
  end
22
24
 
23
25
  def namespace_for_serializer
24
- @namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
26
+ @namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
27
+ end
28
+
29
+ def namespace_for_class(klass)
30
+ if Module.method_defined?(:module_parent)
31
+ klass.module_parent
32
+ else
33
+ klass.parent
34
+ end
25
35
  end
26
36
 
27
37
  def serialization_scope
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model_serializers/adapter'
2
4
  require 'active_model_serializers/deprecate'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/collection_serializer'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/lazy_association'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/field'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class CollectionSerializer
@@ -19,11 +21,10 @@ module ActiveModel
19
21
 
20
22
  # @api private
21
23
  def serializable_hash(adapter_options, options, adapter_instance)
22
- include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
23
- adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
24
- adapter_opts = adapter_options.merge(include_directive: include_directive)
24
+ options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
25
+ options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, options[:include_directive])
25
26
  serializers.map do |serializer|
26
- serializer.serializable_hash(adapter_opts, options, adapter_instance)
27
+ serializer.serializable_hash(adapter_options, options, adapter_instance)
27
28
  end
28
29
  end
29
30
 
@@ -48,8 +49,7 @@ module ActiveModel
48
49
  # 4. key may be nil for empty collection and no serializer option
49
50
  key &&= key.pluralize
50
51
  # 5. fail if the key cannot be determined
51
- key || fail(ArgumentError, 'Cannot infer root key from collection type. Please
52
- specify the root or each_serializer option, or render a JSON String')
52
+ key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
53
53
  end
54
54
  # rubocop:enable Metrics/CyclomaticComplexity
55
55
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  UndefinedCacheKey = Class.new(StandardError)
@@ -281,7 +283,9 @@ module ActiveModel
281
283
  # Use object's cache_key if available, else derive a key from the object
282
284
  # Pass the `key` option to the `cache` declaration or override this method to customize the cache key
283
285
  def object_cache_key
284
- if object.respond_to?(:cache_key)
286
+ if object.respond_to?(:cache_key_with_version)
287
+ object.cache_key_with_version
288
+ elsif object.respond_to?(:cache_key)
285
289
  object.cache_key
286
290
  elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
287
291
  object_time_safe = object.updated_at
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class ErrorSerializer < ActiveModel::Serializer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/error_serializer'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # Holds all the meta-data about a field (i.e. attribute or association) as it was
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Fieldset
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -7,11 +9,12 @@ module ActiveModel
7
9
  delegate :collection?, to: :reflection
8
10
 
9
11
  def reflection_options
10
- @reflection_options ||= reflection.options.dup.reject { |k, _| !REFLECTION_OPTIONS.include?(k) }
12
+ @reflection_options ||= reflection.options.select { |k, _| REFLECTION_OPTIONS.include?(k) }
11
13
  end
12
14
 
13
15
  def object
14
- @object ||= reflection.value(
16
+ return @object if defined?(@object)
17
+ @object = reflection.value(
15
18
  association_options.fetch(:parent_serializer),
16
19
  association_options.fetch(:include_slice)
17
20
  )
@@ -76,6 +79,7 @@ module ActiveModel
76
79
  serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
77
80
  serializer = reflection_options.fetch(:serializer, nil)
78
81
  serializer_options[:serializer] = serializer if serializer
82
+ serializer_options[:namespace] = reflection_options[:namespace] if reflection_options[:namespace]
79
83
  serializer_class.new(object, serializer_options)
80
84
  end
81
85
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model/serializer/field'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ # Holds all the data about a serializer link
8
+ #
9
+ # @example
10
+ # class PostSerializer < ActiveModel::Serializer
11
+ # link :callback, if: :internal? do
12
+ # object.callback_link
13
+ # end
14
+ #
15
+ # def internal?
16
+ # instance_options[:internal] == true
17
+ # end
18
+ # end
19
+ #
20
+ class Link < Field
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Lint
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Null < Serializer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/field'
2
4
  require 'active_model/serializer/association'
3
5
 
@@ -151,6 +153,9 @@ module ActiveModel
151
153
  # @yield [ActiveModel::Serializer]
152
154
  # @return [:nil, associated resource or resource collection]
153
155
  def value(serializer, include_slice)
156
+ # NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe
157
+ # Therefore, when we build associations from reflections, we dup the entire reflection instance.
158
+ # Better solutions much appreciated!
154
159
  @object = serializer.object
155
160
  @scope = serializer.scope
156
161
 
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
- VERSION = '0.10.7'.freeze
5
+ VERSION = '0.10.10'.freeze
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thread_safe'
2
4
  require 'jsonapi/include_directive'
3
5
  require 'active_model/serializer/collection_serializer'
@@ -18,16 +20,17 @@ module ActiveModel
18
20
  # @see #serializable_hash for more details on these valid keys.
19
21
  SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
20
22
  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
23
+ eager_autoload do
24
+ autoload :Adapter
25
+ autoload :Null
26
+ autoload :Attribute
27
+ autoload :Link
28
+ autoload :Association
29
+ autoload :Reflection
30
+ autoload :BelongsToReflection
31
+ autoload :HasOneReflection
32
+ autoload :HasManyReflection
33
+ end
31
34
  include ActiveSupport::Configurable
32
35
  include Caching
33
36
 
@@ -91,7 +94,7 @@ module ActiveModel
91
94
  if serializer_class
92
95
  serializer_class
93
96
  elsif klass.superclass
94
- get_serializer_for(klass.superclass)
97
+ get_serializer_for(klass.superclass, namespace)
95
98
  else
96
99
  nil # No serializer found
97
100
  end
@@ -275,9 +278,14 @@ module ActiveModel
275
278
  # link(:self) { "http://example.com/resource/#{object.id}" }
276
279
  # @example
277
280
  # link :resource, "http://example.com/resource"
281
+ # @example
282
+ # link(:callback, if: :internal?), { "http://example.com/callback" }
278
283
  #
279
- def self.link(name, value = nil, &block)
280
- _links[name] = block || value
284
+ def self.link(name, *args, &block)
285
+ options = args.extract_options!
286
+ # For compatibility with the use case of passing link directly as string argument
287
+ # without block, we are creating a wrapping block
288
+ _links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
281
289
  end
282
290
 
283
291
  # Set the JSON API meta attribute of a serializer.
@@ -341,7 +349,7 @@ module ActiveModel
341
349
  return Enumerator.new {} unless object
342
350
 
343
351
  Enumerator.new do |y|
344
- self.class._reflections.each do |key, reflection|
352
+ (self.instance_reflections ||= self.class._reflections.deep_dup).each do |key, reflection|
345
353
  next if reflection.excluded?(self)
346
354
  next unless include_directive.key?(key)
347
355
 
@@ -357,6 +365,9 @@ module ActiveModel
357
365
  def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
358
366
  adapter_options ||= {}
359
367
  options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
368
+ if (fieldset = adapter_options[:fieldset])
369
+ options[:fields] = fieldset.fields_for(json_key)
370
+ end
360
371
  resource = attributes_hash(adapter_options, options, adapter_instance)
361
372
  relationships = associations_hash(adapter_options, options, adapter_instance)
362
373
  resource.merge(relationships)
@@ -371,7 +382,12 @@ module ActiveModel
371
382
 
372
383
  # Used by adapter as resource root.
373
384
  def json_key
374
- root || _type || object.class.model_name.to_s.underscore
385
+ root || _type ||
386
+ begin
387
+ object.class.model_name.to_s.underscore
388
+ rescue ArgumentError
389
+ 'anonymous_object'
390
+ end
375
391
  end
376
392
 
377
393
  def read_attribute_for_serialization(attr)
@@ -405,6 +421,6 @@ module ActiveModel
405
421
 
406
422
  protected
407
423
 
408
- attr_accessor :instance_options
424
+ attr_accessor :instance_options, :instance_reflections
409
425
  end
410
426
  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
@@ -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
@@ -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
@@ -11,7 +13,7 @@ module ActiveModelSerializers
11
13
  type = inflect_type(type)
12
14
  type = type_for(:no_class_needed, type, options)
13
15
  if id.blank?
14
- { type: type }
16
+ nil
15
17
  else
16
18
  { id: id.to_s, type: type }
17
19
  end
@@ -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'