jinrai 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b26a6b9d12517eed97e97a11bb4550dad6cf03ab8712b3b56d3d40709cd2f5c
4
- data.tar.gz: f13582e01a09ba49fe3e945b4f9f213537fcc8c9da6182669cc75215c7b2121d
3
+ metadata.gz: '0178eeb57c5a6f97b36376989e7c3e5081b870d400ef538f9365798345448313'
4
+ data.tar.gz: 249497a66a0678e1060d13c952885d12c6228fa6536d36eb4da59e6870e22b93
5
5
  SHA512:
6
- metadata.gz: d044081459021b3352961f6aad68ae61c525ec57c512867327ae13011165224fdf8de8d1edc03371778f91f322baa196f38a2bccb5f8c0e6b72b2c3ec5943529
7
- data.tar.gz: 44a3f8de879cab29dcded927d7202f247e66e56f1d8d36e33430c951292be19dfb43d27c029ce3ff37532f5d902dc548282b24c35c77907a43871e5880ea97ca
6
+ metadata.gz: 6565143154e30649171225464f6c283190f581fccb31950f75a7aa49656e3f6f6de33a0e1a0c132741900e3391a6a27edcecfbdc633d3d59373116e9a70f0a5b
7
+ data.tar.gz: 363deba13720ccdfcf144a07478c1d7410d97be4113b887c22fe4d97a3f2ea6b9f3fbd8662ec5fa43af8b7d83ede8a1a01d3ceb281c91988fc59e06a9685dfb6
@@ -2,6 +2,9 @@ require 'jinrai/active_record/finder_methods'
2
2
 
3
3
  module Jinrai
4
4
  module ActiveRecord
5
+ RecordNotFound = Class.new(::ActiveRecord::RecordNotFound)
6
+ StatementInvalid = Class.new(::ActiveRecord::StatementInvalid)
7
+
5
8
  module Core
6
9
  extend ActiveSupport::Concern
7
10
 
@@ -6,10 +6,12 @@ module Jinrai
6
6
  include Jinrai::ConfigurationMethods
7
7
 
8
8
  def since_cursor
9
+ return unless first
9
10
  encode_cursor(first)
10
11
  end
11
12
 
12
13
  def till_cursor
14
+ return unless last
13
15
  encode_cursor(last)
14
16
  end
15
17
 
@@ -7,43 +7,37 @@ module Jinrai
7
7
 
8
8
  included do
9
9
  include Jinrai::ConfigurationMethods
10
-
11
- def to_cursor
12
- attributes = self.class.default_cursor_format.map do |attr|
13
- value = send(attr)
14
- value.respond_to?(:iso8601) ? value.iso8601 : value
15
- end
16
- Base64.urlsafe_encode64(attributes.join("_"))
17
- end
18
10
  end
19
11
 
20
12
  module ClassMethods
21
-
22
13
  def cursor(**options)
14
+ sort_order = options[:sort_order] || default_cursor_sort_order
23
15
  relation =
24
- if default_cursor_sort_order == :desc
16
+ if sort_order == :desc
25
17
  cursoring(:lt, :gt, options[:since], options[:sort_at]).cursoring(:gt, :lt, options[:till], options[:sort_at])
26
- elsif default_cursor_sort_order == :asc
18
+ elsif sort_order == :asc
27
19
  cursoring(:gt, :lt, options[:since], options[:sort_at]).cursoring(:lt, :gt, options[:till], options[:sort_at])
28
20
  end
29
21
  relation.extending_cursor
30
22
  end
31
23
 
32
24
  def after(cursor, **options)
25
+ sort_order = options[:sort_order] || default_cursor_sort_order
33
26
  relation =
34
- if default_cursor_sort_order == :desc
27
+ if sort_order == :desc
35
28
  cursoring(:lt, :gt, cursor, options[:sort_at])
36
- elsif default_cursor_sort_order == :asc
29
+ elsif sort_order == :asc
37
30
  cursoring(:gt, :lt, cursor, options[:sort_at])
38
31
  end
39
32
  relation.extending_cursor
40
33
  end
41
34
 
42
35
  def before(cursor, **options)
36
+ sort_order = options[:sort_order] || default_cursor_sort_order
43
37
  relation =
44
- if default_cursor_sort_order == :desc
38
+ if sort_order == :desc
45
39
  cursoring(:gt, :lt, cursor, options[:sort_at])
46
- elsif default_cursor_sort_order == :asc
40
+ elsif sort_order == :asc
47
41
  cursoring(:lt, :gt, cursor, options[:sort_at])
48
42
  end
49
43
  relation.extending_cursor
@@ -57,26 +51,33 @@ module Jinrai
57
51
  def cursoring(rank, rank_for_primary, cursor, sort_at)
58
52
  sort_at ||= primary_key
59
53
  if cursor
60
- attributes = HashWithIndifferentAccess.new(default_attributes_from_cursor.call(decode_cursor(cursor)))
54
+ attributes = default_attributes_from_cursor.call(decode_cursor(cursor))
55
+ pointed = find_by!(attributes)
61
56
 
62
57
  if sort_at != primary_key
63
- condition_1 = arel_table[sort_at].send(rank, attributes[sort_at])
64
- condition_2 = arel_table.grouping(arel_table[sort_at].eq(attributes[sort_at]).and(arel_table[primary_key].send(rank_for_primary, attributes[primary_key])))
58
+ condition_1 = arel_table[sort_at].send(rank, pointed[sort_at])
59
+ condition_2 = arel_table.grouping(arel_table[sort_at].eq(pointed[sort_at]).and(arel_table[primary_key].send(rank_for_primary, pointed[primary_key])))
65
60
  relation = where(condition_1.or(condition_2))
66
61
  else
67
- relation = where(arel_table[primary_key].send(rank, id))
62
+ relation = where(arel_table[primary_key].send(rank, pointed[primary_key]))
68
63
  end
69
64
  else
70
65
  relation = all
71
66
  end
72
67
  relation.order(sort_at => default_cursor_sort_order)
68
+ rescue ::ActiveRecord::StatementInvalid => e
69
+ # TODO: modify error message
70
+ raise Jinrai::ActiveRecord::StatementInvalid, e
71
+ rescue ::ActiveRecord::RecordNotFound
72
+ raise Jinrai::ActiveRecord::RecordNotFound,
73
+ "Could not find record cursor pointing, Check cursor_format settings."
73
74
  end
74
75
 
75
76
  private
76
77
 
77
78
  def decode_cursor(cursor)
78
79
  attributes = Base64.urlsafe_decode64(cursor).split("_")
79
- default_cursor_format.zip(attributes).to_h
80
+ HashWithIndifferentAccess.new(default_cursor_format.zip(attributes).to_h)
80
81
  end
81
82
  end
82
83
  end
@@ -8,19 +8,15 @@ module Jinrai #:nodoc:
8
8
  @_default_cursor_per = num.to_i
9
9
  end
10
10
 
11
- def cursor_format(*attributes)
11
+ def cursor_format(*attributes, &block)
12
12
  @_default_cursor_format = attributes
13
+ @_default_attributes_from_cursor = block
13
14
  end
14
15
 
15
16
  def cursor_sort_order(rank)
16
17
  @_default_cursor_sort_order = rank.to_sym
17
18
  end
18
19
 
19
- def attributes_from_cursor(&block)
20
- @_default_attributes_from_cursor = block
21
-
22
- end
23
-
24
20
  def default_cursor_per
25
21
  @_default_cursor_per || Jinrai.config.default_cursor_per
26
22
  end
@@ -1,3 +1,3 @@
1
1
  module Jinrai
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jinrai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - atomiyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2019-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_bot_rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pry-rails
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Jinrai is a awesome Cursor based pagination Link.
70
98
  email:
71
99
  - akifumi.tomiyama@studyplus.jp