deep_unrest 0.1.16 → 0.1.17

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: e805e8e946507f8e9e3c7efc8699f45da9aeb6d1
4
- data.tar.gz: 24dbd8cd92cac4fd34ce96c0ee7234a20773ccd1
3
+ metadata.gz: cbd08967e61dac10c811ea4a4183108ef47a1069
4
+ data.tar.gz: 22fcd681e5e79ca74723fbd470d767dc9b5d9dd6
5
5
  SHA512:
6
- metadata.gz: e565d06c10f0d524d1f4e940ec125702704d28722ed86e8db399af34aacd08c5a2639e5fef76e544dbfd0809a515e9e6ee0ab7abcfa0bc178aa1439c6d774983
7
- data.tar.gz: 957d530761f55e9aa9b9392efa38181a4627354c6f38f129a3c8c83cc4992c47bf078adb11785b9d053a82b8525c1f9c6699ba298263a35448bc86919cb955fd
6
+ metadata.gz: 67dce499be10b554c4530694fb6fbb51c6aa119b58b1d81d5748fa4dd5339273bc63a6ee29b7fcc9ac54595780ef1e53aba00150f83823e07f007b85415c3632
7
+ data.tar.gz: bc4141b5c7e7d3c58f58562dd227e06b24deb1b63f6613d7ece5a20dab6b6a9047962c33393e0bd591384ecc1bd8da176ffa43bb4277d4ba40a753608fafcbaa
@@ -9,6 +9,7 @@ module DeepUnrest
9
9
  # rails can't deal with array indices in params (converts them to hashes)
10
10
  # see https://gist.github.com/bloudermilk/2884947
11
11
  def repair_nested_params(obj)
12
+ return unless obj.respond_to?(:each)
12
13
  obj.each do |key, value|
13
14
  if value.is_a?(ActionController::Parameters) || value.is_a?(Hash)
14
15
  # If any non-integer keys
@@ -25,10 +26,9 @@ module DeepUnrest
25
26
  def update
26
27
  redirect = allowed_params[:redirect]
27
28
  data = repair_nested_params(allowed_params)[:data]
28
- redirect_replace = DeepUnrest.perform_update(data,
29
- current_user)
30
- resp = {}
31
- resp[:redirect] = redirect_replace.call(redirect) if redirect
29
+ results = DeepUnrest.perform_update(data, current_user)
30
+ resp = { destroyed: results[:destroyed] }
31
+ resp[:redirect] = results[:redirect_regex].call(redirect) if redirect
32
32
  render json: resp, status: 200
33
33
  rescue DeepUnrest::Unauthorized => err
34
34
  render json: err.message, status: 403
data/lib/deep_unrest.rb CHANGED
@@ -94,7 +94,7 @@ module DeepUnrest
94
94
  end
95
95
 
96
96
  def self.temp_id?(str)
97
- /^\[[\w+\-]+\]$/.match(str)
97
+ /\[[\w+\-]+\]$/.match(str)
98
98
  end
99
99
 
100
100
  def self.plural?(s)
@@ -245,6 +245,7 @@ module DeepUnrest
245
245
  case action
246
246
  when :destroy
247
247
  cursor[:_destroy] = true
248
+ scope[:destroyed] = true
248
249
  when :update, :create, :update_all
249
250
  cursor.merge! parse_attributes(type,
250
251
  operation[:action],
@@ -461,14 +462,19 @@ module DeepUnrest
461
462
  end
462
463
 
463
464
  def self.perform_update(params, user)
465
+ # reject new resources marked for destruction
466
+ viable_params = params.reject do |param|
467
+ temp_id?(param[:path]) && param[:destroy]
468
+ end
469
+
464
470
  # identify requested scope(s)
465
- scopes = collect_all_scopes(params)
471
+ scopes = collect_all_scopes(viable_params)
466
472
 
467
473
  # authorize user for requested scope(s)
468
474
  DeepUnrest.authorization_strategy.authorize(scopes, user).flatten
469
475
 
470
476
  # bulid update arguments
471
- mutations = build_mutation_body(params, scopes, user)
477
+ mutations = build_mutation_body(viable_params, scopes, user)
472
478
 
473
479
  merge_siblings!(mutations)
474
480
  remove_temp_ids!(mutations)
@@ -486,7 +492,16 @@ module DeepUnrest
486
492
  temp_ids = results.map { |res| res[:temp_ids] }
487
493
  .compact
488
494
  .each_with_object({}) { |item, mem| mem.merge!(item) }
489
- return build_redirect_regex(temp_ids)
495
+
496
+ return {
497
+ redirect_regex: build_redirect_regex(temp_ids),
498
+ destroyed: scopes.select { |item| item[:destroyed] }
499
+ .map do |item|
500
+ { type: item[:type],
501
+ id: parse_id(item[:id]),
502
+ destroyed: true }
503
+ end
504
+ }
490
505
  end
491
506
 
492
507
  # map errors to their sources
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeepUnrest
4
- VERSION = '0.1.16'
4
+ VERSION = '0.1.17'
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.16
4
+ version: 0.1.17
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-06-15 00:00:00.000000000 Z
11
+ date: 2017-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails