diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index 20dd9f7..9d4586e 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -55,6 +55,31 @@ module Searchkick self end + def page(value) + clone.page!(value) + end + + def page!(value) + check_loaded + @options[:page] = value + self + end + + def per_page(value = NO_DEFAULT_VALUE) + # TODO remove in Searchkick 6 + if value == NO_DEFAULT_VALUE + private_execute.per_page + else + clone.per_page!(value) + end + end + + def per_page!(value) + check_loaded + @options[:per_page] = value + self + end + def loaded? !@execute.nil? end diff --git a/test/pagination_test.rb b/test/pagination_test.rb index 9b59381..b394077 100644 --- a/test/pagination_test.rb +++ b/test/pagination_test.rb @@ -65,6 +65,32 @@ class PaginationTest < Minitest::Test assert products.any? end + def test_pagination_relation + store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] + products = Product.search("product", order: {name: :asc}, padding: 1).page(2).per_page(2) + assert_equal ["Product D", "Product E"], products.map(&:name) + assert_equal "product", products.entry_name + assert_equal 2, products.current_page + assert_equal 1, products.padding + assert_equal 2, products.per_page + assert_equal 2, products.size + assert_equal 2, products.length + assert_equal 3, products.total_pages + assert_equal 6, products.total_count + assert_equal 6, products.total_entries + assert_equal 2, products.limit_value + assert_equal 3, products.offset_value + assert_equal 3, products.offset + assert_equal 3, products.next_page + assert_equal 1, products.previous_page + assert_equal 1, products.prev_page + assert !products.first_page? + assert !products.last_page? + assert !products.empty? + assert !products.out_of_range? + assert products.any? + end + def test_pagination_body store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1) -- libgit2 0.21.0