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,7 +134,7 @@ Plays nicely with kaminari and will_paginate. | ||
134 | 134 | ||
135 | ```ruby | 135 | ```ruby |
136 | # controller | 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 | # view | 139 | # view |
140 | <%= paginate @products %> | 140 | <%= paginate @products %> |
lib/searchkick/query.rb
@@ -36,9 +36,10 @@ module Searchkick | @@ -36,9 +36,10 @@ module Searchkick | ||
36 | operator = options[:operator] || (options[:partial] ? "or" : "and") | 36 | operator = options[:operator] || (options[:partial] ? "or" : "and") |
37 | 37 | ||
38 | # pagination | 38 | # pagination |
39 | + padding = [options[:padding].to_i, 0].max | ||
39 | page = [options[:page].to_i, 1].max | 40 | page = [options[:page].to_i, 1].max |
40 | per_page = (options[:limit] || options[:per_page] || 100000).to_i | 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 | conversions_field = searchkick_options[:conversions] | 44 | conversions_field = searchkick_options[:conversions] |
44 | personalize_field = searchkick_options[:personalize] | 45 | personalize_field = searchkick_options[:personalize] |
@@ -296,6 +297,7 @@ module Searchkick | @@ -296,6 +297,7 @@ module Searchkick | ||
296 | 297 | ||
297 | @body = payload | 298 | @body = payload |
298 | @facet_limits = facet_limits | 299 | @facet_limits = facet_limits |
300 | + @padding = padding | ||
299 | @page = page | 301 | @page = page |
300 | @per_page = per_page | 302 | @per_page = per_page |
301 | @load = load | 303 | @load = load |
@@ -352,6 +354,7 @@ module Searchkick | @@ -352,6 +354,7 @@ module Searchkick | ||
352 | end | 354 | end |
353 | 355 | ||
354 | opts = { | 356 | opts = { |
357 | + padding: @padding, | ||
355 | page: @page, | 358 | page: @page, |
356 | per_page: @per_page, | 359 | per_page: @per_page, |
357 | load: @load, | 360 | load: @load, |
lib/searchkick/results.rb
@@ -80,6 +80,10 @@ module Searchkick | @@ -80,6 +80,10 @@ module Searchkick | ||
80 | options[:page] | 80 | options[:page] |
81 | end | 81 | end |
82 | 82 | ||
83 | + def padding | ||
84 | + options[:padding] | ||
85 | + end | ||
86 | + | ||
83 | def per_page | 87 | def per_page |
84 | options[:per_page] | 88 | options[:per_page] |
85 | end | 89 | end |
@@ -90,7 +94,7 @@ module Searchkick | @@ -90,7 +94,7 @@ module Searchkick | ||
90 | end | 94 | end |
91 | 95 | ||
92 | def offset_value | 96 | def offset_value |
93 | - current_page * per_page | 97 | + padding + current_page * per_page |
94 | end | 98 | end |
95 | alias_method :offset, :offset_value | 99 | alias_method :offset, :offset_value |
96 | 100 |
test/sql_test.rb
@@ -20,9 +20,10 @@ class TestSql < Minitest::Unit::TestCase | @@ -20,9 +20,10 @@ class TestSql < Minitest::Unit::TestCase | ||
20 | 20 | ||
21 | def test_pagination | 21 | def test_pagination |
22 | store_names ["Product A", "Product B", "Product C", "Product D", "Product E"] | 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 | assert_equal 2, products.current_page | 25 | assert_equal 2, products.current_page |
26 | + assert_equal 1, products.padding | ||
26 | assert_equal 2, products.per_page | 27 | assert_equal 2, products.per_page |
27 | assert_equal 2, products.size | 28 | assert_equal 2, products.size |
28 | assert_equal 2, products.length | 29 | assert_equal 2, products.length |
@@ -30,8 +31,8 @@ class TestSql < Minitest::Unit::TestCase | @@ -30,8 +31,8 @@ class TestSql < Minitest::Unit::TestCase | ||
30 | assert_equal 5, products.total_count | 31 | assert_equal 5, products.total_count |
31 | assert_equal 5, products.total_entries | 32 | assert_equal 5, products.total_entries |
32 | assert_equal 2, products.limit_value | 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 | assert !products.first_page? | 36 | assert !products.first_page? |
36 | assert !products.last_page? | 37 | assert !products.last_page? |
37 | assert !products.empty? | 38 | assert !products.empty? |