deep_unrest 0.1.64 → 0.1.69

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
  SHA256:
3
- metadata.gz: 2455324c4b8f985d12776c58a397e719261a8a67eed3de7d713527127d6e412d
4
- data.tar.gz: 3bd44536c8a3a861d0ff9fbf0d11d3dac75a5bfd898e1cdcfff83d076ab3eb03
3
+ metadata.gz: ca959dddbde430d68949a501d37ebd9b995dfd390849fdbe40ebc53a9dc42a72
4
+ data.tar.gz: 9491ceca33bc4f4c6b0cacd5dee5f0dd2377ca2f3e9d00c035f7efd3f0fa9033
5
5
  SHA512:
6
- metadata.gz: 1e2f633f77b6a5f34277a09280c6017f452c4d42f6cd6da86ed4569c09ad97b175c478c8a707457ba23f1ec4c9a21002df35349957b8d99ca2425664bf3d3ba9
7
- data.tar.gz: 675b09eea9fd593ee92509d001d2d411506044be2e79e50ca456ff363dd7cbb430d67fa412168565269088171c89a3cd6136f7f2eccc29a82b55389ecb6e27a6
6
+ metadata.gz: 5b7ea668419b1b53ff670a633657424a140105c3028d6bd32fd48b39285c6749500545e8ee0ec284da6c9d0c2dec0544ed632520b4f7bf619d0433e6d8e80ea9
7
+ data.tar.gz: '0220088c448da93dbbba7ebc8c43312b6734e2a494a9df4d65017e0b599ba5a100d848e4c01b1367269019c2a6eae008f0f7be57d5b32f7649de1b8062fb2a8e'
@@ -51,6 +51,7 @@ module DeepUnrest
51
51
  def write
52
52
  repaired_params = repair_nested_params(params[:data])
53
53
  data = repaired_params[:data]
54
+ data = ActionController::Parameters.new({}) if data&.blank?
54
55
  context = repaired_params[:context] || {}
55
56
  context[:uuid] = request.uuid
56
57
  context[:current_user] = current_user
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  DeepUnrest::Engine.routes.draw do
2
2
  patch 'update', to: 'application#update'
3
3
  get 'read', to: 'application#read'
4
+ patch 'read', to: 'application#read'
4
5
  patch 'write', to: 'application#write'
5
6
  end
data/lib/deep_unrest.rb CHANGED
@@ -162,7 +162,7 @@ module DeepUnrest
162
162
  def self.parse_attributes(type, scope_type, attributes, user)
163
163
  p = JSONAPI::RequestParser.new
164
164
  resource = get_resource(type)
165
- p.resource_klass = resource
165
+ p.source_klass = resource
166
166
  ctx = { current_user: user }
167
167
  opts = if scope_type == :create
168
168
  resource.creatable_fields(ctx)
@@ -170,7 +170,7 @@ module DeepUnrest
170
170
  resource.updatable_fields(ctx)
171
171
  end
172
172
 
173
- p.parse_params({ attributes: attributes }, opts)[:attributes]
173
+ p.parse_params(resource, { attributes: attributes }, opts)[:attributes]
174
174
  rescue JSONAPI::Exceptions::ParameterNotAllowed
175
175
  unpermitted_keys = attributes.keys.map(&:to_sym) - opts
176
176
  msg = "Attributes #{unpermitted_keys} of #{type.classify} not allowed"
@@ -216,7 +216,7 @@ module DeepUnrest
216
216
  end
217
217
 
218
218
  def self.parse_id(id_str)
219
- return unless id_str.is_a? String
219
+ return unless id_str.is_a?(String) || id_str.is_a?(Integer)
220
220
  return false if id_str.nil?
221
221
  return id_str if id_str.is_a? Integer
222
222
 
@@ -558,6 +558,19 @@ module DeepUnrest
558
558
  DeepUnrest::Write.write(ctx, params, user)
559
559
  end
560
560
 
561
+ def self.serialize_resource(resource_klass, fields, id)
562
+ resource_identity = JSONAPI::ResourceIdentity.new(resource_klass, id)
563
+ id_tree = JSONAPI::PrimaryResourceIdTree.new
564
+ id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(resource_identity), {})
565
+ resource_set = JSONAPI::ResourceSet.new(id_tree)
566
+ serializer = JSONAPI::ResourceSerializer.new(
567
+ resource_klass,
568
+ fields: fields
569
+ )
570
+ resource_set.populate!(serializer, {}, {})
571
+ serializer.serialize_resource_set_to_hash_single(resource_set)['data'].except('links')
572
+ end
573
+
561
574
  def self.serialize_changes(diffs, user)
562
575
  ctx = { current_user: user }
563
576
  diffs.each do |diff|
@@ -582,10 +595,7 @@ module DeepUnrest
582
595
  keys = diff[:resource].keys.map(&:to_sym)
583
596
  fields[to_assoc(diff[:klass].to_s.pluralize)] = keys
584
597
 
585
- JSONAPI::ResourceSerializer.new(
586
- resource_klass,
587
- fields: fields
588
- ).serialize_to_hash(resource_klass.new(diff[:model], ctx))[:data]
598
+ serialize_resource(resource_klass, fields, diff[:model].id)
589
599
  end
590
600
  resources.select { |item| item.dig('attributes') }.compact
591
601
  end
@@ -692,10 +702,10 @@ module DeepUnrest
692
702
  def self.serialize_result(ctx, item)
693
703
  resource = item[:resource]
694
704
  resource_instance = resource.new(item[:record], ctx)
695
- fields = item[:query][:fields].map(&:underscore).map(&:to_sym)
696
- JSONAPI::ResourceSerializer.new(
697
- resource,
698
- fields: { "#{item[:key].underscore.pluralize}": fields }
699
- ).serialize_to_hash(resource_instance)[:data]
705
+ fields = {}
706
+ keys = item[:query][:fields].map(&:underscore).map(&:to_sym)
707
+ fields[to_assoc(item[:key].pluralize)] = keys
708
+
709
+ serialize_resource(resource, fields, item[:record].id).except(:links)
700
710
  end
701
711
  end
@@ -102,6 +102,11 @@ module DeepUnrest
102
102
  end
103
103
  end
104
104
 
105
+ def self.format_processor_results(resource_klass, processor_result)
106
+ results = processor_result.resource_set.resource_klasses[resource_klass]
107
+ results.values.map {|r| r[:resource] }
108
+ end
109
+
105
110
  def self.query_list(ctx, item, mappings, parent_context, included, meta, addr, parent)
106
111
  base_query = item[:query].deep_dup
107
112
  extension = base_query.dig(:extend, parent&.fetch(:record)&.id&.to_s&.underscore) || {}
@@ -122,15 +127,20 @@ module DeepUnrest
122
127
  # transform sort value casing for rails
123
128
  sort_criteria = query[:sort]&.map { |s| s.clone.merge(field: s[:field].underscore) }
124
129
 
130
+ serializer = JSONAPI::ResourceSerializer.new(resource)
131
+
125
132
  processor = JSONAPI::Processor.new(resource,
126
133
  :find,
127
134
  filters: query[:filter] || {},
128
135
  context: ctx,
129
136
  sort_criteria: sort_criteria,
137
+ serializer: serializer,
130
138
  paginator: paginator)
131
139
 
132
140
  jsonapi_result = processor.process
133
141
 
142
+ resource_results = format_processor_results(resource, jsonapi_result)
143
+
134
144
  # un-monkey patch the resource :records method
135
145
  r_metaclass.send(:alias_method, :records, :records_original)
136
146
 
@@ -146,14 +156,14 @@ module DeepUnrest
146
156
  }
147
157
 
148
158
  # make sure to return empty array if no results are found for this node
149
- if jsonapi_result.resources.empty?
159
+ if resource_results.empty?
150
160
  meta << {
151
161
  addr: [*addr, item[:key], 'data'],
152
162
  serialized_result: []
153
163
  }
154
164
  end
155
165
 
156
- jsonapi_result.resources.each_with_index do |record, i|
166
+ resource_results.each_with_index do |record, i|
157
167
  next_addr = [*addr, item[:key], 'data[]', i]
158
168
  result = {
159
169
  **item,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeepUnrest
4
- VERSION = '0.1.64'
4
+ VERSION = '0.1.69'
5
5
  end
@@ -3,7 +3,6 @@
3
3
  module DeepUnrest
4
4
  module Write
5
5
  def self.get_scope_type(item, path)
6
- return :index if DeepUnrest.plural?(path)
7
6
  return :destroy if item[:destroy]
8
7
  return :show if item[:readOnly] || item[:attributes].blank?
9
8
  return :create if item[:id] && DeepUnrest.temp_id?(item[:id].to_s)
@@ -74,14 +73,14 @@ module DeepUnrest
74
73
  attributes = item.dig(:query, :attributes) || {}
75
74
  resource = item[:resource]
76
75
  p = JSONAPI::RequestParser.new
77
- p.resource_klass = resource
76
+ p.source_klass = resource
78
77
  opts = if item[:scope_type] == :create
79
78
  resource.creatable_fields(ctx)
80
79
  else
81
80
  resource.updatable_fields(ctx)
82
81
  end
83
82
 
84
- p.parse_params({ attributes: attributes }, opts)[:attributes]
83
+ p.parse_params(resource, { attributes: attributes }, opts)[:attributes]
85
84
  rescue JSONAPI::Exceptions::ParameterNotAllowed
86
85
  unpermitted_keys = attributes.keys.map(&:to_sym) - opts
87
86
  item[:errors] = unpermitted_keys.each_with_object({}) do |attr_key, memo|
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.64
4
+ version: 0.1.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lynn Hurley
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-27 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.3
19
+ version: 6.1.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.3
26
+ version: 6.1.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: jsonapi-resources
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.9.10
33
+ version: '0.10'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.9.10
40
+ version: '0.10'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,42 +58,42 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.2.3
61
+ version: 2.0.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 1.2.3
68
+ version: 2.0.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pundit
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 1.1.0
75
+ version: 2.1.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 1.1.0
82
+ version: 2.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: dragonfly
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 1.1.2
89
+ version: 1.3.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 1.1.2
96
+ version: 1.3.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: database_cleaner
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - '='
116
116
  - !ruby/object:Gem::Version
117
- version: 1.7.3
117
+ version: 2.18.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - '='
123
123
  - !ruby/object:Gem::Version
124
- version: 1.7.3
124
+ version: 2.18.0
125
125
  description: Update multiple or deeply nested JSONAPI resources
126
126
  email:
127
127
  - lynn.dylan.hurley@gmail.com
@@ -157,7 +157,7 @@ homepage: https://github.com/graveflex/deep_unrest
157
157
  licenses:
158
158
  - MIT
159
159
  metadata: {}
160
- post_install_message:
160
+ post_install_message:
161
161
  rdoc_options: []
162
162
  require_paths:
163
163
  - lib
@@ -172,8 +172,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
174
  requirements: []
175
- rubygems_version: 3.0.3
176
- signing_key:
175
+ rubygems_version: 3.2.3
176
+ signing_key:
177
177
  specification_version: 4
178
178
  summary: Update multiple or deeply nested JSONAPI resources
179
179
  test_files: []