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:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: c62461c4c0310350845f94c41ac762fc39e55892471c3b3f27482000ce2573fb
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 0cc0e0d561bedeac454bcec204b56e7b2cb85bf84e0785ed50862753604c60f3
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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, 
     | 
| 
      
 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 
     | 
    
         
            -
                  @ 
     | 
| 
      
 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 
     | 
    
         
            -
                  @ 
     | 
| 
      
 88 
     | 
    
         
            +
                  @forward_pagination = false if value
         
     | 
| 
       84 
89 
     | 
    
         
             
                  @before = value
         
     | 
| 
       85 
90 
     | 
    
         
             
                end
         
     | 
| 
       86 
91 
     | 
    
         | 
| 
       87 
92 
     | 
    
         
             
                def after=(value)
         
     | 
| 
       88 
     | 
    
         
            -
                  if  
     | 
| 
      
 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 =  
     | 
| 
      
 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 @ 
     | 
| 
      
 145 
     | 
    
         
            +
                  records.reverse! unless @forward_pagination
         
     | 
| 
       140 
146 
     | 
    
         | 
| 
       141 
     | 
    
         
            -
                  if @ 
     | 
| 
      
 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 @ 
     | 
| 
      
 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 @ 
     | 
| 
      
 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 @ 
     | 
| 
      
 327 
     | 
    
         
            +
                      if @forward_pagination
         
     | 
| 
       322 
328 
     | 
    
         
             
                        page.next_cursor
         
     | 
| 
       323 
329 
     | 
    
         
             
                      else
         
     | 
| 
       324 
330 
     | 
    
         
             
                        page.previous_cursor
         
     | 
    
        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. 
     | 
| 
      
 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- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2025-03-10 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activerecord
         
     |