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