active_model_serializers 0.10.0 → 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 +239 -2
  3. data/README.md +171 -34
  4. data/lib/action_controller/serialization.rb +23 -3
  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 +4 -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 +10 -5
  13. data/lib/active_model/serializer/association.rb +64 -10
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  16. data/lib/active_model/serializer/collection_serializer.rb +48 -13
  17. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
  18. data/lib/active_model/serializer/error_serializer.rb +13 -7
  19. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  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 +5 -3
  23. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  24. data/lib/active_model/serializer/lazy_association.rb +99 -0
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +136 -130
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +130 -65
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +321 -86
  31. data/lib/active_model_serializers/adapter/attributes.rb +17 -57
  32. data/lib/active_model_serializers/adapter/base.rb +41 -39
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  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 +3 -1
  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 +52 -20
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  42. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +9 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +3 -2
  47. data/lib/active_model_serializers/deserialization.rb +4 -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 +82 -0
  51. data/lib/active_model_serializers/model/caching.rb +26 -0
  52. data/lib/active_model_serializers/model.rb +111 -28
  53. data/lib/active_model_serializers/railtie.rb +7 -1
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  55. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  56. data/lib/active_model_serializers/serialization_context.rb +12 -3
  57. data/lib/active_model_serializers/test/schema.rb +4 -2
  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 +35 -10
  61. data/lib/generators/rails/resource_override.rb +3 -1
  62. data/lib/generators/rails/serializer_generator.rb +6 -4
  63. data/lib/grape/active_model_serializers.rb +9 -5
  64. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  65. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  66. data/lib/tasks/rubocop.rake +55 -0
  67. metadata +104 -296
  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 -104
  72. data/.rubocop_todo.yml +0 -167
  73. data/.simplecov +0 -110
  74. data/.travis.yml +0 -43
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -53
  77. data/Rakefile +0 -103
  78. data/active_model_serializers.gemspec +0 -66
  79. data/appveyor.yml +0 -24
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/serve_benchmark +0 -39
  83. data/docs/ARCHITECTURE.md +0 -126
  84. data/docs/README.md +0 -40
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -245
  87. data/docs/general/caching.md +0 -52
  88. data/docs/general/configuration_options.md +0 -100
  89. data/docs/general/deserialization.md +0 -100
  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 -14
  94. data/docs/general/rendering.md +0 -255
  95. data/docs/general/serializers.md +0 -372
  96. data/docs/how-open-source-maintained.jpg +0 -0
  97. data/docs/howto/add_pagination_links.md +0 -139
  98. data/docs/howto/add_root_key.md +0 -51
  99. data/docs/howto/outside_controller_use.md +0 -58
  100. data/docs/howto/passing_arbitrary_options.md +0 -27
  101. data/docs/howto/serialize_poro.md +0 -32
  102. data/docs/howto/test.md +0 -152
  103. data/docs/integrations/ember-and-json-api.md +0 -112
  104. data/docs/integrations/grape.md +0 -19
  105. data/docs/jsonapi/errors.md +0 -56
  106. data/docs/jsonapi/schema/schema.json +0 -366
  107. data/docs/jsonapi/schema.md +0 -151
  108. data/docs/rfcs/0000-namespace.md +0 -106
  109. data/docs/rfcs/template.md +0 -15
  110. data/lib/active_model/serializer/associations.rb +0 -100
  111. data/lib/active_model/serializer/attributes.rb +0 -82
  112. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  113. data/lib/active_model/serializer/configuration.rb +0 -35
  114. data/lib/active_model/serializer/include_tree.rb +0 -111
  115. data/lib/active_model/serializer/links.rb +0 -35
  116. data/lib/active_model/serializer/meta.rb +0 -29
  117. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  118. data/lib/active_model/serializer/type.rb +0 -25
  119. data/lib/active_model_serializers/key_transform.rb +0 -70
  120. data/test/action_controller/adapter_selector_test.rb +0 -53
  121. data/test/action_controller/explicit_serializer_test.rb +0 -134
  122. data/test/action_controller/json/include_test.rb +0 -167
  123. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  124. data/test/action_controller/json_api/errors_test.rb +0 -41
  125. data/test/action_controller/json_api/linked_test.rb +0 -197
  126. data/test/action_controller/json_api/pagination_test.rb +0 -116
  127. data/test/action_controller/json_api/transform_test.rb +0 -181
  128. data/test/action_controller/serialization_scope_name_test.rb +0 -229
  129. data/test/action_controller/serialization_test.rb +0 -469
  130. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  131. data/test/active_model_serializers/json_pointer_test.rb +0 -20
  132. data/test/active_model_serializers/key_transform_test.rb +0 -263
  133. data/test/active_model_serializers/logging_test.rb +0 -77
  134. data/test/active_model_serializers/model_test.rb +0 -9
  135. data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
  136. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
  137. data/test/active_model_serializers/test/schema_test.rb +0 -130
  138. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  139. data/test/active_record_test.rb +0 -9
  140. data/test/adapter/deprecation_test.rb +0 -100
  141. data/test/adapter/json/belongs_to_test.rb +0 -45
  142. data/test/adapter/json/collection_test.rb +0 -90
  143. data/test/adapter/json/has_many_test.rb +0 -45
  144. data/test/adapter/json/transform_test.rb +0 -93
  145. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  146. data/test/adapter/json_api/collection_test.rb +0 -95
  147. data/test/adapter/json_api/errors_test.rb +0 -78
  148. data/test/adapter/json_api/fields_test.rb +0 -87
  149. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  150. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  151. data/test/adapter/json_api/has_many_test.rb +0 -144
  152. data/test/adapter/json_api/has_one_test.rb +0 -80
  153. data/test/adapter/json_api/json_api_test.rb +0 -35
  154. data/test/adapter/json_api/linked_test.rb +0 -392
  155. data/test/adapter/json_api/links_test.rb +0 -93
  156. data/test/adapter/json_api/pagination_links_test.rb +0 -166
  157. data/test/adapter/json_api/parse_test.rb +0 -137
  158. data/test/adapter/json_api/relationship_test.rb +0 -161
  159. data/test/adapter/json_api/relationships_test.rb +0 -199
  160. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  161. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  162. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  163. data/test/adapter/json_api/transform_test.rb +0 -502
  164. data/test/adapter/json_api/type_test.rb +0 -61
  165. data/test/adapter/json_test.rb +0 -45
  166. data/test/adapter/null_test.rb +0 -23
  167. data/test/adapter/polymorphic_test.rb +0 -171
  168. data/test/adapter_test.rb +0 -67
  169. data/test/array_serializer_test.rb +0 -22
  170. data/test/benchmark/app.rb +0 -65
  171. data/test/benchmark/benchmarking_support.rb +0 -67
  172. data/test/benchmark/bm_caching.rb +0 -119
  173. data/test/benchmark/bm_transform.rb +0 -34
  174. data/test/benchmark/config.ru +0 -3
  175. data/test/benchmark/controllers.rb +0 -84
  176. data/test/benchmark/fixtures.rb +0 -219
  177. data/test/cache_test.rb +0 -485
  178. data/test/collection_serializer_test.rb +0 -110
  179. data/test/fixtures/active_record.rb +0 -78
  180. data/test/fixtures/poro.rb +0 -282
  181. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  182. data/test/generators/serializer_generator_test.rb +0 -57
  183. data/test/grape_test.rb +0 -82
  184. data/test/include_tree/from_include_args_test.rb +0 -26
  185. data/test/include_tree/from_string_test.rb +0 -94
  186. data/test/include_tree/include_args_to_hash_test.rb +0 -64
  187. data/test/lint_test.rb +0 -49
  188. data/test/logger_test.rb +0 -18
  189. data/test/poro_test.rb +0 -9
  190. data/test/serializable_resource_test.rb +0 -83
  191. data/test/serializers/association_macros_test.rb +0 -36
  192. data/test/serializers/associations_test.rb +0 -295
  193. data/test/serializers/attribute_test.rb +0 -151
  194. data/test/serializers/attributes_test.rb +0 -52
  195. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  196. data/test/serializers/configuration_test.rb +0 -32
  197. data/test/serializers/fieldset_test.rb +0 -14
  198. data/test/serializers/meta_test.rb +0 -196
  199. data/test/serializers/options_test.rb +0 -21
  200. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  201. data/test/serializers/root_test.rb +0 -21
  202. data/test/serializers/serialization_test.rb +0 -55
  203. data/test/serializers/serializer_for_test.rb +0 -134
  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 -79
  206. data/test/support/rails5_shims.rb +0 -47
  207. data/test/support/rails_app.rb +0 -45
  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 -53
  215. data/test/test_helper.rb +0 -57
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: eb97c8740c2530c085ee9186ea27ddd220367fc3
4
- data.tar.gz: 0a768d3ab122abe2c3e7f5b6c00a8dada6872433
2
+ SHA256:
3
+ metadata.gz: 94ecdeeabcad7f33e6b2891f2354edeb1fdabf444d9938014d839b554f0a5ab7
4
+ data.tar.gz: cd43b9ceddf4920feb4efae6c5d0748bbb6e37111328c015cdcca06ba8f9d86d
5
5
  SHA512:
6
- metadata.gz: 5589022dd6fa160c0a03981cedf46433fb1eb19e4bac5f3489908671e9e0969c48bb7a86cf03d12cf8ce825b2efe3ed6ad1b6594019635470319c899e7964617
7
- data.tar.gz: 989cf413780f12d47a553a7aae5cba33de1625bbc022366b7e4533810b87b151302ed6000116c03234bf2b2048a913048624728a54be87cead848076924cf6ce
6
+ metadata.gz: 93b28755ff8f015277534dfc971cab0ef5bf30745bebb34c90e2f6e17c1cb611ef1100ba9e484ebdd1dc415d2c9d1c7e715984a1052e75daada8f712b1126850
7
+ data.tar.gz: 86d257304d0e58b87e82a8c32f55a7722ffdc568dd394ac93214dfe382bf8ff27c8bed24c11f6a948184a16a67ad5fa7bbaf98f2443b2d11c017ce5463c58947
data/CHANGELOG.md CHANGED
@@ -1,8 +1,244 @@
1
1
  ## 0.10.x
2
2
 
3
- ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...master)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.13...0-10-stable)
4
4
 
5
- ### v0.10.0 (2016-05-17)
5
+ Breaking changes:
6
+
7
+ Features:
8
+
9
+ Fixes:
10
+
11
+ Misc:
12
+
13
+ ### [v0.10.13 (2022-01013)](https://github.com/rails-api/active_model_serializers/compare/v0.10.12...v0.10.13)
14
+
15
+ Fixes:
16
+
17
+ - [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0)
18
+
19
+ ### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)
20
+
21
+ Fixes:
22
+
23
+ - [#2398](https://github.com/rails-api/active_model_serializers/pull/2398) Update rails dependency to < 6.2 (@ritikesh)
24
+
25
+ ### [v0.10.11 (2020-12-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...v0.10.11)
26
+
27
+ Features:
28
+
29
+ - [#2361](https://github.com/rails-api/active_model_serializers/pull/2361) Add `ActiveModelSerializers.config.use_sha1_digests` to allow customization of the hashing algorithm used for serializer caching (@alexzherdev)
30
+
31
+ Fixes:
32
+
33
+ - [#2344](https://github.com/rails-api/active_model_serializers/pull/2344) Fixes #2341 introduced since #2223 (@wasifhossain)
34
+ - [#2395](https://github.com/rails-api/active_model_serializers/pull/2395) remove explicit require for thread_safe (@ritikesh)
35
+
36
+ ### [v0.10.10 (2019-07-13)](https://github.com/rails-api/active_model_serializers/compare/v0.10.9...v0.10.10)
37
+
38
+ Fixes:
39
+
40
+ - [#2319](https://github.com/rails-api/active_model_serializers/pull/2319) Fixes #2316. (@kylekeesling)
41
+ - Fix Rails 6.0 deprication warnings
42
+ - update test fixture schema to use `timestamps` instead of `timestamp`
43
+ - [#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)
44
+ - [#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)
45
+ - Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
46
+ - [#2172](https://github.com/rails-api/active_model_serializers/pull/2172) Preserve the namespace when falling back to a superclass serializer
47
+
48
+ Misc:
49
+
50
+ - [#2327](https://github.com/rails-api/active_model_serializers/pull/2327) Add support for Ruby 2.6 on Travis CI (@wasifhossain)
51
+ - [#2304](https://github.com/rails-api/active_model_serializers/pull/2304) Slim down bundled gem by excluding test files and docs (@greysteil)
52
+
53
+ ### [v0.10.9 (2019-02-08)](https://github.com/rails-api/active_model_serializers/compare/v0.10.8...v0.10.9)
54
+
55
+ Fixes:
56
+
57
+ - [#2288](https://github.com/rails-api/active_model_serializers/pull/2288)
58
+ Change the fetch method to deal with recyclable key cache strategy.
59
+ Fixes #2287. (@cintamani, @wasifhossain)
60
+ - [#2307](https://github.com/rails-api/active_model_serializers/pull/2307) Falsey attribute values should not be reevaluated.
61
+
62
+ Misc:
63
+
64
+ - [#2309](https://github.com/rails-api/active_model_serializers/pull/2309) Performance and memory usage fixes
65
+
66
+ ### [v0.10.8 (2018-11-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...v0.10.8)
67
+
68
+ Features:
69
+ - [#2279](https://github.com/rails-api/active_model_serializers/pull/2279) Support condition options in serializer link statements
70
+
71
+ Fixes:
72
+
73
+ - [#2296](https://github.com/rails-api/active_model_serializers/pull/2296) Fixes #2295 (@Hirurg103)
74
+ - Fix finding of namespaced serializer and non-namespaced model.
75
+ - [#2289](https://github.com/rails-api/active_model_serializers/pull/2289) Fixes #2255 (@f-mer)
76
+ - Fix autoloading race condition, especially in Rails 5.
77
+ - [#2299](https://github.com/rails-api/active_model_serializers/pull/2299) Fixes #2270 (@chau-bao-long via #2276)
78
+ - Fix reflection thread-safety bug
79
+
80
+ ### [v0.10.7 (2017-11-14)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...v0.10.7)
81
+
82
+ Regressions Fixed From v0.10.6:
83
+
84
+ - [#2211](https://github.com/rails-api/active_model_serializers/pull/2211). Fixes #2125, #2160. (@bf4)
85
+ - Fix polymorphic belongs_to tests; passes on v0.10.5, fails on v0.10.6
86
+ - Fix JSON:API polymorphic type regression from v0.10.5
87
+ - Fix JSON:API: for_type_and_id should always inflect_type
88
+ ```
89
+ Should Serializer._type ever be inflected?
90
+ Right now, it won't be, but association.serializer._type will be inflected.
91
+
92
+ That's the current behavior.
93
+ ```
94
+ - [#2216](https://github.com/rails-api/active_model_serializers/pull/2216). Fixes #2132, #2180. (@bf4)
95
+ - Fix JSON:API: Serialize resource type for unpersisted records (blank id)
96
+ - [#2218](https://github.com/rails-api/active_model_serializers/pull/2218). Fixes #2178. (@bf4)
97
+ - Fix JSON:API: Make using foreign key on belongs_to opt-in. No effect on polymorphic relationships.
98
+ ```
99
+ # set to true to opt-in
100
+ ActiveModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
101
+ ```
102
+
103
+ Features:
104
+
105
+ - [#2136](https://github.com/rails-api/active_model_serializers/pull/2136) Enable inclusion of sideloaded relationship objects by `key`. (@caomania)
106
+ - [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
107
+ - [#2130](https://github.com/rails-api/active_model_serializers/pull/2130) Allow serialized ID to be overwritten for belongs-to relationships. (@greysteil)
108
+ - [#2189](https://github.com/rails-api/active_model_serializers/pull/2189)
109
+ Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.3']`
110
+ (@tagliala)
111
+
112
+ Fixes:
113
+
114
+ - [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
115
+ - [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
116
+ - [#2214](https://github.com/rails-api/active_model_serializers/pull/2214) Fail if unable to infer collection type with json adapter. (@jmeredith16)
117
+ - [#2149](https://github.com/rails-api/active_model_serializers/pull/2149) Always include self, first, last pagination link. (@mecampbellsoup)
118
+ - [#2179](https://github.com/rails-api/active_model_serializers/pull/2179) Fixes #2173, Pass block to Enumerator.new. (@drn)
119
+
120
+ Misc:
121
+
122
+ - [#2176](https://github.com/rails-api/active_model_serializers/pull/2176) Documentation for global adapter config. (@mrpinsky)
123
+ - [#2215](https://github.com/rails-api/active_model_serializers/pull/2215) Update `serializers.md` documentation to denote alternate use cases for `scope`. (@stratigos)
124
+ - [#2212](https://github.com/rails-api/active_model_serializers/pull/2212) Remove legacy has_many_embed_ids test. (@bf4)
125
+
126
+ ### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6)
127
+
128
+ Fixes:
129
+
130
+ - [#1857](https://github.com/rails-api/active_model_serializers/pull/1857) JSON:API does not load belongs_to relation to get identifier id. (@bf4)
131
+ - [#2119](https://github.com/rails-api/active_model_serializers/pull/2119) JSON:API returns null resource object identifier when 'id' is null. (@bf4)
132
+ - [#2093](https://github.com/rails-api/active_model_serializers/pull/2093) undef problematic Serializer methods: display, select. (@bf4)
133
+
134
+ Misc:
135
+
136
+ - [#2104](https://github.com/rails-api/active_model_serializers/pull/2104) Documentation for serializers and rendering. (@cassidycodes)
137
+ - [#2081](https://github.com/rails-api/active_model_serializers/pull/2081) Documentation for `include` option in adapters. (@charlie-wasp)
138
+ - [#2120](https://github.com/rails-api/active_model_serializers/pull/2120) Documentation for association options: foreign_key, type, class_name, namespace. (@bf4)
139
+
140
+ ### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5)
141
+
142
+ Breaking changes:
143
+
144
+ Features:
145
+
146
+ - [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
147
+ - [#2057](https://github.com/rails-api/active_model_serializers/pull/2057)
148
+ Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.2']`
149
+ (@jaredbeck)
150
+
151
+ Fixes:
152
+
153
+ - [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
154
+
155
+ Misc:
156
+
157
+ - [#2055](https://github.com/rails-api/active_model_serializers/pull/2055)
158
+ Replace deprecated dependency jsonapi with jsonapi-renderer. (@jaredbeck)
159
+ - [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) Make test attributes explicit. Tests have Model#associations. (@bf4)
160
+ - [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
161
+ - [#2035](https://github.com/rails-api/active_model_serializers/pull/2035) Document how to disable the logger. (@MSathieu)
162
+ - [#2039](https://github.com/rails-api/active_model_serializers/pull/2039) Documentation fixes. (@biow0lf)
163
+
164
+ ### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4)
165
+
166
+ Misc:
167
+
168
+ - [#2005](https://github.com/rails-api/active_model_serializers/pull/2005) Update jsonapi runtime dependency to 0.1.1.beta6, support Ruby 2.4. (@kofronpi)
169
+ - [#1993](https://github.com/rails-api/active_model_serializers/pull/1993) Swap out KeyTransform for CaseTransform gem for the possibility of native extension use. (@NullVoxPopuli)
170
+
171
+ ### [v0.10.3 (2016-11-21)](https://github.com/rails-api/active_model_serializers/compare/v0.10.2...v0.10.3)
172
+
173
+ Fixes:
174
+
175
+ - [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh)
176
+ - [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
177
+ - [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
178
+ - [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)
179
+ - [#1930](https://github.com/rails-api/active_model_serializers/pull/1930) Ensure valid jsonapi when relationship has no links or data (@richmolj)
180
+
181
+ Features:
182
+
183
+ - [#1757](https://github.com/rails-api/active_model_serializers/pull/1757) Make serializer lookup chain configurable. (@NullVoxPopuli)
184
+ - [#1968](https://github.com/rails-api/active_model_serializers/pull/1968) (@NullVoxPopuli)
185
+ - Add controller namespace to default controller lookup
186
+ - Provide a `namespace` render option
187
+ - document how set the namespace in the controller for implicit lookup.
188
+ - [#1791](https://github.com/rails-api/active_model_serializers/pull/1791) (@bf4, @youroff, @NullVoxPopuli)
189
+ - Added `jsonapi_namespace_separator` config option.
190
+ - [#1889](https://github.com/rails-api/active_model_serializers/pull/1889) Support key transformation for Attributes adapter (@iancanderson, @danbee)
191
+ - [#1917](https://github.com/rails-api/active_model_serializers/pull/1917) Add `jsonapi_pagination_links_enabled` configuration option (@richmolj)
192
+ - [#1797](https://github.com/rails-api/active_model_serializers/pull/1797) Only include 'relationships' when sideloading (@richmolj)
193
+
194
+ Fixes:
195
+
196
+ - [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh)
197
+ - [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
198
+
199
+ Misc:
200
+ - [#1767](https://github.com/rails-api/active_model_serializers/pull/1767) Replace raising/rescuing `CollectionSerializer::NoSerializerError`,
201
+ throw/catch `:no_serializer`. (@bf4)
202
+ - [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)
203
+ - [#1812](https://github.com/rails-api/active_model_serializers/pull/1812) add a code of conduct (@corainchicago)
204
+
205
+ - [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
206
+
207
+ - [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@cassidycodes)
208
+ - [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
209
+ - [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono)
210
+ - [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe)
211
+
212
+ ### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
213
+
214
+ Fixes:
215
+ - [#1814](https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache
216
+ - [#1848](https://github.com/rails-api/active_model_serializers/pull/1848) Redefine associations on inherited serializers. (@EhsanYousefi)
217
+
218
+ Misc:
219
+ - [#1808](https://github.com/rails-api/active_model_serializers/pull/1808) Adds documentation for `fields` option. (@luizkowalski)
220
+
221
+ ### [v0.10.1 (2016-06-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...v0.10.1)
222
+
223
+ Features:
224
+ - [#1668](https://github.com/rails-api/active_model_serializers/pull/1668) Exclude nil and empty links. (@sigmike)
225
+ - [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact)
226
+
227
+ Fixes:
228
+ - [#1754](https://github.com/rails-api/active_model_serializers/pull/1754) Fixes #1759, Grape integration, improves serialization_context
229
+ missing error message on pagination. Document overriding CollectionSerializer#paginated?. (@bf4)
230
+ Moved serialization_context creation to Grape formatter, so resource serialization works without explicit calls to the `render` helper method.
231
+ Added Grape collection tests. (@onomated)
232
+ - [#1287](https://github.com/rails-api/active_model_serializers/pull/1287) Pass `fields` options from adapter to serializer. (@vasilakisfil)
233
+ - [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
234
+ is set to `false`. (@groyoh)
235
+ - [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear)
236
+
237
+ Misc:
238
+ - [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
239
+ - [#1685](https://github.com/rails-api/active_model_serializers/pull/1685) Replace `IncludeTree` with `IncludeDirective` from the jsonapi gem.
240
+
241
+ ### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0)
6
242
 
7
243
  Breaking changes:
8
244
  - [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
@@ -22,6 +258,7 @@ Fixes:
22
258
 
23
259
  Misc:
24
260
  - [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre)
261
+ - [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever)
25
262
 
26
263
  ### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5)
27
264
 
data/README.md CHANGED
@@ -4,8 +4,8 @@
4
4
  <tr>
5
5
  <td>Build Status</td>
6
6
  <td>
7
- <a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://travis-ci.org/rails-api/active_model_serializers.svg?branch=master" alt="Build Status" ></a>
8
- <a href="https://ci.appveyor.com/project/joaomdmoura/active-model-serializers/branch/master"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
7
+ <a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://api.travis-ci.org/rails-api/active_model_serializers.svg?branch=0-10-stable" alt="Build Status" ></a>
8
+ <a href="https://ci.appveyor.com/project/bf4/active-model-serializers/branch/0-10-stable"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
9
9
  </td>
10
10
  </tr>
11
11
  <tr>
@@ -13,7 +13,6 @@
13
13
  <td>
14
14
  <a href="https://codeclimate.com/github/rails-api/active_model_serializers"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg" alt="Code Quality"></a>
15
15
  <a href="https://codebeat.co/projects/github-com-rails-api-active_model_serializers"><img src="https://codebeat.co/badges/a9ab35fa-8b5a-4680-9d4e-a81f9a55ebcd" alt="codebeat" ></a>
16
- <a href="https://codeclimate.com/github/rails-api/active_model_serializers/coverage"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/coverage.svg" alt="Test Coverage"></a>
17
16
  </td>
18
17
  </tr>
19
18
  <tr>
@@ -24,17 +23,6 @@
24
23
  </tr>
25
24
  </table>
26
25
 
27
-
28
- ## Documentation
29
-
30
- - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
31
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
32
- - [Guides](docs)
33
- - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
34
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
35
- - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
36
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
37
-
38
26
  ## About
39
27
 
40
28
  ActiveModelSerializers brings convention over configuration to your JSON generation.
@@ -50,33 +38,22 @@ resource serialization. The serialization has the `#as_json`, `#to_json` and `#s
50
38
  methods used by the Rails JSON Renderer. (SerializableResource actually delegates
51
39
  these methods to the adapter.)
52
40
 
53
- By default ActiveModelSerializers will use the **Attributes Adapter**.
41
+ By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
54
42
  But we strongly advise you to use **JsonApi Adapter**, which
55
- follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
43
+ follows 1.0 of the format specified in [jsonapi.org/format](https://jsonapi.org/format).
56
44
  Check how to change the adapter in the sections below.
57
45
 
58
- ## RELEASE CANDIDATE, PLEASE READ
59
-
60
- This is the **master** branch of ActiveModelSerializers.
61
-
62
- It will become the `0.10.0` release when it's ready. Currently this is a release candidate.
63
-
64
46
  `0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
65
47
 
66
- `0.10.x` will be based on the `0.8.0` code, but with a more flexible
48
+ `0.10.x` is based on the `0.8.0` code, but with a more flexible
67
49
  architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
68
50
 
69
- It is generally safe and recommended to use the master branch.
70
-
71
- For more information, see the post '[The future of
72
- AMS](https://medium.com/@joaomdmoura/the-future-of-ams-e5f9047ca7e9)'.
73
-
74
51
  ## Installation
75
52
 
76
53
  Add this line to your application's Gemfile:
77
54
 
78
55
  ```
79
- gem 'active_model_serializers'
56
+ gem 'active_model_serializers', '~> 0.10.0'
80
57
  ```
81
58
 
82
59
  And then execute:
@@ -97,14 +74,36 @@ More information is available in the [Guides](docs) and
97
74
  If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new)
98
75
  and see our [contributing guide](CONTRIBUTING.md).
99
76
 
100
- If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
77
+ If you have a question, please [post to Stack Overflow](https://stackoverflow.com/questions/tagged/active-model-serializers).
101
78
 
102
- If you'd like to chat, we have a [community slack](http://amserializers.herokuapp.com).
79
+ If you'd like to chat, we have a [community slack](https://amserializers.herokuapp.com).
103
80
 
104
81
  Thanks!
105
82
 
83
+ ## Documentation
84
+
85
+ If you're reading this at https://github.com/rails-api/active_model_serializers you are
86
+ reading documentation for our `master`, which may include features that have not
87
+ been released yet. Please see below for the documentation relevant to you.
88
+
89
+ - [0.10 (0-10-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-10-stable)
90
+ - [0.10.10 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.10)
91
+ - [![API Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/active_model_serializers/0.10.10)
92
+ - [Guides](docs)
93
+ - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
94
+ - [![API Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/active_model_serializers/0.9.7)
95
+ - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
96
+ - [![API Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/active_model_serializers/0.8.4)
97
+
98
+
106
99
  ## High-level behavior
107
100
 
101
+ Choose an adapter from [adapters](lib/active_model_serializers/adapter):
102
+
103
+ ``` ruby
104
+ ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
105
+ ```
106
+
108
107
  Given a [serializable model](lib/active_model/serializer/lint.rb):
109
108
 
110
109
  ```ruby
@@ -114,7 +113,7 @@ class SomeResource < ActiveRecord::Base
114
113
  end
115
114
  # or
116
115
  class SomeResource < ActiveModelSerializers::Model
117
- attr_accessor :title, :body
116
+ attributes :title, :body
118
117
  end
119
118
  ```
120
119
 
@@ -160,8 +159,146 @@ serializer = SomeSerializer.new(resource, serializer_options)
160
159
  serializer.attributes
161
160
  serializer.associations
162
161
  ```
163
- See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
164
162
 
165
- # Contributing
163
+ ## Architecture
164
+
165
+ This section focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
166
+ please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
167
+ [0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
168
+
169
+ The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
170
+
171
+ ### ActiveModel::Serializer
172
+
173
+ An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/serialization.rb)
174
+ and exposes an `attributes` method, among a few others.
175
+ It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
176
+ It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
177
+ It may be useful to think of it as a
178
+ [presenter](https://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
179
+
180
+ #### ActiveModel::CollectionSerializer
181
+
182
+ The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
183
+ and, if there is no serializer, primitives.
184
+
185
+ ### ActiveModelSerializers::Adapter::Base
186
+
187
+ The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
188
+ serializer. For example, the `Attributes` example represents each serializer as its
189
+ unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
190
+ API](https://jsonapi.org/) document.
191
+
192
+ ### ActiveModelSerializers::SerializableResource
193
+
194
+ The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
195
+ to an object that responds to `to_json`, and `as_json`. It is used in the controller to
196
+ encapsulate the serialization resource when rendered. However, it can also be used on its own
197
+ to serialize a resource outside of a controller, as well.
198
+
199
+ ### Primitive handling
200
+
201
+ Definitions: A primitive is usually a String or Array. There is no serializer
202
+ defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
203
+ `to_json`). (The below also applies for any object with no serializer.)
204
+
205
+ - ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
206
+
207
+ Internally, if no serializer can be found in the controller, the resource is not decorated by
208
+ ActiveModelSerializers.
209
+
210
+ - However, when a primitive value is an attribute or in a collection, it is not modified.
211
+
212
+ When serializing a collection and the collection serializer (CollectionSerializer) cannot
213
+ identify a serializer for a resource in its collection, it throws [`:no_serializer`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128).
214
+ For example, when caught by `Reflection#build_association`, and the association value is set directly:
215
+
216
+ ```ruby
217
+ reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
218
+ ```
219
+
220
+ (which is called by the adapter as `serializer.associations(*)`.)
221
+
222
+ ### How options are parsed
223
+
224
+ High-level overview:
225
+
226
+ - For a **collection**
227
+ - `:serializer` specifies the collection serializer and
228
+ - `:each_serializer` specifies the serializer for each resource in the collection.
229
+ - For a **single resource**, the `:serializer` option is the resource serializer.
230
+ - Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
231
+ [`ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
232
+ The remaining options are serializer options.
233
+
234
+ Details:
235
+
236
+ 1. **ActionController::Serialization**
237
+ 1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
238
+ 1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
239
+ The `adapter_opts` keys are defined in [`ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
240
+ 1. **ActiveModelSerializers::SerializableResource**
241
+ 1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
242
+ - Where `serializer?` is `use_adapter? && !!(serializer)`
243
+ - Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
244
+ False when explicit adapter is falsy (nil or false)'
245
+ - Where `serializer`:
246
+ 1. from explicit `:serializer` option, else
247
+ 2. implicitly from resource `ActiveModel::Serializer.serializer_for(resource)`
248
+ 1. A side-effect of checking `serializer` is:
249
+ - The `:serializer` option is removed from the serializer_opts hash
250
+ - If the `:each_serializer` option is present, it is removed from the serializer_opts hash and set as the `:serializer` option
251
+ 1. The serializer and adapter are created as
252
+ 1. `serializer_instance = serializer.new(resource, serializer_opts)`
253
+ 2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
254
+ 1. **ActiveModel::Serializer::CollectionSerializer#new**
255
+ 1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
256
+ is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer/collection_serializer.rb#L77-L79).
257
+ 1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
258
+ resource as defined by the serializer.
259
+
260
+ (In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
261
+ methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
262
+ to know about, but not part of ActiveModelSerializers.)
263
+
264
+ ### What does a 'serializable resource' look like?
265
+
266
+ - An `ActiveRecord::Base` object.
267
+ - Any Ruby object that passes the
268
+ [Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
269
+ [(code)](lib/active_model/serializer/lint.rb).
270
+
271
+ ActiveModelSerializers provides a
272
+ [`ActiveModelSerializers::Model`](lib/active_model_serializers/model.rb),
273
+ which is a simple serializable PORO (Plain-Old Ruby Object).
274
+
275
+ `ActiveModelSerializers::Model` may be used either as a reference implementation, or in production code.
276
+
277
+ ```ruby
278
+ class MyModel < ActiveModelSerializers::Model
279
+ attributes :id, :name, :level
280
+ end
281
+ ```
282
+
283
+ The default serializer for `MyModel` would be `MyModelSerializer` whether MyModel is an
284
+ ActiveRecord::Base object or not.
285
+
286
+ Outside of the controller the rules are **exactly** the same as for records. For example:
287
+
288
+ ```ruby
289
+ render json: MyModel.new(level: 'awesome'), adapter: :json
290
+ ```
291
+
292
+ would be serialized the same as
293
+
294
+ ```ruby
295
+ ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
296
+ ```
297
+
298
+ ## Semantic Versioning
299
+
300
+ This project adheres to [semver](https://semver.org/)
301
+
302
+ ## Contributing
166
303
 
167
304
  See [CONTRIBUTING.md](CONTRIBUTING.md)
@@ -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
 
@@ -16,19 +18,37 @@ module ActionController
16
18
  included do
17
19
  class_attribute :_serialization_scope
18
20
  self._serialization_scope = :current_user
21
+
22
+ attr_writer :namespace_for_serializer
23
+ end
24
+
25
+ def namespace_for_serializer
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
19
35
  end
20
36
 
21
37
  def serialization_scope
22
- send(_serialization_scope) if _serialization_scope &&
23
- respond_to?(_serialization_scope, true)
38
+ return unless _serialization_scope && respond_to?(_serialization_scope, true)
39
+
40
+ send(_serialization_scope)
24
41
  end
25
42
 
26
43
  def get_serializer(resource, options = {})
27
- if !use_adapter?
44
+ unless use_adapter?
28
45
  warn 'ActionController::Serialization#use_adapter? has been removed. '\
29
46
  "Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
30
47
  options[:adapter] = false
31
48
  end
49
+
50
+ options.fetch(:namespace) { options[:namespace] = namespace_for_serializer }
51
+
32
52
  serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
33
53
  serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
34
54
  serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _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
@@ -7,9 +9,11 @@ module ActiveModel
7
9
  deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
8
10
  end
9
11
 
12
+ # :nocov:
10
13
  def initialize(serializer, options = {})
11
14
  super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
12
15
  end
16
+ # :nocov:
13
17
  end
14
18
  end
15
19
  end
@@ -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