Skip to content

Commit f676a15

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

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def self.included(base)
102102

103103
# Include the paging methods in results and records
104104
#
105-
methods = [:current_page, :offset, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
105+
methods = [:current_page, :offset, :length, :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
@@ -111,6 +111,10 @@ def offset
111111
(current_page - 1) * per_page
112112
end
113113

114+
def length
115+
search.definition[:size]
116+
end
117+
114118
# Main pagination method
115119
#
116120
# @example

elasticsearch-model/test/unit/response_pagination_will_paginate_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
3333
:offset,
3434
:per_page,
3535
:total_entries,
36+
:length,
3637

3738
# methods defined by WillPaginate::CollectionMethods
3839
:total_pages,
@@ -73,6 +74,12 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
7374
assert_equal 6, @response.offset
7475
end
7576
end
77+
context "#length method" do
78+
should "return count of paginated results" do
79+
@response.per_page(3).page(3)
80+
assert_equal 3, @response.length
81+
end
82+
end
7683

7784
context "#paginate method" do
7885
should "set from/size using defaults" do

0 commit comments

Comments
 (0)