@@ -16,6 +16,7 @@ class JsonApi < Base
16
16
# TODO: if we like this abstraction and other API objects to it,
17
17
# then extract to its own file and require it.
18
18
module ApiObjects
19
+ # {http://jsonapi.org/format/#document-jsonapi-object Jsonapi Object}
19
20
module Jsonapi
20
21
ActiveModelSerializers . config . jsonapi_version = '1.0'
21
22
ActiveModelSerializers . config . jsonapi_toplevel_meta = { }
@@ -54,6 +55,8 @@ def initialize(serializer, options = {})
54
55
@fieldset = options [ :fieldset ] || ActiveModel ::Serializer ::Fieldset . new ( options . delete ( :fields ) )
55
56
end
56
57
58
+ # {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
59
+ # {http://jsonapi.org/format/#document-top-level data and errors MUST NOT coexist in the same document.}
57
60
def serializable_hash ( options = nil )
58
61
options ||= { }
59
62
if serializer . success?
@@ -63,6 +66,7 @@ def serializable_hash(options = nil)
63
66
end
64
67
end
65
68
69
+ # {http://jsonapi.org/format/#document-top-level Primary data}
66
70
def success_document ( options )
67
71
is_collection = serializer . respond_to? ( :each )
68
72
serializers = is_collection ? serializer : [ serializer ]
@@ -87,6 +91,7 @@ def success_document(options)
87
91
hash
88
92
end
89
93
94
+ # {http://jsonapi.org/format/#errors JSON API Errors}
90
95
# TODO: look into caching
91
96
# rubocop:disable Style/AsciiComments
92
97
# definition:
@@ -120,6 +125,7 @@ def fragment_cache(cached_hash, non_cached_hash)
120
125
121
126
private
122
127
128
+ # {http://jsonapi.org/format/#document-resource-objects Primary data}
123
129
def resource_objects_for ( serializers )
124
130
@primary = [ ]
125
131
@included = [ ]
@@ -161,10 +167,12 @@ def process_relationship(serializer, include_tree)
161
167
process_relationships ( serializer , include_tree )
162
168
end
163
169
170
+ # {http://jsonapi.org/format/#document-resource-object-attributes Document Resource Object Attributes}
164
171
def attributes_for ( serializer , fields )
165
172
serializer . attributes ( fields ) . except ( :id )
166
173
end
167
174
175
+ # {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
168
176
def resource_object_for ( serializer )
169
177
resource_object = cache_check ( serializer ) do
170
178
resource_object = ApiObjects ::ResourceIdentifier . new ( serializer ) . as_json
@@ -188,6 +196,7 @@ def resource_object_for(serializer)
188
196
resource_object
189
197
end
190
198
199
+ # {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship}
191
200
def relationships_for ( serializer , requested_associations )
192
201
include_tree = IncludeTree . from_include_args ( requested_associations )
193
202
serializer . associations ( include_tree ) . each_with_object ( { } ) do |association , hash |
@@ -201,16 +210,19 @@ def relationships_for(serializer, requested_associations)
201
210
end
202
211
end
203
212
213
+ # {http://jsonapi.org/format/#document-links Document Links}
204
214
def links_for ( serializer )
205
215
serializer . _links . each_with_object ( { } ) do |( name , value ) , hash |
206
216
hash [ name ] = Link . new ( serializer , value ) . as_json
207
217
end
208
218
end
209
219
220
+ # {http://jsonapi.org/format/#fetching-pagination Pagination Links}
210
221
def pagination_links_for ( serializer , options )
211
222
JsonApi ::PaginationLinks . new ( serializer . object , options [ :serialization_context ] ) . serializable_hash ( options )
212
223
end
213
224
225
+ # {http://jsonapi.org/format/#document-meta Docment Meta}
214
226
def meta_for ( serializer )
215
227
Meta . new ( serializer ) . as_json
216
228
end
0 commit comments