graphiti 1.2.39 → 1.2.44

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: 6d2917a607d95ef6d4701b8960ed183fd39923662b034d74fda9f8080f65d534
4
- data.tar.gz: 7fc8ec33363e0112c541d05c676c7315b1934876f15875ac0b153bf2fc6cebdd
3
+ metadata.gz: 1c0d06bf22a3732408737c7c603c500ba8f7c71f339f6782e96d93b290e2db84
4
+ data.tar.gz: 47f4cadecfe0b119dd2ad3de56ff87a6cd1f59a1d2bda572a98c57d142d4a059
5
5
  SHA512:
6
- metadata.gz: 6f9867cbf49483cb09a887db26af222e2d74962c07aaf6ece7552a73cf74b625ca49e19ed730c1f383f7706ae10546551d54792b2b09832709aa14829ddd8180
7
- data.tar.gz: 746d7900a12437a7a610dc8d4e8737ada319867578349898cc95067d7295c1b04b659dec00e33aec0a9977c33ecafd0a58f9f07da61ec04def6ad8131656351f
6
+ metadata.gz: 13d3880b37b14c2d22f8702c8a5f09cebd6a2ce4768018f4e109e9d05666d3b0da19d5685462978cd76d3f6f5ac8400283e3298d7d558c96af3077ba83cd2955
7
+ data.tar.gz: 3470e79878d650afb8ca1bddf70d510b8a0e0fdb208945307ffeb24436ab793428c6627d746200d1eb33718fd23f3e77163fbb97bf9f1147a4283e5388a53b6f
@@ -244,14 +244,15 @@ module Graphiti
244
244
  # @param scope The scope object we are chaining
245
245
  # @param [Integer] current_page The current page number
246
246
  # @param [Integer] per_page The number of results per page
247
+ # @param [Integer] offset The offset to start from
247
248
  # @return the scope
248
249
  #
249
250
  # @example ActiveRecord default
250
251
  # # via kaminari gem
251
- # def paginate(scope, current_page, per_page)
252
+ # def paginate(scope, current_page, per_page, offset)
252
253
  # scope.page(current_page).per(per_page)
253
254
  # end
254
- def paginate(scope, current_page, per_page)
255
+ def paginate(scope, current_page, per_page, offset)
255
256
  raise "you must override #paginate in an adapter subclass"
256
257
  end
257
258
 
@@ -184,8 +184,11 @@ module Graphiti
184
184
  end
185
185
 
186
186
  # (see Adapters::Abstract#paginate)
187
- def paginate(scope, current_page, per_page)
188
- scope.page(current_page).per(per_page)
187
+ def paginate(scope, current_page, per_page, offset)
188
+ scope = scope.page(current_page) if current_page
189
+ scope = scope.per(per_page) if per_page
190
+ scope = scope.padding(offset) if offset
191
+ scope
189
192
  end
190
193
 
191
194
  # (see Adapters::Abstract#count)
@@ -240,7 +243,8 @@ module Graphiti
240
243
  children.each do |child|
241
244
  if association_type == :many_to_many &&
242
245
  [:create, :update].include?(Graphiti.context[:namespace]) &&
243
- !parent.send(association_name).exists?(child.id)
246
+ !parent.send(association_name).exists?(child.id) &&
247
+ child.errors.blank?
244
248
  parent.send(association_name) << child
245
249
  else
246
250
  target = association.instance_variable_get(:@target)
@@ -178,7 +178,7 @@ module Graphiti
178
178
  end
179
179
 
180
180
  # (see Adapters::Abstract#paginate)
181
- def paginate(scope, current_page, per_page)
181
+ def paginate(scope, current_page, per_page, offset)
182
182
  scope
183
183
  end
184
184
 
@@ -30,13 +30,14 @@ module Graphiti
30
30
 
31
31
  uri = URI(@proxy.resource.endpoint[:url].to_s)
32
32
 
33
+ page_params = {
34
+ number: page,
35
+ size: page_size
36
+ }
37
+ page_params[:offset] = offset if offset
38
+
33
39
  # Overwrite the pagination query params with the desired page
34
- uri.query = pagination_params.merge({
35
- page: {
36
- number: page,
37
- size: page_size
38
- }
39
- }).to_query
40
+ uri.query = pagination_params.merge(page: page_params).to_query
40
41
  uri.to_s
41
42
  end
42
43
 
@@ -46,8 +47,11 @@ module Graphiti
46
47
  elsif page_size == 0 || item_count == 0
47
48
  return nil
48
49
  end
49
- @last_page = (item_count / page_size)
50
- @last_page += 1 if item_count % page_size > 0
50
+
51
+ count = item_count
52
+ count = item_count - offset if offset
53
+ @last_page = (count / page_size)
54
+ @last_page += 1 if count % page_size > 0
51
55
  @last_page
52
56
  end
53
57
 
@@ -82,6 +86,14 @@ module Graphiti
82
86
  @current_page ||= (page_param[:number] || 1).to_i
83
87
  end
84
88
 
89
+ def offset
90
+ @offset ||= begin
91
+ if (value = page_param[:offset])
92
+ value.to_i
93
+ end
94
+ end
95
+ end
96
+
85
97
  def page_size
86
98
  @page_size ||= (page_param[:size] ||
87
99
  @proxy.resource.default_page_size ||
@@ -32,7 +32,9 @@ module Graphiti
32
32
  end
33
33
 
34
34
  def pagination_links?
35
- if Graphiti.config.pagination_links_on_demand
35
+ if action == :find
36
+ false
37
+ elsif Graphiti.config.pagination_links_on_demand
36
38
  [true, "true"].include?(@params[:pagination_links])
37
39
  else
38
40
  Graphiti.config.pagination_links
@@ -193,7 +195,7 @@ module Graphiti
193
195
  end
194
196
  elsif nested?(name)
195
197
  hash[name.to_s.split(".").last.to_sym] = value
196
- elsif top_level? && [:number, :size].include?(name.to_sym)
198
+ elsif top_level? && [:number, :size, :offset].include?(name.to_sym)
197
199
  hash[name.to_sym] = value.to_i
198
200
  end
199
201
  end
@@ -171,14 +171,18 @@ module Graphiti
171
171
  type = Graphiti::Types[filter[:type]]
172
172
  array_or_string = [:string, :array].include?(type[:canonical_name])
173
173
  if (arr = value.scan(/\[.*?\]/)).present? && array_or_string
174
- value = arr.map { |json|
175
- begin
176
- JSON.parse(json)
177
- rescue
178
- raise Errors::InvalidJSONArray.new(resource, value)
179
- end
180
- }
181
- value = value[0] if value.length == 1
174
+ begin
175
+ value = arr.map { |json|
176
+ begin
177
+ JSON.parse(json)
178
+ rescue
179
+ raise Errors::InvalidJSONArray.new(resource, value)
180
+ end
181
+ }
182
+ value = value[0] if value.length == 1
183
+ rescue Errors::InvalidJSONArray => e
184
+ raise(e) if type[:canonical_name] == :array
185
+ end
182
186
  else
183
187
  value = parse_string_arrays(value, !!filter[:single])
184
188
  end
@@ -33,12 +33,24 @@ module Graphiti
33
33
 
34
34
  # Apply default pagination proc via the Resource adapter
35
35
  def apply_standard_scope
36
- resource.adapter.paginate(@scope, number, size)
36
+ meth = resource.adapter.method(:paginate)
37
+
38
+ if meth.arity == 4 # backwards-compat
39
+ resource.adapter.paginate(@scope, number, size, offset)
40
+ else
41
+ resource.adapter.paginate(@scope, number, size)
42
+ end
37
43
  end
38
44
 
39
45
  # Apply the custom pagination proc
40
46
  def apply_custom_scope
41
- resource.instance_exec(@scope, number, size, resource.context, &custom_scope)
47
+ resource.instance_exec \
48
+ @scope,
49
+ number,
50
+ size,
51
+ resource.context,
52
+ offset,
53
+ &custom_scope
42
54
  end
43
55
 
44
56
  private
@@ -51,6 +63,12 @@ module Graphiti
51
63
  @page_param ||= (query_hash[:page] || {})
52
64
  end
53
65
 
66
+ def offset
67
+ if (value = page_param[:offset])
68
+ value.to_i
69
+ end
70
+ end
71
+
54
72
  def number
55
73
  (page_param[:number] || 1).to_i
56
74
  end
@@ -137,11 +137,16 @@ module Graphiti
137
137
  end
138
138
 
139
139
  def link_extra_fields
140
+ return unless context&.respond_to?(:params)
141
+
140
142
  extra_fields_name = [association_name, resource.type].find { |param|
141
143
  context.params.dig(:extra_fields, param)
142
144
  }
143
145
 
144
- {resource.type => context.params.dig(:extra_fields, extra_fields_name)} if extra_fields_name
146
+ if extra_fields_name
147
+ extra_fields = context.params.dig(:extra_fields, extra_fields_name)
148
+ {resource.type => extra_fields}
149
+ end
145
150
  end
146
151
 
147
152
  # The parent resource is a remote,
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.2.39"
2
+ VERSION = "1.2.44"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.39
4
+ version: 1.2.44
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-26 00:00:00.000000000 Z
11
+ date: 2021-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable
@@ -361,7 +361,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
361
361
  - !ruby/object:Gem::Version
362
362
  version: '0'
363
363
  requirements: []
364
- rubygems_version: 3.0.6
364
+ rubygems_version: 3.0.3
365
365
  signing_key:
366
366
  specification_version: 4
367
367
  summary: Easily build jsonapi.org-compatible APIs