Commit f94476042ec7d955ccea84519ce8415ee1dadc45
1 parent
80f8811a
Exists in
master
and in
21 other branches
Add pagination `padding` option to #search.
This allows you to pad a number of records that is not a multiple of the page size.
Showing
4 changed files
with
15 additions
and
7 deletions
Show diff stats
README.md
... | ... | @@ -134,7 +134,7 @@ Plays nicely with kaminari and will_paginate. |
134 | 134 | |
135 | 135 | ```ruby |
136 | 136 | # controller |
137 | -@products = Product.search "milk", page: params[:page], per_page: 20 | |
137 | +@products = Product.search "milk", page: params[:page], per_page: 20, padding: 5 | |
138 | 138 | |
139 | 139 | # view |
140 | 140 | <%= paginate @products %> | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -36,9 +36,10 @@ module Searchkick |
36 | 36 | operator = options[:operator] || (options[:partial] ? "or" : "and") |
37 | 37 | |
38 | 38 | # pagination |
39 | + padding = [options[:padding].to_i, 0].max | |
39 | 40 | page = [options[:page].to_i, 1].max |
40 | 41 | per_page = (options[:limit] || options[:per_page] || 100000).to_i |
41 | - offset = options[:offset] || (page - 1) * per_page | |
42 | + offset = options[:offset] || padding + (page - 1) * per_page | |
42 | 43 | |
43 | 44 | conversions_field = searchkick_options[:conversions] |
44 | 45 | personalize_field = searchkick_options[:personalize] |
... | ... | @@ -296,6 +297,7 @@ module Searchkick |
296 | 297 | |
297 | 298 | @body = payload |
298 | 299 | @facet_limits = facet_limits |
300 | + @padding = padding | |
299 | 301 | @page = page |
300 | 302 | @per_page = per_page |
301 | 303 | @load = load |
... | ... | @@ -352,6 +354,7 @@ module Searchkick |
352 | 354 | end |
353 | 355 | |
354 | 356 | opts = { |
357 | + padding: @padding, | |
355 | 358 | page: @page, |
356 | 359 | per_page: @per_page, |
357 | 360 | load: @load, | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -80,6 +80,10 @@ module Searchkick |
80 | 80 | options[:page] |
81 | 81 | end |
82 | 82 | |
83 | + def padding | |
84 | + options[:padding] | |
85 | + end | |
86 | + | |
83 | 87 | def per_page |
84 | 88 | options[:per_page] |
85 | 89 | end |
... | ... | @@ -90,7 +94,7 @@ module Searchkick |
90 | 94 | end |
91 | 95 | |
92 | 96 | def offset_value |
93 | - current_page * per_page | |
97 | + padding + current_page * per_page | |
94 | 98 | end |
95 | 99 | alias_method :offset, :offset_value |
96 | 100 | ... | ... |
test/sql_test.rb
... | ... | @@ -20,9 +20,10 @@ class TestSql < Minitest::Unit::TestCase |
20 | 20 | |
21 | 21 | def test_pagination |
22 | 22 | store_names ["Product A", "Product B", "Product C", "Product D", "Product E"] |
23 | - products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2) | |
24 | - assert_equal ["Product C", "Product D"], products.map(&:name) | |
23 | + products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2, padding: 1) | |
24 | + assert_equal ["Product D", "Product E"], products.map(&:name) | |
25 | 25 | assert_equal 2, products.current_page |
26 | + assert_equal 1, products.padding | |
26 | 27 | assert_equal 2, products.per_page |
27 | 28 | assert_equal 2, products.size |
28 | 29 | assert_equal 2, products.length |
... | ... | @@ -30,8 +31,8 @@ class TestSql < Minitest::Unit::TestCase |
30 | 31 | assert_equal 5, products.total_count |
31 | 32 | assert_equal 5, products.total_entries |
32 | 33 | assert_equal 2, products.limit_value |
33 | - assert_equal 4, products.offset_value | |
34 | - assert_equal 4, products.offset | |
34 | + assert_equal 5, products.offset_value | |
35 | + assert_equal 5, products.offset | |
35 | 36 | assert !products.first_page? |
36 | 37 | assert !products.last_page? |
37 | 38 | assert !products.empty? | ... | ... |