Commit d0fd1dbece10f3cdc94b9bc4612885c6a08fe9de
1 parent
3b460ccd
Exists in
master
and in
2 other branches
Added page and per_page methods on relation
Showing
2 changed files
with
51 additions
and
0 deletions
Show diff stats
lib/searchkick/relation.rb
@@ -55,6 +55,31 @@ module Searchkick | @@ -55,6 +55,31 @@ module Searchkick | ||
55 | self | 55 | self |
56 | end | 56 | end |
57 | 57 | ||
58 | + def page(value) | ||
59 | + clone.page!(value) | ||
60 | + end | ||
61 | + | ||
62 | + def page!(value) | ||
63 | + check_loaded | ||
64 | + @options[:page] = value | ||
65 | + self | ||
66 | + end | ||
67 | + | ||
68 | + def per_page(value = NO_DEFAULT_VALUE) | ||
69 | + # TODO remove in Searchkick 6 | ||
70 | + if value == NO_DEFAULT_VALUE | ||
71 | + private_execute.per_page | ||
72 | + else | ||
73 | + clone.per_page!(value) | ||
74 | + end | ||
75 | + end | ||
76 | + | ||
77 | + def per_page!(value) | ||
78 | + check_loaded | ||
79 | + @options[:per_page] = value | ||
80 | + self | ||
81 | + end | ||
82 | + | ||
58 | def loaded? | 83 | def loaded? |
59 | !@execute.nil? | 84 | !@execute.nil? |
60 | end | 85 | end |
test/pagination_test.rb
@@ -65,6 +65,32 @@ class PaginationTest < Minitest::Test | @@ -65,6 +65,32 @@ class PaginationTest < Minitest::Test | ||
65 | assert products.any? | 65 | assert products.any? |
66 | end | 66 | end |
67 | 67 | ||
68 | + def test_pagination_relation | ||
69 | + store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] | ||
70 | + products = Product.search("product", order: {name: :asc}, padding: 1).page(2).per_page(2) | ||
71 | + assert_equal ["Product D", "Product E"], products.map(&:name) | ||
72 | + assert_equal "product", products.entry_name | ||
73 | + assert_equal 2, products.current_page | ||
74 | + assert_equal 1, products.padding | ||
75 | + assert_equal 2, products.per_page | ||
76 | + assert_equal 2, products.size | ||
77 | + assert_equal 2, products.length | ||
78 | + assert_equal 3, products.total_pages | ||
79 | + assert_equal 6, products.total_count | ||
80 | + assert_equal 6, products.total_entries | ||
81 | + assert_equal 2, products.limit_value | ||
82 | + assert_equal 3, products.offset_value | ||
83 | + assert_equal 3, products.offset | ||
84 | + assert_equal 3, products.next_page | ||
85 | + assert_equal 1, products.previous_page | ||
86 | + assert_equal 1, products.prev_page | ||
87 | + assert !products.first_page? | ||
88 | + assert !products.last_page? | ||
89 | + assert !products.empty? | ||
90 | + assert !products.out_of_range? | ||
91 | + assert products.any? | ||
92 | + end | ||
93 | + | ||
68 | def test_pagination_body | 94 | def test_pagination_body |
69 | store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] | 95 | store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] |
70 | products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1) | 96 | products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1) |