deep_unrest 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/deep_unrest/application_controller.rb +2 -1
- data/lib/deep_unrest/version.rb +1 -1
- data/lib/deep_unrest.rb +58 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fab8d7d197527284f4b4dda948e4cde5b294b3d3
|
4
|
+
data.tar.gz: 4fab933a2b1e362d73b861c0be91b8144d85a1f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e743c5b618da969f16f9ba25c612f62a5a23c5f1310754ef6f0613f22ac753e9b0d6d9657da0a8fe1b941ba02cec63511edea457a77f71f7241088d1affbfaf1
|
7
|
+
data.tar.gz: b136c57f3520d945653fb41930635e0081a233f5c607a7fe1a15e6f12fa09d573b2299f4317199a90fc98629b0911f8b365a585f403d0878bfb252d9915c89db
|
@@ -29,7 +29,6 @@ module DeepUnrest
|
|
29
29
|
current_user)
|
30
30
|
resp = {}
|
31
31
|
resp[:redirect] = redirect_replace.call(redirect) if redirect
|
32
|
-
response.headers.merge! update_auth_header
|
33
32
|
render json: resp, status: 200
|
34
33
|
rescue DeepUnrest::Unauthorized => err
|
35
34
|
render json: err.message, status: 403
|
@@ -37,6 +36,8 @@ module DeepUnrest
|
|
37
36
|
render json: err.message, status: 405
|
38
37
|
rescue DeepUnrest::Conflict => err
|
39
38
|
render json: err.message, status: 409
|
39
|
+
ensure
|
40
|
+
response.headers.merge! update_auth_header
|
40
41
|
end
|
41
42
|
|
42
43
|
def current_user
|
data/lib/deep_unrest/version.rb
CHANGED
data/lib/deep_unrest.rb
CHANGED
@@ -188,9 +188,26 @@ module DeepUnrest
|
|
188
188
|
id_match && id_match[:id]
|
189
189
|
end
|
190
190
|
|
191
|
-
def self.
|
191
|
+
def self.increment_error_indices(path_info, memo)
|
192
|
+
path_info.each_with_index.map do |(type, id), i|
|
193
|
+
next if i.zero?
|
194
|
+
parent_type, parent_id = path_info[i - 1]
|
195
|
+
key = "#{parent_type}#{parent_id}"
|
196
|
+
memo[key] = [] unless memo[key]
|
197
|
+
idx = memo[key].find_index(id)
|
198
|
+
unless idx
|
199
|
+
idx = memo[key].size
|
200
|
+
memo[key] << id
|
201
|
+
end
|
202
|
+
|
203
|
+
"#{type}[#{idx}]"
|
204
|
+
end.compact.join('.')
|
205
|
+
end
|
206
|
+
|
207
|
+
def self.set_action(cursor, operation, type, user, scopes, err_path_memo)
|
192
208
|
# TODO: this is horrible. find a better way to go about this
|
193
|
-
|
209
|
+
path_info = parse_path(operation[:path])
|
210
|
+
id_str = path_info.last[1]
|
194
211
|
id = parse_id(id_str)
|
195
212
|
action = get_scope_type(id_str,
|
196
213
|
true,
|
@@ -198,6 +215,13 @@ module DeepUnrest
|
|
198
215
|
|
199
216
|
cursor[:id] = id || id_str
|
200
217
|
|
218
|
+
scope = scopes.find do |s|
|
219
|
+
s[:type] == type && s[:id] == id_str
|
220
|
+
end
|
221
|
+
|
222
|
+
scope[:ar_error_key] = increment_error_indices(path_info, err_path_memo)
|
223
|
+
scope[:dr_error_key] = path_info.map {|pair| pair.join('') }.join('.')
|
224
|
+
|
201
225
|
case action
|
202
226
|
when :destroy
|
203
227
|
cursor[:_destroy] = true
|
@@ -207,6 +231,7 @@ module DeepUnrest
|
|
207
231
|
operation[:attributes],
|
208
232
|
user)
|
209
233
|
end
|
234
|
+
|
210
235
|
cursor
|
211
236
|
end
|
212
237
|
|
@@ -276,11 +301,11 @@ module DeepUnrest
|
|
276
301
|
mutations
|
277
302
|
end
|
278
303
|
|
279
|
-
def self.build_mutation_fragment(op, user, rest = nil, memo = nil, cursor = nil, type = nil)
|
304
|
+
def self.build_mutation_fragment(op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil)
|
280
305
|
rest ||= parse_path(op[:path])
|
281
306
|
|
282
307
|
if rest.empty?
|
283
|
-
set_action(cursor, op, type, user)
|
308
|
+
set_action(cursor, op, type, user, scopes, err_path_memo)
|
284
309
|
return memo
|
285
310
|
end
|
286
311
|
|
@@ -299,12 +324,13 @@ module DeepUnrest
|
|
299
324
|
scope_type)
|
300
325
|
|
301
326
|
next_cursor[:id] = id if id
|
302
|
-
build_mutation_fragment(op, user, rest, memo, next_cursor, type)
|
327
|
+
build_mutation_fragment(op, scopes, user, err_path_memo, rest, memo, next_cursor, type)
|
303
328
|
end
|
304
329
|
|
305
|
-
def self.build_mutation_body(ops, user)
|
330
|
+
def self.build_mutation_body(ops, scopes, user)
|
331
|
+
err_path_memo = {}
|
306
332
|
ops.each_with_object(HashWithIndifferentAccess.new({})) do |op, memo|
|
307
|
-
memo.deeper_merge(build_mutation_fragment(op, user))
|
333
|
+
memo.deeper_merge(build_mutation_fragment(op, scopes, user, err_path_memo))
|
308
334
|
end
|
309
335
|
end
|
310
336
|
|
@@ -342,17 +368,30 @@ module DeepUnrest
|
|
342
368
|
end
|
343
369
|
|
344
370
|
def self.parse_error_path(key)
|
345
|
-
rx =
|
371
|
+
rx = /^(?<path>.*\])?\.?(?<field>[\w\-\.]+)$/
|
346
372
|
rx.match(key)
|
347
373
|
end
|
348
374
|
|
349
375
|
def self.format_errors(operation, path_info, values)
|
350
376
|
if operation
|
351
377
|
return values.map do |msg|
|
352
|
-
base_path =
|
378
|
+
base_path = (
|
379
|
+
operation[:error_path] ||
|
380
|
+
operation[:dr_error_key] ||
|
381
|
+
operation[:ar_error_key]
|
382
|
+
)
|
383
|
+
# TODO: case field name according to jsonapi_resources settings
|
384
|
+
field_name = path_info[:field].camelize(:lower)
|
385
|
+
pointer = [base_path, field_name].compact.join('.')
|
386
|
+
active_record_path = [operation[:ar_error_key],
|
387
|
+
field_name].reject(&:empty?).compact.join('.')
|
388
|
+
deep_unrest_path = [operation[:dr_error_key],
|
389
|
+
field_name].compact.join('.')
|
353
390
|
{ title: "#{path_info[:field].humanize} #{msg}",
|
354
391
|
detail: msg,
|
355
|
-
source: { pointer:
|
392
|
+
source: { pointer: pointer,
|
393
|
+
deepUnrestPath: deep_unrest_path,
|
394
|
+
activeRecordPath: active_record_path } }
|
356
395
|
end
|
357
396
|
end
|
358
397
|
values.map do |msg|
|
@@ -365,10 +404,12 @@ module DeepUnrest
|
|
365
404
|
errors.map do |key, values|
|
366
405
|
path_info = parse_error_path(key.to_s)
|
367
406
|
operation = scopes.find do |s|
|
368
|
-
(
|
369
|
-
|
407
|
+
(
|
408
|
+
s[:ar_error_key] &&
|
409
|
+
s[:ar_error_key] == (path_info[:path] || '') &&
|
410
|
+
s[:scope_type] != :show
|
411
|
+
)
|
370
412
|
end
|
371
|
-
|
372
413
|
format_errors(operation, path_info, values)
|
373
414
|
end
|
374
415
|
end.flatten
|
@@ -387,16 +428,9 @@ module DeepUnrest
|
|
387
428
|
end
|
388
429
|
end
|
389
430
|
|
390
|
-
def self.format_error_keys(res
|
431
|
+
def self.format_error_keys(res)
|
391
432
|
record = res[:record]
|
392
|
-
|
393
|
-
if errors
|
394
|
-
base_key = "#{record.class.to_s.camelize(:lower).pluralize}[#{i}]"
|
395
|
-
errors.keys.map do |attr|
|
396
|
-
errors["#{base_key}.#{attr}".to_sym] = errors.delete(attr)
|
397
|
-
end
|
398
|
-
end
|
399
|
-
errors
|
433
|
+
record&.errors&.messages
|
400
434
|
end
|
401
435
|
|
402
436
|
def self.perform_update(params, user)
|
@@ -407,7 +441,7 @@ module DeepUnrest
|
|
407
441
|
DeepUnrest.authorization_strategy.authorize(scopes, user).flatten
|
408
442
|
|
409
443
|
# bulid update arguments
|
410
|
-
mutations = build_mutation_body(params, user)
|
444
|
+
mutations = build_mutation_body(params, scopes, user)
|
411
445
|
|
412
446
|
merge_siblings!(mutations)
|
413
447
|
remove_temp_ids!(mutations)
|
@@ -416,8 +450,7 @@ module DeepUnrest
|
|
416
450
|
results = mutate(mutations, user).flatten
|
417
451
|
|
418
452
|
# check results for errors
|
419
|
-
errors = results.
|
420
|
-
.map { |res, i| format_error_keys(res, i) }
|
453
|
+
errors = results.map { |res| format_error_keys(res) }
|
421
454
|
.compact
|
422
455
|
.reject(&:empty?)
|
423
456
|
.compact
|