-
Notifications
You must be signed in to change notification settings - Fork 81
/
formatters.rb
322 lines (300 loc) · 13.2 KB
/
formatters.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
module JSONAPI
module Utils
module Response
module Formatters
# Helper method to format ActiveRecord or Hash objects into JSON API-compliant ones.
#
# @note The return of this method represents what will actually be displayed in the response body.
# @note It can also be called as #jsonapi_serialize due to backward compatibility issues.
#
# @param object [ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>]
# Object to be formatted into JSON
# e.g.: User.first, User.all, { data: { id: 1, first_name: 'Tiago' } },
# [{ data: { id: 1, first_name: 'Tiago' } }]
#
# @option options [JSONAPI::Resource] resource: it tells the formatter which resource
# class to be used rather than use an infered one (default behaviour)
#
# @option options [JSONAPI::Resource] source: it tells the formatter that this response is from a related resource
# and the result should be interpreted as a related resources response
#
# @option options [String, Symbol] relationship_type: it tells that the formatter which relationship the data is from
#
# @option options [ActiveRecord::Base] model: ActiveRecord model class to be instantiated
# when a Hash or Array of Hashes is passed as the "object" argument
#
# @option options [Integer] count: if it's rendering a collection of resources, the default
# gem's counting method can be bypassed by the use of this options. It's shows then the total
# records resulting from that request and also calculates the pagination.
#
# @return [Hash]
#
# @api public
def jsonapi_format(object, options = {})
if object.is_a?(Hash)
hash = object.with_indifferent_access
object = hash_to_active_record(hash[:data], options[:model])
end
fix_custom_request_options(object)
build_response_document(object, options).contents
end
alias_method :jsonapi_serialize, :jsonapi_format
# Helper method to format ActiveRecord or any object that responds to #errors
# into JSON API-compliant error response bodies.
#
# @note The return of this method represents what will actually be displayed in the response body.
# @note It can also be called as #jsonapi_serialize_errors due to backward compatibility issues.
#
# @param object [ActiveRecord::Base or any object that responds to #errors]
# Error object to be serialized into JSON
# e.g.: User.new(name: nil).tap(&:save), MyErrorDecorator.new(invalid_object)
#
# @return [Array]
#
# @api public
def jsonapi_format_errors(object)
if active_record_obj?(object)
object = JSONAPI::Utils::Exceptions::ActiveRecord.new(object, @request.resource_klass, context)
end
errors = object.respond_to?(:errors) ? object.errors : object
JSONAPI::Utils::Support::Error.sanitize(errors).uniq
end
alias_method :jsonapi_serialize_errors, :jsonapi_format_errors
private
# Check whether the given object is an ActiveRecord-like one.
#
# @param object [Object] Object to be checked
#
# @return [TrueClass, FalseClass]
#
# @api private
def active_record_obj?(object)
defined?(ActiveRecord::Base) &&
(object.is_a?(ActiveRecord::Base) ||
object.singleton_class.include?(ActiveModel::Model))
end
# Build the full response document.
#
# @param object [ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>]
# Object to be formatted into JSON.
#
# @option options [JSONAPI::Resource] :resource which resource class to be used
# rather than using the default one (inferred)
#
# @option options [ActiveRecord::Base, JSONAPI::Resource] :source source of related resource,
# the result should be interpreted as a related resources response
#
# @option options [String, Symbol] :relationship which relationship the data is from
#
# @option options [Integer] count: if it's rendering a collection of resources, the default
# gem's counting method can be bypassed by the use of this options. It's shows then the total
# records resulting from that request and also calculates the pagination.
#
# @return [JSONAPI::ResponseDocument]
#
# @api private
def build_response_document(object, options)
results = JSONAPI::OperationResults.new
if object.respond_to?(:to_ary)
results.add_result(build_collection_result(object, options))
else
record = turn_into_resource(object, options)
results.add_result(JSONAPI::ResourceOperationResult.new(:ok, record))
end
@_response_document = create_response_document(results)
end
# Build the result operation object for collection actions.
#
# @param object [ActiveRecord::Relation, Array<Hash>]
# Object to be formatted into JSON.
#
# @option options [JSONAPI::Resource] :resource which resource class to be used
# rather than using the default one (inferred)
#
# @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/resource
# of the related resource
#
# @option options [String, Symbol] :relationship which relationship the data is from
#
# @option options [Integer] count: if it's rendering a collection of resources, the default
# gem's counting method can be bypassed by the use of this options. It's shows then the total
# records resulting from that request and also calculates the pagination.
#
# @return [JSONAPI::ResourcesOperationResult, JSONAPI::RelatedResourcesOperationResult]
#
# @api private
def build_collection_result(object, options)
records = build_collection(object, options)
result_options = result_options(object, options)
if related_resource_operation?(options)
source_resource = turn_source_into_resource(options[:source])
relationship_type = get_source_relationship(options)
JSONAPI::RelatedResourcesOperationResult.new(
:ok,
source_resource,
relationship_type,
records,
result_options
)
else
JSONAPI::ResourcesOperationResult.new(:ok, records, result_options)
end
end
# Is this a request for related resources?
#
# In order to answer that it needs to check for some {options}
# controller params like {params[:source]} and {params[:relationship]}.
#
# @option options [Boolean] :related when true, jsonapi-utils infers the parent and
# related resources from controller's {params} values.
#
# @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/resource
# of the related resource
#
# @option options [String, Symbol] :relationship which relationship the data is from
#
# @return [Boolean]
#
# @api private
def related_resource_operation?(options)
(options[:related] || options[:source].present?) &&
params[:source].present? &&
params[:relationship].present?
end
# Apply a proper action setup for custom requests/actions.
#
# @note The setup_(index|show)_action comes from JSONAPI::Resources' API.
#
# @param object [ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>]
# It's checked whether this object refers to a collection or not.
#
# @api private
def fix_custom_request_options(object)
return unless custom_get_request_with_params?
action = object.respond_to?(:to_ary) ? 'index' : 'show'
@request.send("setup_#{action}_action", params)
end
# Check whether it's a custom GET request with params.
#
# @return [TrueClass, FalseClass]
#
# @api private
def custom_get_request_with_params?
request.method =~ /get/i && !%w(index show).include?(params[:action]) && !params.nil?
end
# Turn a collection of AR or Hash objects into a collection of JSONAPI::Resource ones.
#
# @param records [ActiveRecord::Relation, Hash, Array<Hash>]
# Objects to be instantiated as JSONAPI::Resource ones.
# e.g.: User.all, [{ data: { id: 1, first_name: 'Tiago' } }]
#
# @option options [JSONAPI::Resource] :resource it resource class to be used rather than default one (infered)
#
# @option options [Integer] :count if it's rendering a collection of resources, the default
# gem's counting method can be bypassed by the use of this options. It's shows then the total
# records resulting from that request and also calculates the pagination.
#
# @return [Array]
#
# @api private
def build_collection(records, options)
records = apply_filter(records, options)
records = apply_sort(records)
records = apply_pagination(records, options)
records.respond_to?(:to_ary) ? records.map { |record| turn_into_resource(record, options) } : []
end
# Turn an AR or Hash object into a JSONAPI::Resource one.
#
# @param records [ActiveRecord::Relation, Hash, Array<Hash>]
# Object to be instantiated as a JSONAPI::Resource one.
# e.g.: User.first, { data: { id: 1, first_name: 'Tiago' } }
#
# @option options [JSONAPI::Resource] resource: it tells which resource
# class to be used rather than use an infered one (default behaviour)
#
# @return [JSONAPI::Resource]
#
# @api private
def turn_into_resource(record, options)
if options[:resource]
options[:resource].to_s.constantize.new(record, context)
else
@request.resource_klass.new(record, context)
end
end
# Get JSONAPI::Resource for source object
#
# @param record [ActiveRecord::Base, JSONAPI::Resource]
#
# @return [JSONAPI::Resource]
#
# @api private
def turn_source_into_resource(record)
return record if record.kind_of?(JSONAPI::Resource)
@request.source_klass.new(record, context)
end
# Get relationship type of source object
#
# @option options [Symbol] relationship: it tells which relationship
# to be used rather than use an infered one (default behaviour)
#
# @return [Symbol]
#
# @api private
def get_source_relationship(options)
options[:relationship]&.to_sym || @request.resource_klass._type
end
# Apply some result options like pagination params and record count to collection responses.
#
# @param records [ActiveRecord::Relation, Hash, Array<Hash>]
# Object to be formatted into JSON
# e.g.: User.all, [{ data: { id: 1, first_name: 'Tiago' } }]
#
# @option options [Integer] count: if it's rendering a collection of resources, the default
# gem's counting method can be bypassed by the use of this options. It's shows then the total
# records resulting from that request and also calculates the pagination.
#
# @return [Hash]
#
# @api private
def result_options(records, options)
{}.tap do |data|
if include_pagination_links?
data[:pagination_params] = pagination_params(records, options)
end
if JSONAPI.configuration.top_level_meta_include_record_count
data[:record_count] = record_count_for(records, options)
end
if include_page_count?
data[:page_count] = page_count_for(data[:record_count])
end
end
end
# Convert Hash or collection of Hashes into AR objects.
#
# @param data [Hash, Array<Hash>] Hash or collection to be converted
# e.g.: { data: { id: 1, first_name: 'Tiago' } },
# [{ data: { id: 1, first_name: 'Tiago' } }],
#
# @option options [ActiveRecord::Base] model: ActiveRecord model class to be
# used as base for the objects' intantialization.
#
# @return [ActiveRecord::Base, ActiveRecord::Relation]
#
# @api private
def hash_to_active_record(data, model)
return data if model.nil?
coerced = [data].flatten.map { |hash| model.new(hash) }
data.is_a?(Array) ? coerced : coerced.first
rescue ActiveRecord::UnknownAttributeError
if data.is_a?(Array)
ids = data.map { |e| e[:id] }
model.where(id: ids)
else
model.find_by(id: data[:id])
end
end
end
end
end
end