page_cursor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 932517a4e798874d1d618d79ec7d96957a27997e3bcf7b755a8922e13b1d6563
4
+ data.tar.gz: a4e27daca15f4d1f4fc83a8dbd467de05e35319e0ddfefdfa5495cd54371783e
5
+ SHA512:
6
+ metadata.gz: 56a5c40258fc216d7dbcaf61e44514f400f5dc2e52d9cfa81020e5f4b6a3e587f806c7ff29c62e1d7c6ad4d57ba57aebcf614b2e714ce9d2e61442dac1174e41
7
+ data.tar.gz: d49481ae21e0667fef99733800187caaed9dbecbcb173de4e42bf762cefd138015aa299ef345e198070963ee6d79865c7043bb45656a8f1a45c1a90272f4b20b
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Matthias Kadenbach
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # page_cursor
2
+
3
+ Cursor-based pagination for Rails.
4
+
5
+ ```ruby
6
+ gem 'page_cursor'
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ @cursor, @records = paginate(User.where(active: true)) # in controller
13
+
14
+ <%= pagination_nav @cursor %> # in view
15
+ ```
16
+
17
+ Please note that you'll have to create the `pagination_nav` helper yourself. Have a look
18
+ at an [example helper](test/dummy/app/helpers/application_helper.rb) with its rendered
19
+ [example partial](test/dummy/app/views/layouts/_pagination_nav.html.erb).
20
+
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PageCursor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,2 @@
1
+ require "page_cursor/cursor"
2
+ require "page_cursor/railtie"
@@ -0,0 +1,66 @@
1
+ module PageCursor
2
+ module ActionControllerExtension
3
+ # paginate returns a cursor-enabled pagination.
4
+ # It uses params[:after] and params[:before] request variables.
5
+ # It assumes @record's primary key is sortable.
6
+ #
7
+ # Example usage:
8
+ # ```
9
+ # @cursor, @records = paginate(@records) # in controller
10
+ # <%= pagination_nav @cursor %> # in view
11
+ # ```
12
+ #
13
+ # @cursor is a hash returning the next (`after`)
14
+ # and previous (`before`) primary key, if more records are available.
15
+ #
16
+ # Caveat: All collection's order statements are overwritten by paginate.
17
+ def paginate(collection, direction = :asc, limit = 10)
18
+ fail "direction must be :asc or :desc" unless [:asc, :desc].include?(direction)
19
+ fail "limit must be >= 1" unless limit >= 1
20
+
21
+ after = params[:after]
22
+ before = params[:before]
23
+ fail "only provide one, either params[:after] or params[:before]" if after.present? && before.present?
24
+
25
+ # reference the table's primary key attribute
26
+ pk = collection.arel_table[collection.primary_key]
27
+
28
+ # return limit + 1 to see if there are more records
29
+ collection = collection.limit(limit + 1)
30
+
31
+ if after.present?
32
+ if direction == :asc
33
+ collection = collection.where(pk.send("gt", after)).reorder(pk.send(:asc))
34
+ elsif direction == :desc
35
+ collection = collection.where(pk.send("lt", after)).reorder(pk.send(:desc))
36
+ end
37
+ r = collection.all
38
+
39
+ return { :after => nil, :before => r.first&.id }, r if r.size <= limit
40
+ r = r.first(r.size - 1)
41
+ return { :after => r.last&.id, :before => r.first&.id }, r
42
+
43
+ # ---
44
+ elsif before.present?
45
+ if direction == :asc
46
+ collection = collection.where(pk.send("lt", before)).reorder(pk.send(:desc))
47
+ elsif direction == :desc
48
+ collection = collection.where(pk.send("gt", before)).reorder(pk.send(:asc))
49
+ end
50
+ r = collection.all.reverse
51
+
52
+ return { :after => r.last&.id, :before => nil }, r if r.size <= limit
53
+ r = r.last(r.size - 1)
54
+ return { :after => r.last&.id, :before => r.first&.id }, r
55
+
56
+ # ---
57
+ else
58
+ # if after and before are both missing
59
+ r = collection.reorder(pk.send(direction)).all
60
+ return { :after => nil, :before => nil }, r if r.size <= limit
61
+ r = r.first(r.size - 1)
62
+ return { :after => r.last&.id, :before => nil }, r
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,9 @@
1
+ module PageCursor
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "page_cursor" do |app|
4
+ ActiveSupport.on_load :action_controller do
5
+ ActionController::Base.include PageCursor::ActionControllerExtension
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PageCursor
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_cursor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthias Kadenbach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ksuid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Cursor-based pagination for Rails.
56
+ email:
57
+ - matthias.kadenbach@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/page_cursor.rb
66
+ - lib/page_cursor/cursor.rb
67
+ - lib/page_cursor/railtie.rb
68
+ - lib/page_cursor/version.rb
69
+ homepage: https://github.com/mattes/ruby_page_cursor
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Cursor-based pagination
92
+ test_files: []