deep_unrest 0.1.20 → 0.1.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 298848186d99b08b61138ee69e64f4b7e1533323
4
- data.tar.gz: 6e6e1d641b427c8bff316dddc5022645c4a0d9e3
3
+ metadata.gz: d956b080370fba60df6de844edcb1346763aa98f
4
+ data.tar.gz: 0126de558b0326faf98280030b1e6b32f8c208c0
5
5
  SHA512:
6
- metadata.gz: 294ef08f72dc4b7c75af39139be6860aca9728556e20856618a42bdd105f0e59a7559817ce6903f1a4a8cf64327fda14303cf44f49df91bd6b63af9a4a1495a7
7
- data.tar.gz: 412601b198372de42790ca4985737d3ee9f876703d595fb3741000bfcad7a55edd8dbda7e053697e06814008ed53b869639c1b5adc2de4f0d660d077ec19327a
6
+ metadata.gz: 096bde94abae1571026d1664dfec6fd87feac55aab27fbd3c2eeac4267f69926f0738b717289f769ff11b1b1b59daf768920117b47606545deab60391f8ad268
7
+ data.tar.gz: d3574d96731e97b160dcf32c3dd1d6d75c5eace3abc8bbdad93e1ebc163e35803e998f5489d7743bbae7ba24bfaf924949ca9185290bfdc72c7dd1aa14fd66cf
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepUnrest
2
4
  class ApplicationController < ActionController::API
3
5
  include DeepUnrest.authentication_concern
4
6
 
7
+ @@temp_ids = {}
8
+
5
9
  def context
6
10
  { current_user: current_user }
7
11
  end
@@ -24,10 +28,12 @@ module DeepUnrest
24
28
  end
25
29
 
26
30
  def update
31
+ @@temp_ids[request.uuid] = {}
27
32
  redirect = allowed_params[:redirect]
28
33
  data = repair_nested_params(allowed_params)[:data]
29
- results = DeepUnrest.perform_update(data, current_user)
30
- resp = { destroyed: results[:destroyed] }
34
+ results = DeepUnrest.perform_update(request.uuid, data, current_user)
35
+ resp = { destroyed: results[:destroyed],
36
+ tempIds: @@temp_ids[request.uuid] }
31
37
  resp[:redirect] = results[:redirect_regex].call(redirect) if redirect
32
38
  render json: resp, status: 200
33
39
  rescue DeepUnrest::Unauthorized => err
@@ -36,6 +42,8 @@ module DeepUnrest
36
42
  render json: err.message, status: 405
37
43
  rescue DeepUnrest::Conflict => err
38
44
  render json: err.message, status: 409
45
+ ensure
46
+ @@temp_ids.delete(request.uuid)
39
47
  end
40
48
 
41
49
  def current_user
data/lib/deep_unrest.rb CHANGED
@@ -240,7 +240,7 @@ module DeepUnrest
240
240
  end
241
241
 
242
242
  scope[:ar_error_key] = increment_error_indices(path_info, err_path_memo)
243
- scope[:dr_error_key] = path_info.map {|pair| pair.join('') }.join('.')
243
+ scope[:dr_error_key] = path_info.map { |pair| pair.join('') }.join('.')
244
244
 
245
245
  case action
246
246
  when :destroy
@@ -256,9 +256,13 @@ module DeepUnrest
256
256
  cursor
257
257
  end
258
258
 
259
- def self.get_mutation_cursor(memo, cursor, addr, type, id, temp_id, scope_type)
259
+ def self.get_mutation_cursor(ctx, memo, cursor, addr, type, id, temp_id, scope_type)
260
260
  if memo
261
261
  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
262
266
  if plural?(type)
263
267
  cursor[addr] = [record]
264
268
  next_cursor = cursor[addr][0]
@@ -273,6 +277,8 @@ module DeepUnrest
273
277
  klass = to_class(type)
274
278
  body = {}
275
279
  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
276
282
  cursor[type_sym] = {
277
283
  klass: klass
278
284
  }
@@ -329,7 +335,7 @@ module DeepUnrest
329
335
  mutations
330
336
  end
331
337
 
332
- def self.build_mutation_fragment(op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil)
338
+ def self.build_mutation_fragment(ctx, op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil)
333
339
  rest ||= parse_path(op[:path])
334
340
 
335
341
  if rest.empty?
@@ -343,7 +349,8 @@ module DeepUnrest
343
349
  scope_type = get_scope_type(id_str, rest.blank?, op[:destroy])
344
350
  temp_id = scope_type == :create ? id_str : nil
345
351
 
346
- memo, next_cursor = get_mutation_cursor(memo,
352
+ memo, next_cursor = get_mutation_cursor(ctx,
353
+ memo,
347
354
  cursor,
348
355
  addr,
349
356
  type,
@@ -352,44 +359,46 @@ module DeepUnrest
352
359
  scope_type)
353
360
 
354
361
  next_cursor[:id] = id if id
355
- build_mutation_fragment(op, scopes, user, err_path_memo, rest, memo, next_cursor, type)
362
+ build_mutation_fragment(ctx, op, scopes, user, err_path_memo, rest, memo, next_cursor, type)
356
363
  end
357
364
 
358
- def self.build_mutation_body(ops, scopes, user)
365
+ def self.build_mutation_body(ctx, ops, scopes, user)
359
366
  err_path_memo = {}
360
367
  ops.each_with_object(HashWithIndifferentAccess.new({})) do |op, memo|
361
- memo.deeper_merge(build_mutation_fragment(op, scopes, user, err_path_memo))
368
+ memo.deeper_merge(build_mutation_fragment(ctx, op, scopes, user, err_path_memo))
362
369
  end
363
370
  end
364
371
 
365
372
  def self.mutate(mutation, user)
366
- mutation.map do |_, item|
367
- item[:operations].map do |id, ops|
368
- ops.map do |_, action|
369
- record = case action[:method]
370
- when :update_all
371
- DeepUnrest.authorization_strategy
372
- .get_authorized_scope(user, item[:klass])
373
- .update(action[:body])
374
- nil
375
- when :destroy_all
376
- DeepUnrest.authorization_strategy
377
- .get_authorized_scope(user, item[:klass])
378
- .destroy_all
379
- nil
380
- when :update
381
- item[:klass].update(id, action[:body])
382
- when :create
383
- item[:klass].create(action[:body])
384
- when :destroy
385
- item[:klass].destroy(id)
386
- end
387
- result = { record: record }
388
- if action[:temp_id]
389
- result[:temp_ids] = {}
390
- result[:temp_ids][action[:temp_id]] = record.id
373
+ ActiveRecord::Base.transaction do
374
+ mutation.map do |_, item|
375
+ item[:operations].map do |id, ops|
376
+ ops.map do |_, action|
377
+ record = case action[:method]
378
+ when :update_all
379
+ DeepUnrest.authorization_strategy
380
+ .get_authorized_scope(user, item[:klass])
381
+ .update(action[:body])
382
+ nil
383
+ when :destroy_all
384
+ DeepUnrest.authorization_strategy
385
+ .get_authorized_scope(user, item[:klass])
386
+ .destroy_all
387
+ nil
388
+ when :update
389
+ item[:klass].update(id, action[:body])
390
+ when :create
391
+ item[:klass].create(action[:body])
392
+ when :destroy
393
+ item[:klass].destroy(id)
394
+ end
395
+ result = { record: record }
396
+ if action[:temp_id]
397
+ result[:temp_ids] = {}
398
+ result[:temp_ids][action[:temp_id]] = record.id
399
+ end
400
+ result
391
401
  end
392
- result
393
402
  end
394
403
  end
395
404
  end
@@ -470,7 +479,7 @@ module DeepUnrest
470
479
  record&.errors&.messages
471
480
  end
472
481
 
473
- def self.perform_update(params, user)
482
+ def self.perform_update(ctx, params, user)
474
483
  # reject new resources marked for destruction
475
484
  viable_params = params.reject do |param|
476
485
  temp_id?(param[:path]) && param[:destroy].present?
@@ -483,7 +492,7 @@ module DeepUnrest
483
492
  DeepUnrest.authorization_strategy.authorize(scopes, user).flatten
484
493
 
485
494
  # bulid update arguments
486
- mutations = build_mutation_body(viable_params, scopes, user)
495
+ mutations = build_mutation_body(ctx, viable_params, scopes, user)
487
496
 
488
497
  merge_siblings!(mutations)
489
498
  remove_temp_ids!(mutations)
@@ -0,0 +1,22 @@
1
+ module DeepUnrest
2
+ module Concerns
3
+ module MapTempIds
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_accessor :deep_unrest_context
8
+ attr_accessor :deep_unrest_temp_id
9
+ after_create :map_temp_id
10
+ end
11
+
12
+ def map_temp_id
13
+ # return unless @deep_unrest_temp_id
14
+ temp_id_map = DeepUnrest::ApplicationController.class_variable_get(
15
+ '@@temp_ids'
16
+ )
17
+ return unless temp_id_map && @deep_unrest_temp_id
18
+ temp_id_map[@deep_unrest_context][@deep_unrest_temp_id] = id
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,7 @@ module DeepUnrest
2
2
  module Concerns
3
3
  module NullConcern
4
4
  extend ActiveSupport::Concern
5
+
5
6
  included do
6
7
  before_action :issue_warning
7
8
  end
@@ -2,6 +2,7 @@ require 'deep_unrest/authorization/base_strategy'
2
2
  require 'deep_unrest/authorization/none_strategy'
3
3
  require 'deep_unrest/authorization/pundit_strategy'
4
4
  require 'deep_unrest/concerns/null_concern'
5
+ require 'deep_unrest/concerns/map_temp_ids'
5
6
  require 'deep_merge/rails_compat'
6
7
 
7
8
  module DeepUnrest
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeepUnrest
4
- VERSION = '0.1.20'
4
+ VERSION = '0.1.21'
5
5
  end
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.20
4
+ version: 0.1.21
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-07-14 00:00:00.000000000 Z
11
+ date: 2017-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -173,6 +173,7 @@ files:
173
173
  - lib/deep_unrest/authorization/base_strategy.rb
174
174
  - lib/deep_unrest/authorization/none_strategy.rb
175
175
  - lib/deep_unrest/authorization/pundit_strategy.rb
176
+ - lib/deep_unrest/concerns/map_temp_ids.rb
176
177
  - lib/deep_unrest/concerns/null_concern.rb
177
178
  - lib/deep_unrest/engine.rb
178
179
  - lib/deep_unrest/version.rb