Skip to content

Commit d9777fd

Browse files
balexandkarmi
authored andcommitted
[MODEL] Added the offset method to the "will_paginate" integration
Closes elastic#135 Related: elastic#131, elastic#135, elastic#175
1 parent a496d58 commit d9777fd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

elasticsearch-model/lib/elasticsearch/model/response/pagination.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ def self.included(base)
102102

103103
# Include the paging methods in results and records
104104
#
105-
methods = [:current_page, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
105+
methods = [:current_page, :offset, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
106106
Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
107107
Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
108108
end
109109

110+
def offset
111+
(current_page - 1) * per_page
112+
end
113+
110114
# Main pagination method
111115
#
112116
# @example

elasticsearch-model/test/unit/response_pagination_will_paginate_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
3030
@expected_methods = [
3131
# methods needed by WillPaginate::CollectionMethods
3232
:current_page,
33+
:offset,
3334
:per_page,
3435
:total_entries,
3536

@@ -66,6 +67,13 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
6667
end
6768
end
6869

70+
context "#offset method" do
71+
should "calculate offset using current_page and per_page" do
72+
@response.per_page(3).page(3)
73+
assert_equal 6, @response.offset
74+
end
75+
end
76+
6977
context "#paginate method" do
7078
should "set from/size using defaults" do
7179
@response.klass.client

0 commit comments

Comments
 (0)