activerecord_cursor_paginate 0.3.0 → 0.4.0

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: 198c2d0c2e71f89e73fca64d7ab8711949cf25faa20af6297d1d6045587ec7cf
4
- data.tar.gz: 6f60b98579b2dcd132c09d4f75105b5cab7219ae8841c6970df9e77df6b59722
3
+ metadata.gz: c62461c4c0310350845f94c41ac762fc39e55892471c3b3f27482000ce2573fb
4
+ data.tar.gz: 0cc0e0d561bedeac454bcec204b56e7b2cb85bf84e0785ed50862753604c60f3
5
5
  SHA512:
6
- metadata.gz: 85fd85af08637f1c22e52430f51bae57c5b116af2f843703dd2ab528d36616bf323d686c53c7b059fb04aa176a01bc0ec2a3c5a2b40acb6f54cf5412e2770e10
7
- data.tar.gz: be0771bacd8fbd5f110c9f90927a12792ef85d8f4e4278e46321e96d82f93f87b848ad71b86cd9150c9465c39e5d8dd3a4e2a71672e51d7d7c04f00cbb561990
6
+ metadata.gz: 4a2b046f6ee4792539aea7b84fc3a0e65994c39d0f1d14f58cb2d3e57b39a9f4181b61c66dc5bd90462a16fbd6f6b351fbef653189d6c264c543d16c53ee7ca1
7
+ data.tar.gz: 74ab23173240984e9296544ad760172878d14b4d22cd4d01857f362d36220e90e530fe488933ba0fe29fd46ccff9ae909bac35d28323cbc615964492ea3e6096
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.4.0 (2025-03-10)
4
+
5
+ - Add ability to paginate backward from the end of the collection
6
+
7
+ ```ruby
8
+ paginator = users.cursor_paginate(forward_pagination: false)
9
+ ```
10
+
3
11
  ## 0.3.0 (2025-01-06)
4
12
 
5
13
  - Allow paginating over nullable columns
@@ -20,6 +20,7 @@ module ActiveRecordCursorPaginate
20
20
  #
21
21
  class Paginator
22
22
  attr_reader :relation, :before, :after, :limit, :order, :append_primary_key
23
+ attr_accessor :forward_pagination
23
24
 
24
25
  # Create a new instance of the `ActiveRecordCursorPaginate::Paginator`
25
26
  #
@@ -45,10 +46,14 @@ module ActiveRecordCursorPaginate
45
46
  # It is not recommended to use this feature, because the complexity of produced SQL
46
47
  # queries can have a very negative impact on the database performance. It is better
47
48
  # to paginate using only non-nullable columns.
49
+ # @param forward_pagination [Boolean] Whether this is a forward or backward pagination.
50
+ # Optional, defaults to `true` if `:before` is not provided, `false` otherwise.
51
+ # Useful when paginating backward from the end of the collection.
48
52
  #
49
53
  # @raise [ArgumentError] If any parameter is not valid
50
54
  #
51
- def initialize(relation, before: nil, after: nil, limit: nil, order: nil, append_primary_key: true, nullable_columns: nil)
55
+ def initialize(relation, before: nil, after: nil, limit: nil, order: nil, append_primary_key: true,
56
+ nullable_columns: nil, forward_pagination: before.nil?)
52
57
  unless relation.is_a?(ActiveRecord::Relation)
53
58
  raise ArgumentError, "relation is not an ActiveRecord::Relation"
54
59
  end
@@ -58,7 +63,7 @@ module ActiveRecordCursorPaginate
58
63
  @append_primary_key = append_primary_key
59
64
 
60
65
  @cursor = @current_cursor = nil
61
- @is_forward_pagination = true
66
+ @forward_pagination = forward_pagination
62
67
  @before = @after = nil
63
68
  @page_size = nil
64
69
  @limit = nil
@@ -80,17 +85,18 @@ module ActiveRecordCursorPaginate
80
85
 
81
86
  @cursor = value || after
82
87
  @current_cursor = @cursor
83
- @is_forward_pagination = value.blank?
88
+ @forward_pagination = false if value
84
89
  @before = value
85
90
  end
86
91
 
87
92
  def after=(value)
88
- if before.present? && value.present?
93
+ if value.present? && before.present?
89
94
  raise ArgumentError, "Only one of :before and :after can be provided"
90
95
  end
91
96
 
92
- @cursor = before || value
97
+ @cursor = value || before
93
98
  @current_cursor = @cursor
99
+ @forward_pagination = true if value
94
100
  @after = value
95
101
  end
96
102
 
@@ -136,9 +142,9 @@ module ActiveRecordCursorPaginate
136
142
  has_additional = records_plus_one.size > @page_size
137
143
 
138
144
  records = records_plus_one.take(@page_size)
139
- records.reverse! unless @is_forward_pagination
145
+ records.reverse! unless @forward_pagination
140
146
 
141
- if @is_forward_pagination
147
+ if @forward_pagination
142
148
  has_next_page = has_additional
143
149
  has_previous_page = @current_cursor.present?
144
150
  else
@@ -301,7 +307,7 @@ module ActiveRecordCursorPaginate
301
307
  end
302
308
 
303
309
  def pagination_direction(direction)
304
- if @is_forward_pagination
310
+ if @forward_pagination
305
311
  direction
306
312
  else
307
313
  direction == :asc ? :desc : :asc
@@ -309,7 +315,7 @@ module ActiveRecordCursorPaginate
309
315
  end
310
316
 
311
317
  def pagination_operator(direction)
312
- if @is_forward_pagination
318
+ if @forward_pagination
313
319
  direction == :asc ? :gt : :lt
314
320
  else
315
321
  direction == :asc ? :lt : :gt
@@ -318,7 +324,7 @@ module ActiveRecordCursorPaginate
318
324
 
319
325
  def advance_by_page(page)
320
326
  @current_cursor =
321
- if @is_forward_pagination
327
+ if @forward_pagination
322
328
  page.next_cursor
323
329
  else
324
330
  page.previous_cursor
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCursorPaginate
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # https://github.com/rails/rails/issues/54260
4
+ require "logger"
5
+
3
6
  require "active_record"
4
7
 
5
8
  require_relative "activerecord_cursor_paginate/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_cursor_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-06 00:00:00.000000000 Z
11
+ date: 2025-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord