active_model_serializers 0.10.0.rc4 → 0.10.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +30 -103
  7. data/.simplecov +0 -1
  8. data/.travis.yml +20 -8
  9. data/CHANGELOG.md +89 -5
  10. data/CONTRIBUTING.md +54 -179
  11. data/Gemfile +7 -2
  12. data/{LICENSE.txt → MIT-LICENSE} +0 -0
  13. data/README.md +27 -5
  14. data/Rakefile +44 -16
  15. data/active_model_serializers.gemspec +9 -1
  16. data/appveyor.yml +1 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +13 -7
  21. data/docs/README.md +5 -1
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +99 -16
  24. data/docs/general/configuration_options.md +87 -14
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/getting_started.md +35 -0
  27. data/docs/general/instrumentation.md +1 -1
  28. data/docs/general/key_transforms.md +40 -0
  29. data/docs/general/rendering.md +115 -13
  30. data/docs/general/serializers.md +138 -6
  31. data/docs/howto/add_pagination_links.md +36 -18
  32. data/docs/howto/outside_controller_use.md +4 -4
  33. data/docs/howto/passing_arbitrary_options.md +27 -0
  34. data/docs/jsonapi/errors.md +56 -0
  35. data/docs/jsonapi/schema.md +29 -18
  36. data/docs/rfcs/0000-namespace.md +106 -0
  37. data/docs/rfcs/template.md +15 -0
  38. data/lib/action_controller/serialization.rb +10 -19
  39. data/lib/active_model/serializable_resource.rb +4 -65
  40. data/lib/active_model/serializer.rb +73 -18
  41. data/lib/active_model/serializer/adapter.rb +15 -82
  42. data/lib/active_model/serializer/adapter/attributes.rb +5 -56
  43. data/lib/active_model/serializer/adapter/base.rb +5 -47
  44. data/lib/active_model/serializer/adapter/json.rb +6 -12
  45. data/lib/active_model/serializer/adapter/json_api.rb +5 -213
  46. data/lib/active_model/serializer/adapter/null.rb +7 -3
  47. data/lib/active_model/serializer/array_serializer.rb +3 -3
  48. data/lib/active_model/serializer/association.rb +4 -5
  49. data/lib/active_model/serializer/attributes.rb +1 -1
  50. data/lib/active_model/serializer/caching.rb +56 -5
  51. data/lib/active_model/serializer/collection_serializer.rb +30 -13
  52. data/lib/active_model/serializer/configuration.rb +7 -0
  53. data/lib/active_model/serializer/error_serializer.rb +10 -0
  54. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  55. data/lib/active_model/serializer/links.rb +4 -2
  56. data/lib/active_model/serializer/lint.rb +14 -0
  57. data/lib/active_model/serializer/meta.rb +29 -0
  58. data/lib/active_model/serializer/null.rb +17 -0
  59. data/lib/active_model/serializer/reflection.rb +57 -1
  60. data/lib/active_model/serializer/type.rb +1 -1
  61. data/lib/active_model/serializer/version.rb +1 -1
  62. data/lib/active_model_serializers.rb +17 -0
  63. data/lib/active_model_serializers/adapter.rb +92 -0
  64. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  65. data/lib/active_model_serializers/adapter/base.rb +90 -0
  66. data/lib/active_model_serializers/adapter/json.rb +11 -0
  67. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  68. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  69. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  70. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  71. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  72. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  73. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  74. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  75. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  76. data/lib/active_model_serializers/adapter/null.rb +10 -0
  77. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  78. data/lib/active_model_serializers/callbacks.rb +1 -1
  79. data/lib/active_model_serializers/deprecate.rb +55 -0
  80. data/lib/active_model_serializers/deserialization.rb +2 -2
  81. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  82. data/lib/active_model_serializers/json_pointer.rb +14 -0
  83. data/lib/active_model_serializers/key_transform.rb +70 -0
  84. data/lib/active_model_serializers/logging.rb +4 -1
  85. data/lib/active_model_serializers/model.rb +11 -1
  86. data/lib/active_model_serializers/railtie.rb +9 -1
  87. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  88. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  89. data/lib/active_model_serializers/serialization_context.rb +24 -2
  90. data/lib/active_model_serializers/test/schema.rb +2 -2
  91. data/lib/grape/formatters/active_model_serializers.rb +1 -1
  92. data/test/action_controller/adapter_selector_test.rb +1 -1
  93. data/test/action_controller/json_api/deserialization_test.rb +56 -3
  94. data/test/action_controller/json_api/errors_test.rb +41 -0
  95. data/test/action_controller/json_api/linked_test.rb +10 -9
  96. data/test/action_controller/json_api/pagination_test.rb +2 -2
  97. data/test/action_controller/json_api/transform_test.rb +180 -0
  98. data/test/action_controller/serialization_scope_name_test.rb +201 -35
  99. data/test/action_controller/serialization_test.rb +39 -7
  100. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  101. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  102. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  103. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  104. data/test/active_model_serializers/key_transform_test.rb +263 -0
  105. data/test/active_model_serializers/logging_test.rb +8 -8
  106. data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
  107. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  108. data/test/adapter/deprecation_test.rb +100 -0
  109. data/test/adapter/json/belongs_to_test.rb +32 -34
  110. data/test/adapter/json/collection_test.rb +73 -75
  111. data/test/adapter/json/has_many_test.rb +36 -38
  112. data/test/adapter/json/transform_test.rb +93 -0
  113. data/test/adapter/json_api/belongs_to_test.rb +127 -129
  114. data/test/adapter/json_api/collection_test.rb +80 -82
  115. data/test/adapter/json_api/errors_test.rb +78 -0
  116. data/test/adapter/json_api/fields_test.rb +68 -70
  117. data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
  118. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
  119. data/test/adapter/json_api/has_many_test.rb +121 -123
  120. data/test/adapter/json_api/has_one_test.rb +59 -61
  121. data/test/adapter/json_api/json_api_test.rb +28 -30
  122. data/test/adapter/json_api/linked_test.rb +319 -321
  123. data/test/adapter/json_api/links_test.rb +75 -50
  124. data/test/adapter/json_api/pagination_links_test.rb +115 -82
  125. data/test/adapter/json_api/parse_test.rb +114 -116
  126. data/test/adapter/json_api/relationship_test.rb +161 -0
  127. data/test/adapter/json_api/relationships_test.rb +199 -0
  128. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  129. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  130. data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
  131. data/test/adapter/json_api/transform_test.rb +500 -0
  132. data/test/adapter/json_api/type_test.rb +61 -0
  133. data/test/adapter/json_test.rb +35 -37
  134. data/test/adapter/null_test.rb +13 -15
  135. data/test/adapter/polymorphic_test.rb +72 -0
  136. data/test/adapter_test.rb +27 -29
  137. data/test/array_serializer_test.rb +7 -8
  138. data/test/benchmark/app.rb +65 -0
  139. data/test/benchmark/benchmarking_support.rb +67 -0
  140. data/test/benchmark/bm_caching.rb +117 -0
  141. data/test/benchmark/bm_transform.rb +34 -0
  142. data/test/benchmark/config.ru +3 -0
  143. data/test/benchmark/controllers.rb +77 -0
  144. data/test/benchmark/fixtures.rb +167 -0
  145. data/test/cache_test.rb +388 -0
  146. data/test/collection_serializer_test.rb +10 -0
  147. data/test/fixtures/active_record.rb +12 -0
  148. data/test/fixtures/poro.rb +28 -3
  149. data/test/grape_test.rb +5 -5
  150. data/test/lint_test.rb +9 -0
  151. data/test/serializable_resource_test.rb +59 -3
  152. data/test/serializers/associations_test.rb +8 -8
  153. data/test/serializers/attribute_test.rb +7 -7
  154. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  155. data/test/serializers/meta_test.rb +74 -6
  156. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  157. data/test/serializers/serialization_test.rb +55 -0
  158. data/test/support/isolated_unit.rb +3 -0
  159. data/test/support/rails5_shims.rb +26 -8
  160. data/test/support/rails_app.rb +38 -18
  161. data/test/support/serialization_testing.rb +5 -5
  162. data/test/test_helper.rb +6 -10
  163. metadata +132 -37
  164. data/docs/DESIGN.textile +7 -1
  165. data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
  166. data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
  167. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
  168. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
  169. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
  170. data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
  171. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
  172. data/test/active_model_serializers/serialization_context_test.rb +0 -18
  173. data/test/adapter/fragment_cache_test.rb +0 -38
  174. data/test/adapter/json_api/resource_type_config_test.rb +0 -71
  175. data/test/serializers/adapter_for_test.rb +0 -166
  176. data/test/serializers/cache_test.rb +0 -209
  177. data/test/support/simplecov.rb +0 -6
  178. data/test/support/stream_capture.rb +0 -50
  179. data/test/support/test_case.rb +0 -19
@@ -74,32 +74,30 @@ ActiveModelSerializers pagination relies on a paginated collection with the meth
74
74
 
75
75
  If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
76
76
 
77
- In your action specify a custom serializer.
77
+ Add this method to your base API controller.
78
+
78
79
  ```ruby
79
- render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
80
+ def pagination_dict(object)
81
+ {
82
+ current_page: object.current_page,
83
+ next_page: object.next_page,
84
+ prev_page: object.prev_page,
85
+ total_pages: object.total_pages,
86
+ total_count: object.total_count
87
+ }
88
+ end
80
89
  ```
81
90
 
82
- And then, you could do something like the following class.
91
+ Then, use it on your render method.
92
+
83
93
  ```ruby
84
- class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
85
- def initialize(object, options={})
86
- meta_key = options[:meta_key] || :meta
87
- options[meta_key] ||= {}
88
- options[meta_key] = {
89
- current_page: object.current_page,
90
- next_page: object.next_page,
91
- prev_page: object.prev_page,
92
- total_pages: object.total_pages,
93
- total_count: object.total_count
94
- }
95
- super(object, options)
96
- end
97
- end
94
+ render json: posts, meta: pagination_dict(posts)
98
95
  ```
96
+
99
97
  ex.
100
98
  ```json
101
99
  {
102
- "articles": [
100
+ "posts": [
103
101
  {
104
102
  "id": 2,
105
103
  "title": "JSON API paints my bikeshed!",
@@ -116,6 +114,26 @@ ex.
116
114
  }
117
115
  ```
118
116
 
117
+ You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer.
118
+
119
+ ```ruby
120
+ render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@post)
121
+ ```
122
+
123
+ ```ruby
124
+ #expects pagination!
125
+ def meta_attributes(resource, extra_meta = {})
126
+ {
127
+ current_page: resource.current_page,
128
+ next_page: resource.next_page,
129
+ prev_page: resource.prev_page,
130
+ total_pages: resource.total_pages,
131
+ total_count: resource.total_count
132
+ }.merge(extra_meta)
133
+ end
134
+ ```
135
+
136
+
119
137
  ### Attributes adapter
120
138
 
121
139
  This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.
@@ -14,7 +14,7 @@ post = Post.create(title: "Sample post", body: "I love Active Model Serializers!
14
14
  options = {}
15
15
 
16
16
  # Create a serializable resource instance
17
- serializable_resource = ActiveModel::SerializableResource.new(post, options)
17
+ serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
18
18
 
19
19
  # Convert your resource into json
20
20
  model_json = serializable_resource.as_json
@@ -38,20 +38,20 @@ serializer = ActiveModel::Serializer.serializer_for(post, options)
38
38
  You could also retrieve the serializer via:
39
39
 
40
40
  ```ruby
41
- ActiveModel::SerializableResource.new(post, options).serializer
41
+ ActiveModelSerializers::SerializableResource.new(post, options).serializer
42
42
  ```
43
43
 
44
44
  Both approaches will return an instance, if any, of the resource's serializer.
45
45
 
46
46
  ## Serializing before controller render
47
47
 
48
- At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
48
+ At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModelSerializers::SerializableResource` with
49
49
  the resource you want to be serialized and call `.as_json`.
50
50
 
51
51
  ```ruby
52
52
  def create
53
53
  message = current_user.messages.create!(message_params)
54
- message_json = ActiveModel::SerializableResource.new(message).as_json
54
+ message_json = ActiveModelSerializers::SerializableResource.new(message).as_json
55
55
  MessageCreationWorker.perform(message_json)
56
56
  head 204
57
57
  end
@@ -0,0 +1,27 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # Passing Arbitrary Options To A Serializer
4
+
5
+ In addition to the [`serialization_scope`](../general/serializers.md#scope), any options passed to `render`
6
+ that are not reserved for the [adapter](../general/rendering.md#adapter_opts)
7
+ are available in the serializer as [instance_options](../general/serializers.md#instance_options).
8
+
9
+ For example, we could pass in a field, such as `user_id` into our serializer.
10
+
11
+ ```ruby
12
+ # posts_controller.rb
13
+ class PostsController < ApplicationController
14
+ def dashboard
15
+ render json: @post, user_id: 12
16
+ end
17
+ end
18
+
19
+ # post_serializer.rb
20
+ class PostSerializer < ActiveModel::Serializer
21
+ attributes :id, :title, :body
22
+
23
+ def comments_by_me
24
+ Comments.where(user_id: instance_options[:user_id], post_id: object.id)
25
+ end
26
+ end
27
+ ```
@@ -0,0 +1,56 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # [JSON API Errors](http://jsonapi.org/format/#errors)
4
+
5
+ Rendering error documents requires specifying the error serializer(s):
6
+
7
+ - Serializer:
8
+ - For a single resource: `serializer: ActiveModel::Serializer::ErrorSerializer`.
9
+ - For a collection: `serializer: ActiveModel::Serializer::ErrorsSerializer`, `each_serializer: ActiveModel::Serializer::ErrorSerializer`.
10
+
11
+ The resource **MUST** have a non-empty associated `#errors` object.
12
+ The `errors` object must have a `#messages` method that returns a hash of error name to array of
13
+ descriptions.
14
+
15
+ ## Use in controllers
16
+
17
+ ```ruby
18
+ resource = Profile.new(name: 'Name 1',
19
+ description: 'Description 1',
20
+ comments: 'Comments 1')
21
+ resource.errors.add(:name, 'cannot be nil')
22
+ resource.errors.add(:name, 'must be longer')
23
+ resource.errors.add(:id, 'must be a uuid')
24
+
25
+ render json: resource, status: 422, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
26
+ # #=>
27
+ # { :errors =>
28
+ # [
29
+ # { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
30
+ # { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
31
+ # { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
32
+ # ]
33
+ # }.to_json
34
+ ```
35
+
36
+ ## Direct error document generation
37
+
38
+ ```ruby
39
+ options = nil
40
+ resource = ModelWithErrors.new
41
+ resource.errors.add(:name, 'must be awesome')
42
+
43
+ serializable_resource = ActiveModelSerializers::SerializableResource.new(
44
+ resource, {
45
+ serializer: ActiveModel::Serializer::ErrorSerializer,
46
+ adapter: :json_api
47
+ })
48
+ serializable_resource.as_json(options)
49
+ # #=>
50
+ # {
51
+ # :errors =>
52
+ # [
53
+ # { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
54
+ # ]
55
+ # }
56
+ ```
@@ -58,33 +58,33 @@ Example supported requests
58
58
  |-----------------------|----------------------------------------------------------------------------------------------------|----------|---------------------------------------|
59
59
  | schema | oneOf (success, failure, info) | |
60
60
  | success | data, included, meta, links, jsonapi | | AM::SerializableResource
61
- | success.meta | meta | | AM::S::Adapter::Base#meta
62
- | success.included | UniqueArray(resource) | | AM::S::Adapter::JsonApi#serializable_hash_for_collection
61
+ | success.meta | meta | | AMS::Adapter::Base#meta
62
+ | success.included | UniqueArray(resource) | | AMS::Adapter::JsonApi#serializable_hash_for_collection
63
63
  | success.data | data | |
64
- | success.links | allOf (links, pagination) | | AM::S::Adapter::JsonApi#links_for
64
+ | success.links | allOf (links, pagination) | | AMS::Adapter::JsonApi#links_for
65
65
  | success.jsonapi | jsonapi | |
66
- | failure | errors, meta, jsonapi | errors |
67
- | failure.errors | UniqueArray(error) | | #1004
68
- | meta | Object | |
69
- | data | oneOf (resource, UniqueArray(resource)) | | AM::S::Adapter::JsonApi#serializable_hash_for_collection,#serializable_hash_for_single_resource
66
+ | failure | errors, meta, jsonapi | errors | AMS::Adapter::JsonApi#failure_document, #1004
67
+ | failure.errors | UniqueArray(error) | | AM::S::ErrorSerializer, #1004
68
+ | meta | Object | |
69
+ | data | oneOf (resource, UniqueArray(resource)) | | AMS::Adapter::JsonApi#serializable_hash_for_collection,#serializable_hash_for_single_resource
70
70
  | resource | String(type), String(id),<br>attributes, relationships,<br>links, meta | type, id | AM::S::Adapter::JsonApi#primary_data_for
71
71
  | links | Uri(self), Link(related) | | #1028, #1246, #1282
72
72
  | link | oneOf (linkString, linkObject) | |
73
73
  | link.linkString | Uri | |
74
74
  | link.linkObject | Uri(href), meta | href |
75
- | attributes | patternProperties(<br>`"^(?!relationships$|links$)\\w[-\\w_]*$"`),<br>any valid JSON | | AM::Serializer#attributes, AM::S::Adapter::JsonApi#resource_object_for
76
- | relationships | patternProperties(<br>`"^\\w[-\\w_]*$"`);<br>links, relationships.data, meta | | AM::S::Adapter::JsonApi#relationships_for
77
- | relationships.data | oneOf (relationshipToOne, relationshipToMany) | | AM::S::Adapter::JsonApi#resource_identifier_for
75
+ | attributes | patternProperties(<br>`"^(?!relationships$|links$)\\w[-\\w_]*$"`),<br>any valid JSON | | AM::Serializer#attributes, AMS::Adapter::JsonApi#resource_object_for
76
+ | relationships | patternProperties(<br>`"^\\w[-\\w_]*$"`);<br>links, relationships.data, meta | | AMS::Adapter::JsonApi#relationships_for
77
+ | relationships.data | oneOf (relationshipToOne, relationshipToMany) | | AMS::Adapter::JsonApi#resource_identifier_for
78
78
  | relationshipToOne | anyOf(empty, linkage) | |
79
79
  | relationshipToMany | UniqueArray(linkage) | |
80
80
  | empty | null | |
81
- | linkage | String(type), String(id), meta | type, id | AM::S::Adapter::JsonApi#primary_data_for
82
- | pagination | pageObject(first), pageObject(last),<br>pageObject(prev), pageObject(next) | | AM::S::Adapter::JsonApi::PaginationLinks#serializable_hash
81
+ | linkage | String(type), String(id), meta | type, id | AMS::Adapter::JsonApi#primary_data_for
82
+ | pagination | pageObject(first), pageObject(last),<br>pageObject(prev), pageObject(next) | | AMS::Adapter::JsonApi::PaginationLinks#serializable_hash
83
83
  | pagination.pageObject | oneOf(Uri, null) | |
84
- | jsonapi | String(version), meta | | AM::S::Adapter::JsonApi::ApiObjects::JsonApi
85
- | error | String(id), links, String(status),<br>String(code), String(title),<br>String(detail), error.source, meta | |
86
- | error.source | String(pointer), String(parameter) | |
87
- | pointer | [JSON Pointer RFC6901](https://tools.ietf.org/html/rfc6901) | |
84
+ | jsonapi | String(version), meta | | AMS::Adapter::JsonApi::Jsonapi#as_json
85
+ | error | String(id), links, String(status),<br>String(code), String(title),<br>String(detail), error.source, meta | | AM::S::ErrorSerializer, AMS::Adapter::JsonApi::Error.resource_errors
86
+ | error.source | String(pointer), String(parameter) | | AMS::Adapter::JsonApi::Error.error_source
87
+ | pointer | [JSON Pointer RFC6901](https://tools.ietf.org/html/rfc6901) | | AMS::JsonPointer
88
88
 
89
89
 
90
90
  The [http://jsonapi.org/schema](schema/schema.json) makes a nice roadmap.
@@ -102,7 +102,7 @@ The [http://jsonapi.org/schema](schema/schema.json) makes a nice roadmap.
102
102
  ### Failure Document
103
103
 
104
104
  - [ ] failure
105
- - [ ] errors: array of unique items of type ` "$ref": "#/definitions/error"`
105
+ - [x] errors: array of unique items of type ` "$ref": "#/definitions/error"`
106
106
  - [ ] meta: `"$ref": "#/definitions/meta"`
107
107
  - [ ] jsonapi: `"$ref": "#/definitions/jsonapi"`
108
108
 
@@ -137,4 +137,15 @@ The [http://jsonapi.org/schema](schema/schema.json) makes a nice roadmap.
137
137
  - [ ] pagination
138
138
  - [ ] jsonapi
139
139
  - [ ] meta
140
- - [ ] error: id, links, status, code, title: detail: source [{pointer, type}, {parameter: {description, type}], meta
140
+ - [ ] error
141
+ - [ ] id: a unique identifier for this particular occurrence of the problem.
142
+ - [ ] links: a links object containing the following members:
143
+ - [ ] about: a link that leads to further details about this particular occurrence of the problem.
144
+ - [ ] status: the HTTP status code applicable to this problem, expressed as a string value.
145
+ - [ ] code: an application-specific error code, expressed as a string value.
146
+ - [ ] title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
147
+ - [x] detail: a human-readable explanation specific to this occurrence of the problem.
148
+ - [x] source: an object containing references to the source of the error, optionally including any of the following members:
149
+ - [x] pointer: a JSON Pointer [RFC6901](https://tools.ietf.org/html/rfc6901) to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute].
150
+ - [x] parameter: a string indicating which query parameter caused the error.
151
+ - [ ] meta: a meta object containing non-standard meta-information about the error.
@@ -0,0 +1,106 @@
1
+ - Start Date: (2015-10-29)
2
+ - RFC PR: https://github.com/rails-api/active_model_serializers/pull/1310
3
+ - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/1298
4
+
5
+ # Summary
6
+
7
+ Provide a consistent API for the user of the AMS.
8
+
9
+ # Motivation
10
+
11
+ The actual public API is defined under `ActiveModelSerializers`,
12
+ `ActiveModel::Serializer` and `ActiveModel`.
13
+
14
+ At the `ActiveModel::Serializer` we have:
15
+
16
+ - `ActiveModel::Serializer.config`
17
+ - `ActiveModel::Serializer`
18
+
19
+ At the `ActiveModelSerializers` we have:
20
+
21
+ - `ActiveModelSerializers::Model`
22
+ - `ActiveModelSerializers.logger`
23
+
24
+ At `ActiveModel` we have:
25
+
26
+ - `ActiveModel::SerializableResource`
27
+
28
+ The idea here is to provide a single namespace `ActiveModelSerializers` to the user.
29
+ Following the same idea we have on other gems like
30
+ [Devise](https://github.com/plataformatec/devise/blob/e9c82472ffe7c43a448945f77e034a0e47dde0bb/lib/devise.rb),
31
+ [Refile](https://github.com/refile/refile/blob/6b24c293d044862dafbf1bfa4606672a64903aa2/lib/refile.rb) and
32
+ [Active Job](https://github.com/rails/rails/blob/30bacc26f8f258b39e12f63fe52389a968d9c1ea/activejob/lib/active_job.rb)
33
+ for example.
34
+
35
+ This way we are clarifing the boundaries of
36
+ [ActiveModelSerializers and Rails](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md#prehistory)
37
+ and make clear that the `ActiveModel::Serializer` class is no longer the primary
38
+ behavior of the ActiveModelSerializers.
39
+
40
+ # Detailed design
41
+
42
+ ## New classes and modules organization
43
+
44
+ Since this will be a big change we can do this on baby steps, read small pull requests. A
45
+ possible approach is:
46
+
47
+ - All new code will be in `lib/active_model_serializers/` using
48
+ the module namespace `ActiveModelSerializers`.
49
+ - Move all content under `ActiveModel::Serializer` to be under
50
+ `ActiveModelSerializers`, the adapter is on this steps;
51
+ - Move all content under `ActiveModel` to be under `ActiveModelSerializers`,
52
+ the `SerializableResource` is on this step;
53
+ - Change all public API that doesn't make sense, keeping in mind only to keep
54
+ this in the same namespace
55
+ - Update the README;
56
+ - Update the docs;
57
+
58
+ The following table represents the current and the desired classes and modules
59
+ at the first moment.
60
+
61
+ | Current | Desired | Notes |
62
+ |--------------------------------------------------------|--------------------------------------------------|--------------------|
63
+ | `ActiveModelSerializers` and `ActiveModel::Serializer` | `ActiveModelSerializers` | The main namespace |
64
+ | `ActiveModelSerializers.logger` | `ActiveModelSerializers.logger` ||
65
+ | `ActiveModelSerializers::Model` | `ActiveModelSerializers::Model` ||
66
+ | `ActiveModel::SerializableResource` | `ActiveModelSerializers::SerializableResource` ||
67
+ | `ActiveModel::Serializer` | `ActiveModelSerializers::Serializer` | The name can be discussed in a future pull request. For example, we can rename this to `Resource` [following this idea](https://github.com/rails-api/active_model_serializers/pull/1301/files#r42963185) more info about naming in the next section|
68
+ | `ActiveModel::Serializer.config` | `ActiveModelSerializers.config` ||
69
+
70
+ ## Renaming of class and modules
71
+
72
+ When moving some content to the new namespace we can find some names that does
73
+ not make much sense like `ActiveModel::Serializer::Adapter::JsonApi`.
74
+ Discussion of renaming existing classes / modules and JsonApi objects will
75
+ happen in separate pull requests, and issues, and in the google doc
76
+ https://docs.google.com/document/d/1rcrJr0sVcazY2Opd_6Kmv1iIwuHbI84s1P_NzFn-05c/edit?usp=sharing
77
+
78
+ Some of names already have a definition.
79
+
80
+ - Adapters get their own namespace under ActiveModelSerializers. E.g
81
+ `ActiveModelSerializers::Adapter`
82
+ - Serializers get their own namespace under ActiveModelSerializers. E.g
83
+ `ActiveModelSerializers::Serializer`
84
+
85
+ ## Keeping compatibility
86
+
87
+ All moved classes or modules be aliased to their old name and location with
88
+ deprecation warnings, such as
89
+ [was done for CollectionSerializer](https://github.com/rails-api/active_model_serializers/pull/1251).
90
+
91
+ # Drawbacks
92
+
93
+ This will be a breaking change, so all users serializers will be broken after a
94
+ major bump.
95
+ All pull requests will need to rebase since the architeture will change a lot.
96
+
97
+ # Alternatives
98
+
99
+ We can keep the way it is, and keep in mind to not add another namespace as a
100
+ public API.
101
+
102
+ # Unresolved questions
103
+
104
+ What is the better class name to be used to the class that will be inherited at
105
+ the creation of a serializer. This can be discussed in other RFC or directly via
106
+ pull request.
@@ -0,0 +1,15 @@
1
+ - Start Date: (YYYY-MM-DD)
2
+ - RFC PR: https://github.com/rails-api/active_model_serializers/pull/dddd
3
+ - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/dddd
4
+
5
+ # Summary
6
+
7
+ # Motivation
8
+
9
+ # Detailed design
10
+
11
+ # Drawbacks
12
+
13
+ # Alternatives
14
+
15
+ # Unresolved questions
@@ -7,9 +7,6 @@ module ActionController
7
7
 
8
8
  include ActionController::Renderers
9
9
 
10
- # Deprecated
11
- ADAPTER_OPTION_KEYS = ActiveModel::SerializableResource::ADAPTER_OPTION_KEYS
12
-
13
10
  module ClassMethods
14
11
  def serialization_scope(scope)
15
12
  self._serialization_scope = scope
@@ -32,21 +29,13 @@ module ActionController
32
29
  "Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
33
30
  options[:adapter] = false
34
31
  end
35
- serializable_resource = ActiveModel::SerializableResource.new(resource, options)
36
- if serializable_resource.serializer?
37
- serializable_resource.serialization_scope ||= serialization_scope
38
- serializable_resource.serialization_scope_name = _serialization_scope
39
- begin
40
- # Necessary to ensure we have an adapter for the serializable resource
41
- # after it has been figured.
42
- # TODO: This logic should be less opaque and probably moved into the SerializableResource.
43
- serializable_resource.tap(&:adapter)
44
- rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
45
- resource
46
- end
47
- else
48
- resource
49
- end
32
+ serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
33
+ serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
34
+ serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
35
+ # For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
36
+ # Otherwise, since `serializable_resource` is not a string, the renderer would call
37
+ # `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
38
+ serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
50
39
  end
51
40
 
52
41
  # Deprecated
@@ -56,7 +45,9 @@ module ActionController
56
45
 
57
46
  [:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
58
47
  define_method renderer_method do |resource, options|
59
- options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
48
+ options.fetch(:serialization_context) do
49
+ options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request, options)
50
+ end
60
51
  serializable_resource = get_serializer(resource, options)
61
52
  super(serializable_resource, options)
62
53
  end
@@ -1,72 +1,11 @@
1
1
  require 'set'
2
+
2
3
  module ActiveModel
3
4
  class SerializableResource
4
- ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
5
- include ActiveModelSerializers::Logging
6
-
7
- delegate :serializable_hash, :as_json, :to_json, to: :adapter
8
- notify :serializable_hash, :render
9
- notify :as_json, :render
10
- notify :to_json, :render
11
-
12
- # Primary interface to composing a resource with a serializer and adapter.
13
- # @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
14
- def initialize(resource, options = {})
15
- @resource = resource
16
- @adapter_opts, @serializer_opts =
17
- options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
18
- end
19
-
20
- def serialization_scope=(scope)
21
- serializer_opts[:scope] = scope
22
- end
5
+ class << self
6
+ extend ActiveModelSerializers::Deprecate
23
7
 
24
- def serialization_scope
25
- serializer_opts[:scope]
8
+ delegate_and_deprecate :new, ActiveModelSerializers::SerializableResource
26
9
  end
27
-
28
- def serialization_scope_name=(scope_name)
29
- serializer_opts[:scope_name] = scope_name
30
- end
31
-
32
- def adapter
33
- @adapter ||= ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)
34
- end
35
- alias_method :adapter_instance, :adapter
36
-
37
- def serializer_instance
38
- @serializer_instance ||= serializer.new(resource, serializer_opts)
39
- end
40
-
41
- # Get serializer either explicitly :serializer or implicitly from resource
42
- # Remove :serializer key from serializer_opts
43
- # Replace :serializer key with :each_serializer if present
44
- def serializer
45
- @serializer ||=
46
- begin
47
- @serializer = serializer_opts.delete(:serializer)
48
- @serializer ||= ActiveModel::Serializer.serializer_for(resource)
49
-
50
- if serializer_opts.key?(:each_serializer)
51
- serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
52
- end
53
- @serializer
54
- end
55
- end
56
- alias_method :serializer_class, :serializer
57
-
58
- # True when no explicit adapter given, or explicit appear is truthy (non-nil)
59
- # False when explicit adapter is falsy (nil or false)
60
- def use_adapter?
61
- !(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
62
- end
63
-
64
- def serializer?
65
- use_adapter? && !!(serializer)
66
- end
67
-
68
- protected
69
-
70
- attr_reader :resource, :adapter_opts, :serializer_opts
71
10
  end
72
11
  end