pageturner 4.0.0 → 4.1.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
  SHA1:
3
- metadata.gz: 9ec92e81b1919467490096e12c1bb8413a028262
4
- data.tar.gz: 2215eda44407930869479a61c51f40c631d971b8
3
+ metadata.gz: eaeccd8d18f2c48b0c8c4d86b07221cacb1923ec
4
+ data.tar.gz: 5041e0de3262ad5b06c442be7e904a771c9c6b20
5
5
  SHA512:
6
- metadata.gz: 9c8ada0c35616416882e52147758e98dfb86222ac440f17c194f27eebc057da2013ae4331742b04356223d59b95881e936284a13e621168b5ce9b7ae68e96df4
7
- data.tar.gz: 6de96592c15e9cd52ef14b08c09d1f378107877c93df23b2bdec14817f5fb9a8115c924da77e785f6633c966e1080cb19e37064dc9f539551f617f693d88c63c
6
+ metadata.gz: 7ef1390fc398ba7daa3fa207c7cf9fe947d676c8e07d843a76d8a60fbfd2c50c7d295f49a5ed1ee510f8d37b6d912b751453dd31803c327737276573ac011808
7
+ data.tar.gz: ffe3748684edeae86561ced8c73e01a4d873d9282486ddcf4837d25125f2951ed7b94120d0bbc065ed07c00296a37e9974ff87a8ad9a9ef2057e4875f0f0ffbf
@@ -1,3 +1,9 @@
1
+ # v4.0.0
2
+
3
+ ## Backwards Incompatible Changes
4
+
5
+ * Support pagination over columns that are referenced differently in the database schema than in the application code. Although this looks like a feature add, it does break backwards compatibility on the assumptions that Pageturner makes on how the `anchor_column` is referenced in your database and application. Pageturner will now make less assumptions about that by allowing users to pass in database and application translations for the `anchor_column` as input. **Test your use cases after upgrading.**
6
+
1
7
  # v3.1.0
2
8
 
3
9
  ## Changes
data/README.md CHANGED
@@ -56,6 +56,15 @@ Pageturner will send the `anchor_column_active_record_identifier` as a method ch
56
56
 
57
57
  ## API
58
58
 
59
+ ### `#has_next_page?`
60
+
61
+ Returns whether or not there is a next page.
62
+
63
+ ```ruby
64
+ irb(main):001:0> @pagination.has_next_page?
65
+ => true
66
+ ```
67
+
59
68
  ### `#next_cursor`
60
69
 
61
70
  Returns the next page's cursor.
@@ -4,6 +4,7 @@ class Pageturner
4
4
  DESC = "desc"
5
5
  GREATER_THAN_OPERATOR = ">"
6
6
  LESS_THAN_OPERATOR = "<"
7
+ LOOKAHEAD = 1
7
8
 
8
9
  # @param [String] anchor_column - Field to paginate on.
9
10
  # @param [Anything, nil] anchor_value - Value of the anchor_column for the record to paginate from.
@@ -36,6 +37,13 @@ class Pageturner
36
37
  end
37
38
  end
38
39
 
40
+ def has_next_page?
41
+ # Load the current page.
42
+ self.resources
43
+
44
+ @has_next_page
45
+ end
46
+
39
47
  def resources
40
48
  # Memoize expensive querying.
41
49
  return @result if defined?(@result)
@@ -56,20 +64,16 @@ class Pageturner
56
64
  private
57
65
 
58
66
  def calculate_resources
59
- resources =
60
- if can_calculate_nth_page?
61
- calculate_next_page
62
- else
63
- calculate_first_page
64
- end
65
-
66
- resources
67
+ (can_calculate_nth_page? ? calculate_next_page : calculate_first_page).tap do |resources|
68
+ @has_next_page = resources.size > @page_size
69
+ end.take(@page_size)
67
70
  end
68
71
 
69
72
  def comparator_for_fetching_resources
70
- if @sort_direction == Pageturner::ASC
73
+ case @sort_direction
74
+ when Pageturner::ASC
71
75
  GREATER_THAN_OPERATOR
72
- elsif @sort_direction == Pageturner::DESC
76
+ when Pageturner::DESC
73
77
  LESS_THAN_OPERATOR
74
78
  else
75
79
  raise COMPARATOR_INTERNAL_BUG
@@ -81,7 +85,7 @@ class Pageturner
81
85
  end
82
86
 
83
87
  def calculate_first_page
84
- apply_sort_direction(@ar_relation)
88
+ apply_order_and_limit(@ar_relation)
85
89
  end
86
90
 
87
91
  def calculate_next_page
@@ -107,13 +111,13 @@ class Pageturner
107
111
  @ar_relation.where("#{qualified_anchor_column} IS NULL AND #{qualified_anchor_pk_column} #{comparator_for_fetching_resources} ?", @anchor_id)
108
112
  end
109
113
 
110
- apply_sort_direction(where_clause)
114
+ apply_order_and_limit(where_clause)
111
115
  end
112
116
 
113
- def apply_sort_direction(ar_relation)
117
+ def apply_order_and_limit(ar_relation)
114
118
  ar_relation
115
119
  .order("#{@anchor_column_sql_identifier} #{@sort_direction}", @ar_relation.primary_key => @sort_direction)
116
- .limit(@page_size)
120
+ .limit(@page_size + LOOKAHEAD)
117
121
  end
118
122
 
119
123
  def nulls_listed_first?
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "pageturner"
7
- spec.version = "4.0.0"
7
+ spec.version = "4.1.0"
8
8
  spec.authors = ["crzrcn"]
9
9
  spec.email = ["fernanlink@gmail.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pageturner
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - crzrcn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2018-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord