deep_unrest 0.1.21 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/deep_unrest/application_controller.rb +1 -2
- data/lib/deep_unrest.rb +63 -51
- data/lib/deep_unrest/engine.rb +0 -1
- data/lib/deep_unrest/version.rb +1 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbbfefbc56611c2e591a4a0b4765ea55b8692208
|
4
|
+
data.tar.gz: eedb25cd0ab54ba479e8d54fba7170f1161708ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 458883abb589902b4958544173915d8783d4cfd9b99a1ad13a08bb75c54f1b6e1b6a8cf5985c3f9d07ca0d01ae1a8f92f672def3f9d80874fa0d03584e26f5ee
|
7
|
+
data.tar.gz: 503d64e14fc93803a813e45813a387f923b7af89a21e03dc65af0de3e488ef6d45eb4c2f451f0119541d934926d3ccf53d3f0755b2bc82eb971f170f8451bdf7
|
@@ -28,12 +28,11 @@ module DeepUnrest
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def update
|
31
|
-
@@temp_ids[request.uuid] = {}
|
32
31
|
redirect = allowed_params[:redirect]
|
33
32
|
data = repair_nested_params(allowed_params)[:data]
|
34
33
|
results = DeepUnrest.perform_update(request.uuid, data, current_user)
|
35
34
|
resp = { destroyed: results[:destroyed],
|
36
|
-
tempIds:
|
35
|
+
tempIds: results[:temp_ids] }
|
37
36
|
resp[:redirect] = results[:redirect_regex].call(redirect) if redirect
|
38
37
|
render json: resp, status: 200
|
39
38
|
rescue DeepUnrest::Unauthorized => err
|
data/lib/deep_unrest.rb
CHANGED
@@ -194,7 +194,11 @@ module DeepUnrest
|
|
194
194
|
idx = {}
|
195
195
|
params.map { |operation| collect_action_scopes(operation) }
|
196
196
|
.flatten
|
197
|
-
.
|
197
|
+
.each_with_object({}) do |op, memo|
|
198
|
+
# ensure no duplicate scopes
|
199
|
+
memo["#{op[:scope_type]}-#{op[:type]}-#{op[:id]}"] ||= {}
|
200
|
+
memo["#{op[:scope_type]}-#{op[:type]}-#{op[:id]}"].merge!(op)
|
201
|
+
end.values
|
198
202
|
.map do |op|
|
199
203
|
unless op[:scope_type] == :show
|
200
204
|
op[:index] = update_indices(idx, op[:type])[op[:type]] - 1
|
@@ -204,6 +208,7 @@ module DeepUnrest
|
|
204
208
|
end
|
205
209
|
|
206
210
|
def self.parse_id(id_str)
|
211
|
+
return false if id_str.nil?
|
207
212
|
id_match = id_str.match(/^\.?(?<id>\d+)$/)
|
208
213
|
id_match && id_match[:id]
|
209
214
|
end
|
@@ -256,13 +261,9 @@ module DeepUnrest
|
|
256
261
|
cursor
|
257
262
|
end
|
258
263
|
|
259
|
-
def self.get_mutation_cursor(
|
264
|
+
def self.get_mutation_cursor(memo, cursor, addr, type, id, temp_id, scope_type)
|
260
265
|
if memo
|
261
266
|
record = { id: id || temp_id }
|
262
|
-
if temp_id
|
263
|
-
record[:deep_unrest_temp_id] = temp_id
|
264
|
-
record[:deep_unrest_context] = ctx
|
265
|
-
end
|
266
267
|
if plural?(type)
|
267
268
|
cursor[addr] = [record]
|
268
269
|
next_cursor = cursor[addr][0]
|
@@ -277,8 +278,6 @@ module DeepUnrest
|
|
277
278
|
klass = to_class(type)
|
278
279
|
body = {}
|
279
280
|
body[klass.primary_key.to_sym] = id if id
|
280
|
-
body[:deep_unrest_temp_id] = temp_id if temp_id
|
281
|
-
body[:deep_unrest_context] = ctx if temp_id
|
282
281
|
cursor[type_sym] = {
|
283
282
|
klass: klass
|
284
283
|
}
|
@@ -295,47 +294,28 @@ module DeepUnrest
|
|
295
294
|
[memo, next_cursor]
|
296
295
|
end
|
297
296
|
|
298
|
-
def self.
|
299
|
-
mutations.each do |k, v|
|
300
|
-
case v
|
301
|
-
when Array
|
302
|
-
h = v.each_with_object({}) do |item, memo|
|
303
|
-
if item.respond_to?(:key?) && item.key?(:id)
|
304
|
-
# this is a nested resource. merge as such
|
305
|
-
memo[item[:id]] ||= {}
|
306
|
-
memo[item[:id]].deeper_merge(item)
|
307
|
-
merge_siblings!(item)
|
308
|
-
else
|
309
|
-
# otherwise this is just a normal array
|
310
|
-
idx = memo.keys.size
|
311
|
-
memo[idx] = item
|
312
|
-
end
|
313
|
-
end
|
314
|
-
mutations[k] = h.values
|
315
|
-
when Hash
|
316
|
-
merge_siblings!(v)
|
317
|
-
end
|
318
|
-
end
|
319
|
-
mutations
|
320
|
-
end
|
321
|
-
|
322
|
-
def self.remove_temp_ids!(mutations)
|
297
|
+
def self.convert_temp_ids!(ctx, mutations)
|
323
298
|
case mutations
|
324
299
|
when Hash
|
325
|
-
mutations.map do |key
|
300
|
+
mutations.keys.map do |key|
|
301
|
+
val = mutations[key]
|
326
302
|
if ['id', :id].include?(key)
|
327
|
-
|
303
|
+
unless parse_id(val)
|
304
|
+
mutations.delete(key)
|
305
|
+
mutations[:deep_unrest_temp_id] = val
|
306
|
+
mutations[:deep_unrest_context] = ctx
|
307
|
+
end
|
328
308
|
else
|
329
|
-
|
309
|
+
convert_temp_ids!(ctx, val)
|
330
310
|
end
|
331
311
|
end
|
332
312
|
when Array
|
333
|
-
mutations.map { |val|
|
313
|
+
mutations.map { |val| convert_temp_ids!(ctx, val) }
|
334
314
|
end
|
335
315
|
mutations
|
336
316
|
end
|
337
317
|
|
338
|
-
def self.build_mutation_fragment(
|
318
|
+
def self.build_mutation_fragment(op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil)
|
339
319
|
rest ||= parse_path(op[:path])
|
340
320
|
|
341
321
|
if rest.empty?
|
@@ -349,8 +329,7 @@ module DeepUnrest
|
|
349
329
|
scope_type = get_scope_type(id_str, rest.blank?, op[:destroy])
|
350
330
|
temp_id = scope_type == :create ? id_str : nil
|
351
331
|
|
352
|
-
memo, next_cursor = get_mutation_cursor(
|
353
|
-
memo,
|
332
|
+
memo, next_cursor = get_mutation_cursor(memo,
|
354
333
|
cursor,
|
355
334
|
addr,
|
356
335
|
type,
|
@@ -359,13 +338,42 @@ module DeepUnrest
|
|
359
338
|
scope_type)
|
360
339
|
|
361
340
|
next_cursor[:id] = id if id
|
362
|
-
build_mutation_fragment(
|
341
|
+
build_mutation_fragment(op, scopes, user, err_path_memo, rest, memo, next_cursor, type)
|
342
|
+
end
|
343
|
+
|
344
|
+
def self.combine_arrays(a, b)
|
345
|
+
# get list of dupe items
|
346
|
+
groups = (a + b).flatten.group_by { |item| item[:id] }
|
347
|
+
|
348
|
+
dupes = groups.select { |_, v| v.size > 1 }.values
|
349
|
+
|
350
|
+
# combine non-dupe items
|
351
|
+
non_dupes = groups.select { |_, v| v.size == 1 }.values
|
352
|
+
|
353
|
+
# deep_merge dupes
|
354
|
+
merged = dupes.map do |(a2, b2)|
|
355
|
+
a2.deep_merge(b2) do |_, a3, b3|
|
356
|
+
if a3.is_a? Array
|
357
|
+
combine_arrays(a3, b3)
|
358
|
+
else
|
359
|
+
b3
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
(non_dupes + merged).flatten
|
363
365
|
end
|
364
366
|
|
365
|
-
def self.build_mutation_body(
|
367
|
+
def self.build_mutation_body(ops, scopes, user)
|
366
368
|
err_path_memo = {}
|
367
369
|
ops.each_with_object(HashWithIndifferentAccess.new({})) do |op, memo|
|
368
|
-
memo.
|
370
|
+
memo.deep_merge!(build_mutation_fragment(op, scopes, user, err_path_memo)) do |key, a, b|
|
371
|
+
if a.is_a? Array
|
372
|
+
combine_arrays(a, b)
|
373
|
+
else
|
374
|
+
b
|
375
|
+
end
|
376
|
+
end
|
369
377
|
end
|
370
378
|
end
|
371
379
|
|
@@ -392,6 +400,7 @@ module DeepUnrest
|
|
392
400
|
when :destroy
|
393
401
|
item[:klass].destroy(id)
|
394
402
|
end
|
403
|
+
|
395
404
|
result = { record: record }
|
396
405
|
if action[:temp_id]
|
397
406
|
result[:temp_ids] = {}
|
@@ -480,6 +489,12 @@ module DeepUnrest
|
|
480
489
|
end
|
481
490
|
|
482
491
|
def self.perform_update(ctx, params, user)
|
492
|
+
temp_id_map = DeepUnrest::ApplicationController.class_variable_get(
|
493
|
+
'@@temp_ids'
|
494
|
+
)
|
495
|
+
|
496
|
+
temp_id_map[ctx] = {}
|
497
|
+
|
483
498
|
# reject new resources marked for destruction
|
484
499
|
viable_params = params.reject do |param|
|
485
500
|
temp_id?(param[:path]) && param[:destroy].present?
|
@@ -492,10 +507,10 @@ module DeepUnrest
|
|
492
507
|
DeepUnrest.authorization_strategy.authorize(scopes, user).flatten
|
493
508
|
|
494
509
|
# bulid update arguments
|
495
|
-
mutations = build_mutation_body(
|
510
|
+
mutations = build_mutation_body(viable_params, scopes, user)
|
496
511
|
|
497
|
-
|
498
|
-
|
512
|
+
# convert temp_ids from ids to non-activerecord attributes
|
513
|
+
convert_temp_ids!(ctx, mutations)
|
499
514
|
|
500
515
|
# perform update
|
501
516
|
results = mutate(mutations, user).flatten
|
@@ -507,12 +522,9 @@ module DeepUnrest
|
|
507
522
|
.compact
|
508
523
|
|
509
524
|
if errors.empty?
|
510
|
-
temp_ids = results.map { |res| res[:temp_ids] }
|
511
|
-
.compact
|
512
|
-
.each_with_object({}) { |item, mem| mem.merge!(item) }
|
513
|
-
|
514
525
|
return {
|
515
|
-
redirect_regex: build_redirect_regex(
|
526
|
+
redirect_regex: build_redirect_regex(temp_id_map[ctx]),
|
527
|
+
temp_ids: temp_id_map[ctx],
|
516
528
|
destroyed: scopes.select { |item| item[:destroyed] }
|
517
529
|
.map do |item|
|
518
530
|
{ type: item[:type],
|
data/lib/deep_unrest/engine.rb
CHANGED
@@ -3,7 +3,6 @@ require 'deep_unrest/authorization/none_strategy'
|
|
3
3
|
require 'deep_unrest/authorization/pundit_strategy'
|
4
4
|
require 'deep_unrest/concerns/null_concern'
|
5
5
|
require 'deep_unrest/concerns/map_temp_ids'
|
6
|
-
require 'deep_merge/rails_compat'
|
7
6
|
|
8
7
|
module DeepUnrest
|
9
8
|
class Engine < ::Rails::Engine
|
data/lib/deep_unrest/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_unrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.22
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lynn Hurley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 0.9.0
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: deep_merge
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 1.1.1
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 1.1.1
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: sqlite3
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|