jinrai 1.0.1 → 1.1.3

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
- SHA1:
3
- metadata.gz: f90cbb057b2d8ed397a95afbd1be11e53f9a12ee
4
- data.tar.gz: 262d9de132390d3eb80e4641f38372950ac02967
2
+ SHA256:
3
+ metadata.gz: bbffd9d3fbdce795ac45ee5c58f173cd34fddb6ce20b8c66bf4e6f252c9875c7
4
+ data.tar.gz: 3f9ad85bd9b04780ae643a3ccc2c52cb9c471b535a1425a3717fc816d26d0479
5
5
  SHA512:
6
- metadata.gz: 2bce5e4bc9d332e8d3e3921296d624622dd347cfbfc402b426396c15704a207622e378fc4b31af93232f1e458f29f233f19e34a619d9ce394d649cdeacecb436
7
- data.tar.gz: 0141f31333eef1a456fe15edec0feaa0b761fe3d32310d996072aaed5c80bfe7cf28c9f503ccd77db8b0c9b23b522a2ff2718a5ecde409e019aae58503a5345c
6
+ metadata.gz: b111141fe30caf1d9760495a7c938475831737604f9cf01543b5f104fd527dbba29b8ed56f470a5f73bb6815261344b0b4db9aa00e3d4af986c0d7082db348f7
7
+ data.tar.gz: bcb1ad0a8139c5873a95b79d2d11708a02f57d2ce51fceddea2a6965ff83479169cf52025448161a34cd8808494ffb9271a0f3151f6d947a2cee3dcb994a8c86
data/README.md CHANGED
@@ -8,7 +8,7 @@ Jinrai is a awesome cursor based paginator
8
8
  Jinrai.configure do |config|
9
9
  config.default_cursor_per = 20 #=> User.cursor.count == 20
10
10
  config.default_cursor_format = :id, :name #=> cursor format will be "#{user.id}_#{user.name}"
11
- config.cursor_sort_order = :desc
11
+ config.default_cursor_sort_order = :desc
12
12
  end
13
13
  ```
14
14
 
@@ -18,7 +18,7 @@ end
18
18
  class User < ApplicationRecord
19
19
  cursor_per 100
20
20
  cursor_format :name, :age
21
- cursor_order :asc # default: :desc
21
+ cursor_sort_order :asc # default: :desc
22
22
  end
23
23
 
24
24
  User.cursor.count #=> 100
@@ -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
 
@@ -27,7 +29,8 @@ module Jinrai
27
29
 
28
30
  def encode_cursor(record)
29
31
  attributes = default_cursor_format.map do |attr|
30
- record.send(attr)
32
+ value = record.send(attr)
33
+ value.respond_to?(:iso8601) ? value.iso8601 : value
31
34
  end
32
35
  Base64.urlsafe_encode64(attributes.join("_"))
33
36
  end
@@ -7,43 +7,37 @@ module Jinrai
7
7
 
8
8
  included do
9
9
  include Jinrai::ConfigurationMethods
10
-
11
- def to_cursor
12
- cursor_format = self.class.default_cursor_format
13
- attributes = cursor_format.map do |attribute|
14
- self.send(attribute)
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,27 +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(decode_cursor(cursor))
61
- id = find_by(attributes).id
54
+ attributes = default_attributes_from_cursor.call(decode_cursor(cursor))
55
+ pointed = find_by!(attributes)
62
56
 
63
57
  if sort_at != primary_key
64
- condition_1 = arel_table[sort_at].send(rank, attributes[sort_at])
65
- condition_2 = arel_table.grouping(arel_table[sort_at].eq(attributes[sort_at]).and(arel_table[primary_key].send(rank_for_primary, id)))
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])))
66
60
  relation = where(condition_1.or(condition_2))
67
61
  else
68
- relation = where(arel_table[primary_key].send(rank, id))
62
+ relation = where(arel_table[primary_key].send(rank, pointed[primary_key]))
69
63
  end
70
64
  else
71
65
  relation = all
72
66
  end
73
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."
74
74
  end
75
75
 
76
76
  private
77
77
 
78
78
  def decode_cursor(cursor)
79
79
  attributes = Base64.urlsafe_decode64(cursor).split("_")
80
- default_cursor_format.zip(attributes).to_h
80
+ HashWithIndifferentAccess.new(default_cursor_format.zip(attributes).to_h)
81
81
  end
82
82
  end
83
83
  end
@@ -3,9 +3,9 @@ module Jinrai
3
3
  module Result
4
4
  attr_writer :is_cursored
5
5
 
6
- def initialize(*a)
6
+ def initialize(klass, **args)
7
7
  @is_cursored = false
8
- super(*a)
8
+ super
9
9
  end
10
10
 
11
11
  def cursored?
data/lib/jinrai/config.rb CHANGED
@@ -14,12 +14,14 @@ module Jinrai #:nodoc:
14
14
  class Config #:nodoc:
15
15
  attr_accessor :default_cursor_per,
16
16
  :default_cursor_format,
17
- :default_cursor_sort_order
17
+ :default_cursor_sort_order,
18
+ :default_attributes_from_cursor
18
19
 
19
20
  def initialize
20
21
  @default_cursor_per = 20
21
22
  @default_cursor_format = %i[created_at id]
22
23
  @default_cursor_sort_order = :desc
24
+ @default_attributes_from_cursor = Proc.new { |decoded_cursor| decoded_cursor }
23
25
  end
24
26
  end
25
27
  end
@@ -8,8 +8,9 @@ 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)
@@ -27,6 +28,10 @@ module Jinrai #:nodoc:
27
28
  def default_cursor_sort_order
28
29
  @_default_cursor_sort_order || Jinrai.config.default_cursor_sort_order
29
30
  end
31
+
32
+ def default_attributes_from_cursor
33
+ @_default_attributes_from_cursor || Jinrai.config.default_attributes_from_cursor
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -1,3 +1,3 @@
1
1
  module Jinrai
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.3'
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.0.1
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - atomiyama
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-21 00:00:00.000000000 Z
11
+ date: 2022-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.1.0
19
+ version: 5.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: 5.1.0
29
+ version: 5.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: psych
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "<"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '4.0'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: mysql2
29
49
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +72,20 @@ dependencies:
52
72
  - - ">="
53
73
  - !ruby/object:Gem::Version
54
74
  version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: factory_bot_rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
55
89
  - !ruby/object:Gem::Dependency
56
90
  name: pry-rails
57
91
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +100,21 @@ dependencies:
66
100
  - - ">="
67
101
  - !ruby/object:Gem::Version
68
102
  version: '0'
69
- description: Jinrai is a awesome Cursor type pagination Link.
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry-byebug
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Jinrai is a awesome Cursor based pagination Link.
70
118
  email:
71
119
  - akifumi.tomiyama@studyplus.jp
72
120
  executables: []
@@ -88,7 +136,7 @@ homepage: https://github.com/studyplus/jinrai
88
136
  licenses:
89
137
  - MIT
90
138
  metadata: {}
91
- post_install_message:
139
+ post_install_message:
92
140
  rdoc_options: []
93
141
  require_paths:
94
142
  - lib
@@ -103,9 +151,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
151
  - !ruby/object:Gem::Version
104
152
  version: '0'
105
153
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.6.13
108
- signing_key:
154
+ rubygems_version: 3.3.7
155
+ signing_key:
109
156
  specification_version: 4
110
- summary: A cursor type pagination plugin for Rails
157
+ summary: A cursor based pagination plugin for Rails
111
158
  test_files: []