jinrai 1.1.1 → 2.0.0

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: 2b26a6b9d12517eed97e97a11bb4550dad6cf03ab8712b3b56d3d40709cd2f5c
4
- data.tar.gz: f13582e01a09ba49fe3e945b4f9f213537fcc8c9da6182669cc75215c7b2121d
3
+ metadata.gz: e47c8eb83aae3baff1a6bb4b851595e5adbf29c344002a4d14f6acd9ecbbf88e
4
+ data.tar.gz: 5fc4128fe3860790b10722d34af7eff0363c640c6aa74f1a6d4535918276d411
5
5
  SHA512:
6
- metadata.gz: d044081459021b3352961f6aad68ae61c525ec57c512867327ae13011165224fdf8de8d1edc03371778f91f322baa196f38a2bccb5f8c0e6b72b2c3ec5943529
7
- data.tar.gz: 44a3f8de879cab29dcded927d7202f247e66e56f1d8d36e33430c951292be19dfb43d27c029ce3ff37532f5d902dc548282b24c35c77907a43871e5880ea97ca
6
+ metadata.gz: 340d93b0bb2fd0ac9a2135e7aa76fe51aedaacb616787ac75e666b8b9664b62616cb04aad95810dd8baf66973634777eb0580b4762d6283ab3c4658bacd29d77
7
+ data.tar.gz: 579490110ff36cd78776e939c3975bfa51c53ba7270c110300d9a0e51cbc98c1089f9dc2d4c5c8b7f1c96fe118b2536ffeeac19e111fb0002454174f8c10b917
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
@@ -71,6 +71,11 @@ $ gem install jinrai
71
71
  git clone git@github.com:YOUR_USERNAME/jinrai.git
72
72
  ```
73
73
 
74
+ 1. Create database
75
+ ```
76
+ $ mysql --host 127.0.0.1 -uroot -e "create database jinrai_test"
77
+ ```
78
+
74
79
  1. setup dependencies via bundler:
75
80
  ```bash
76
81
  bundle install
@@ -83,7 +88,7 @@ bundle exec rspec
83
88
 
84
89
  1. Make your change, and write spec, make sure test pass:
85
90
  ```bash
86
- bandle exec rspec
91
+ bundle exec rspec
87
92
  ```
88
93
 
89
94
  1. write a good commit message, push to your fork, then submit PullRequest.
@@ -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
@@ -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?
@@ -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 = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jinrai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - atomiyama
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2022-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: psych
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sprockets-rails
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
45
  - - ">="
18
46
  - !ruby/object:Gem::Version
19
- version: 5.1.0
47
+ version: '0'
20
48
  type: :runtime
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
52
  - - ">="
25
53
  - !ruby/object:Gem::Version
26
- version: 5.1.0
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: mysql2
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +80,20 @@ dependencies:
52
80
  - - ">="
53
81
  - !ruby/object:Gem::Version
54
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_bot_rails
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'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: pry-rails
57
99
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +108,20 @@ dependencies:
66
108
  - - ">="
67
109
  - !ruby/object:Gem::Version
68
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
69
125
  description: Jinrai is a awesome Cursor based pagination Link.
70
126
  email:
71
127
  - akifumi.tomiyama@studyplus.jp
@@ -88,7 +144,7 @@ homepage: https://github.com/studyplus/jinrai
88
144
  licenses:
89
145
  - MIT
90
146
  metadata: {}
91
- post_install_message:
147
+ post_install_message:
92
148
  rdoc_options: []
93
149
  require_paths:
94
150
  - lib
@@ -103,8 +159,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
159
  - !ruby/object:Gem::Version
104
160
  version: '0'
105
161
  requirements: []
106
- rubygems_version: 3.0.2
107
- signing_key:
162
+ rubygems_version: 3.2.15
163
+ signing_key:
108
164
  specification_version: 4
109
165
  summary: A cursor based pagination plugin for Rails
110
166
  test_files: []