graphiti 1.0.rc.9 → 1.0.rc.10

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: b671896a39d2dbafcbba2aeff6f28b84975716c5
4
- data.tar.gz: '08d51b340c5311838a386a6b623140b52b4f9109'
3
+ metadata.gz: 5e7e1bd6105f9bf95c5ba1a627045076ab9eb829
4
+ data.tar.gz: 935d68b3b664e3afaa2c7d2894a55d1bab25fff8
5
5
  SHA512:
6
- metadata.gz: 656b61085599d072b1af3d04e0356170e796c0bb02b1d7dabea43fffcc61ad14c4df6c9ef2c7fdb309e3c1fabcf5cfdecb694d99e66243a18fc11a1e982c6611
7
- data.tar.gz: fa1068a26baeae8bdadcd3fe3406e8b15cfc7baa86e50edba8cd073916b9266df6fbd7d599bc147c0f01e29c59e80290775820c12bc7232da300fbff76279f87
6
+ metadata.gz: 683b046c667a6321edc27c3b5622d5c63f36cc72facd81adb056c8d49a0a8caf51120996efe22966f32f18e4edb6c699fd234cdb8a81153e9ee10967d5da2c14
7
+ data.tar.gz: 84a831d72e7f826c74668d717e24fbcf520dbe702b2f783a0838100a2b8d9f0b192c88363a94fe7f65a443a3f6cf0e453b23d087b6bbfa90ed9eed83de37bf64
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  .byebug_history
11
11
  spec/dummy/log/*
12
+ *~
data/lib/graphiti.rb CHANGED
@@ -53,6 +53,7 @@ require "graphiti/scoping/default_filter"
53
53
  require "graphiti/scoping/filter"
54
54
  require "graphiti/stats/dsl"
55
55
  require "graphiti/stats/payload"
56
+ require "graphiti/delegates/pagination"
56
57
  require "graphiti/util/include_params"
57
58
  require "graphiti/util/field_params"
58
59
  require "graphiti/util/hash"
@@ -12,6 +12,7 @@ module Graphiti
12
12
  attr_accessor :context_for_endpoint
13
13
  attr_accessor :schema_path
14
14
  attr_accessor :links_on_demand
15
+ attr_accessor :pagination_links_on_demand
15
16
  attr_accessor :typecast_reads
16
17
  attr_accessor :debug
17
18
  attr_accessor :debug_models
@@ -23,6 +24,7 @@ module Graphiti
23
24
  @concurrency = false
24
25
  @respond_to = [:json, :jsonapi, :xml]
25
26
  @links_on_demand = false
27
+ @pagination_links_on_demand = false
26
28
  @typecast_reads = true
27
29
  self.debug = ENV.fetch('GRAPHITI_DEBUG', true)
28
30
  self.debug_models = ENV.fetch('GRAPHITI_DEBUG_MODELS', false)
@@ -0,0 +1,77 @@
1
+ module Graphiti
2
+ module Delegates
3
+ class Pagination
4
+ def initialize(proxy)
5
+ @proxy = proxy
6
+ end
7
+
8
+ def links?
9
+ @proxy.query.pagination_links?
10
+ end
11
+
12
+ def links
13
+ @links ||= { }.tap do |links|
14
+ links[:first] = pagination_link(1)
15
+ links[:last] = pagination_link(last_page)
16
+ links[:prev] = pagination_link(current_page - 1) unless current_page == 1
17
+ links[:next] = pagination_link(current_page + 1) unless current_page == last_page
18
+ end.select{|k,v| !v.nil? }
19
+ end
20
+
21
+ private
22
+
23
+ def pagination_link(page)
24
+ return nil unless @proxy.resource.endpoint
25
+
26
+ uri = URI(@proxy.resource.endpoint[:url].to_s)
27
+
28
+ # Overwrite the pagination query params with the desired page
29
+ uri.query = @proxy.query.hash.merge({
30
+ page: {
31
+ number: page,
32
+ size: page_size
33
+ }
34
+ }).to_query
35
+ uri.to_s
36
+ end
37
+
38
+ def last_page
39
+ return @last_page if @last_page || page_size == 0
40
+ @last_page = (item_count / page_size)
41
+ @last_page += 1 if item_count % page_size > 0
42
+ @last_page
43
+ end
44
+
45
+ def item_count
46
+ begin
47
+ return @item_count if @item_count
48
+ @item_count = @proxy.resource.stat(:total, :count).call(@proxy.scope.unpaginated_object, :total)
49
+ unless @item_count.kind_of?(Numeric)
50
+ raise TypeError, "#{@proxy.resource}.stat(:total, :count) returned an invalid value #{@item_count}"
51
+ end
52
+ rescue => e
53
+ # FIXME: Unable to log because of how rspec mocks were
54
+ # created for the logger. In other words, logging here will
55
+ # break tests.
56
+
57
+ # Graphiti.logger.warn(e.message)
58
+ @item_count = 0
59
+ end
60
+ end
61
+
62
+ def current_page
63
+ @current_page ||= (page_param[:number] || 1).to_i
64
+ end
65
+
66
+ def page_size
67
+ @page_size ||= (page_param[:size] ||
68
+ @proxy.resource.default_page_size ||
69
+ Graphiti::Scoping::Paginate::DEFAULT_PAGE_SIZE).to_i
70
+ end
71
+
72
+ def page_param
73
+ @page_param ||= (@proxy.query.hash[:page] || {})
74
+ end
75
+ end
76
+ end
77
+ end
@@ -30,6 +30,14 @@ module Graphiti
30
30
  end
31
31
  end
32
32
 
33
+ def pagination_links?
34
+ if Graphiti.config.pagination_links_on_demand
35
+ [true, 'true'].include?(@params[:pagination_links])
36
+ else
37
+ true
38
+ end
39
+ end
40
+
33
41
  def debug_requested?
34
42
  !!@params[:debug]
35
43
  end
@@ -44,6 +44,7 @@ module Graphiti
44
44
  options[:expose][:extra_fields] = proxy.extra_fields
45
45
  options[:expose][:proxy] = proxy
46
46
  options[:include] = proxy.include_hash
47
+ options[:links] = proxy.pagination.links if proxy.pagination.links?
47
48
  options[:meta] ||= {}
48
49
  options[:meta].merge!(stats: proxy.stats) unless proxy.stats.empty?
49
50
  options[:meta][:debug] = Debugger.to_a if debug_json?
@@ -80,6 +80,10 @@ module Graphiti
80
80
  end
81
81
  end
82
82
 
83
+ def pagination
84
+ @pagination ||= Delegates::Pagination.new(self)
85
+ end
86
+
83
87
  def save(action: :create)
84
88
  # TODO: remove this. Only used for persisting many-to-many with AR
85
89
  # (see activerecord adapter)
@@ -1,7 +1,7 @@
1
1
  module Graphiti
2
2
  class Scope
3
3
  attr_accessor :object, :unpaginated_object
4
-
4
+ attr_reader :pagination
5
5
  def initialize(object, resource, query, opts = {})
6
6
  @object = object
7
7
  @resource = resource
@@ -14,11 +14,21 @@ module Graphiti
14
14
  end
15
15
 
16
16
  def generate
17
- on_demand_links(raw_url)
17
+ if linkable?
18
+ on_demand_links(raw_url)
19
+ end
18
20
  end
19
21
 
20
22
  private
21
23
 
24
+ def linkable?
25
+ if @sideload.type == :belongs_to
26
+ not @model.send(@sideload.foreign_key).nil?
27
+ else
28
+ true
29
+ end
30
+ end
31
+
22
32
  def raw_url
23
33
  if @sideload.link_proc
24
34
  @sideload.link_proc.call(@model)
@@ -59,6 +59,8 @@ class Graphiti::Util::ValidationResponse
59
59
  if payload.is_a?(Array)
60
60
  related_objects = model.send(name)
61
61
  related_objects.each_with_index do |r, index|
62
+ method = payload[index].try(:[], :meta).try(:[], :method)
63
+ next if [nil, :destroy, :disassociate].include?(method)
62
64
  valid = valid_object?(r)
63
65
  checks << valid
64
66
 
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.rc.9"
2
+ VERSION = "1.0.rc.10"
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.0.rc.9
4
+ version: 1.0.rc.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-30 00:00:00.000000000 Z
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable
@@ -281,6 +281,7 @@ files:
281
281
  - lib/graphiti/configuration.rb
282
282
  - lib/graphiti/context.rb
283
283
  - lib/graphiti/debugger.rb
284
+ - lib/graphiti/delegates/pagination.rb
284
285
  - lib/graphiti/deserializer.rb
285
286
  - lib/graphiti/errors.rb
286
287
  - lib/graphiti/extensions/boolean_attribute.rb