geared_pagination 1.0.1 → 1.1.1

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: c3e2956267a150a716db8afc9a1e0a9b03ac14906162faa283d5de88624b2f21
4
- data.tar.gz: 85222f625262561aafae1025403375f21adc4fb3b78d750d68929183dcd2269b
3
+ metadata.gz: 750005ac1e001628bcb41a5fc7ff8c3816882df33785ff6038833d17ea9ea895
4
+ data.tar.gz: 983b5d46b9cb9174b1ced7931a0015b2d202f7f3bd578d6fd0172878ba57947a
5
5
  SHA512:
6
- metadata.gz: ebaba73472a04348a60e2e73b679dc4040f39e9b2193816bdda3c111c3a6dc9d16bcf944d4a4adf3c41b9636f6f4d194d21137dcf652f727e3223ae0576b21b1
7
- data.tar.gz: 657c6f6d8d0ee877910fb0f394e5229928325af59c471aa48c8bcddb34c75a824a5c92a128bb60757d2178205c914e3aaf3c2c96adcff036fba0098a30c7e3fe
6
+ metadata.gz: e66422eaa4008173bef8c798283f9b56a73853e88453b7d12fee2a6f5e0d0c88af0ab145fd83972bd41af017cafe35c4e306750aa75cb556ffd2bb51c465f208
7
+ data.tar.gz: 5e76d1a248cf859854b3c53eaaadcba3d14f11e4dd14dafbbca2cef30eeb1f6d791b35db386a9131eb2b9f0e4671921dcbd6171005da0f4d9dca90e0bbe9b453
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *.gem
1
2
  .byebug_history
2
3
 
3
4
  test/dummy/db/*.sqlite3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- geared_pagination (1.0.1)
4
+ geared_pagination (1.1.1)
5
5
  activesupport (>= 5.0)
6
6
  addressable (>= 2.5.0)
7
7
 
@@ -82,14 +82,16 @@ GEM
82
82
  marcel (0.3.3)
83
83
  mimemagic (~> 0.3.2)
84
84
  method_source (1.0.0)
85
- mimemagic (0.3.5)
85
+ mimemagic (0.3.10)
86
+ nokogiri (~> 1)
87
+ rake
86
88
  mini_mime (1.0.2)
87
89
  mini_portile2 (2.4.0)
88
90
  minitest (5.14.0)
89
91
  nio4r (2.5.2)
90
92
  nokogiri (1.10.9)
91
93
  mini_portile2 (~> 2.4.0)
92
- public_suffix (4.0.5)
94
+ public_suffix (4.0.6)
93
95
  rack (2.2.2)
94
96
  rack-test (1.1.0)
95
97
  rack (>= 1.0, < 3)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'geared_pagination'
3
- s.version = '1.0.1'
3
+ s.version = '1.1.1'
4
4
  s.authors = 'David Heinemeier Hansson'
5
5
  s.email = 'david@basecamp.com'
6
6
  s.summary = 'Paginate Active Record sets at variable speeds'
@@ -6,6 +6,8 @@ module GearedPagination
6
6
  class << self
7
7
  def from_param(key)
8
8
  key.present? ? decode(key) : new
9
+ rescue ArgumentError, JSON::ParserError
10
+ new
9
11
  end
10
12
 
11
13
  def decode(key)
@@ -35,7 +35,11 @@ module GearedPagination
35
35
  end
36
36
 
37
37
  def last?
38
- number >= recordset.page_count
38
+ number == recordset.page_count
39
+ end
40
+
41
+ def before_last?
42
+ number < recordset.page_count
39
43
  end
40
44
 
41
45
 
@@ -34,7 +34,7 @@ module GearedPagination
34
34
  end
35
35
 
36
36
  def orderings
37
- orders.to_h { |order| [ order.attribute, order.direction ] }
37
+ orders.map { |order| [ order.attribute, order.direction ] }.to_h
38
38
  end
39
39
 
40
40
  def limit
@@ -17,7 +17,10 @@ module GearedPagination
17
17
  end
18
18
 
19
19
  def offset
20
- (page_number - 1).times.sum { |index| ratios[index + 1] }
20
+ variable = [(page_number - 1), ratios.size - 1].min.times.sum { |index| ratios[index + 1] }
21
+ fixed = [page_number - ratios.size, 0].max * ratios.fixed
22
+
23
+ variable + fixed
21
24
  end
22
25
 
23
26
  def next_param(*)
@@ -3,15 +3,23 @@ module GearedPagination
3
3
  DEFAULTS = [ 15, 30, 50, 100 ]
4
4
 
5
5
  def initialize(ratios = nil)
6
- @ratios = Array(ratios || DEFAULTS)
6
+ @ratios = Array(ratios || DEFAULTS).map(&:to_i)
7
7
  end
8
8
 
9
9
  def [](page_number)
10
- @ratios[page_number - 1] || @ratios.last
10
+ @ratios[page_number - 1] || fixed
11
11
  end
12
12
 
13
13
  def cache_key
14
14
  @ratios.join('-')
15
15
  end
16
+
17
+ def size
18
+ @ratios.size
19
+ end
20
+
21
+ def fixed
22
+ @ratios.last
23
+ end
16
24
  end
17
25
  end
@@ -14,6 +14,9 @@ class GearedPagination::ControllerTest < ActionController::TestCase
14
14
  assert_equal etag_for("placeholder", "page/1:1-2"), response.etag
15
15
  etag_before_gearing_change = response.etag
16
16
 
17
+ get :index, params: { page: 2, per_page: [ 1, 2 ] }
18
+ assert_equal etag_for("placeholder", "page/2:1-2"), response.etag
19
+
17
20
  get :index, params: { page: 1, per_page: [ 1, 2 ] }
18
21
  assert_equal etag_before_gearing_change, response.etag
19
22
 
data/test/cursor_test.rb CHANGED
@@ -7,6 +7,11 @@ class GearedPagination::CursorTest < ActiveSupport::TestCase
7
7
  assert_equal 1, GearedPagination::Cursor.from_param(" ").page_number
8
8
  end
9
9
 
10
+ test "from an invalid param" do
11
+ assert_equal 1, GearedPagination::Cursor.from_param("aGVsbG8K").page_number
12
+ assert_equal 1, GearedPagination::Cursor.from_param("\o/ not base64").page_number
13
+ end
14
+
10
15
  test "decode" do
11
16
  assert_equal 1, GearedPagination::Cursor.decode("eyJwYWdlX251bWJlciI6MX0=").page_number
12
17
  end
data/test/page_test.rb CHANGED
@@ -20,8 +20,13 @@ class GearedPagination::PageTest < ActiveSupport::TestCase
20
20
  end
21
21
 
22
22
  test "last with page number greater than page count" do
23
- page_for_empty_set = GearedPagination::Recordset.new(Recording.none, per_page: 1000).page(2)
24
- assert page_for_empty_set.last?
23
+ assert_not GearedPagination::Recordset.new(Recording.none, per_page: 1000).page(2).last?
24
+ end
25
+
26
+ test "before_last" do
27
+ assert GearedPagination::Recordset.new(Recording.all, per_page: 1).page(1).before_last?
28
+ assert_not GearedPagination::Recordset.new(Recording.all, per_page: 1000).page(1).before_last?
29
+ assert_not GearedPagination::Recordset.new(Recording.none, per_page: 1000).page(2).before_last?
25
30
  end
26
31
 
27
32
  test "next offset param" do
@@ -7,6 +7,9 @@ class GearedPagination::PortionAtOffsetTest < ActiveSupport::TestCase
7
7
  assert_equal 0, GearedPagination::PortionAtOffset.new(page_number: 1).offset
8
8
  assert_equal GearedPagination::Ratios::DEFAULTS.first, GearedPagination::PortionAtOffset.new(page_number: 2).offset
9
9
  assert_equal GearedPagination::Ratios::DEFAULTS.first + GearedPagination::Ratios::DEFAULTS.second, GearedPagination::PortionAtOffset.new(page_number: 3).offset
10
+ assert_equal 4.times.sum { |index| GearedPagination::Ratios::DEFAULTS[index] || GearedPagination::Ratios::DEFAULTS.last }, GearedPagination::PortionAtOffset.new(page_number: 5).offset
11
+ assert_equal 5.times.sum { |index| GearedPagination::Ratios::DEFAULTS[index] || GearedPagination::Ratios::DEFAULTS.last }, GearedPagination::PortionAtOffset.new(page_number: 6).offset
12
+ assert_equal 9.times.sum { |index| GearedPagination::Ratios::DEFAULTS[index] || GearedPagination::Ratios::DEFAULTS.last }, GearedPagination::PortionAtOffset.new(page_number: 10).offset
10
13
  end
11
14
 
12
15
  test "limit" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geared_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-28 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  - !ruby/object:Gem::Version
178
178
  version: '0'
179
179
  requirements: []
180
- rubygems_version: 3.1.2
180
+ rubygems_version: 3.1.4
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: Paginate Active Record sets at variable speeds