Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,19 @@ def self.included(base)

# Include the paging methods in results and records
#
methods = [:current_page, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
end

def offset
(current_page - 1) * per_page
end

def length
search.definition[:size]
end

# Main pagination method
#
# @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
@expected_methods = [
# methods needed by WillPaginate::CollectionMethods
:current_page,
:offset,
:per_page,
:total_entries,
:length,

# methods defined by WillPaginate::CollectionMethods
:total_pages,
Expand Down Expand Up @@ -66,6 +68,19 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
end
end

context "#offset method" do
should "calculate offset using current_page and per_page" do
@response.per_page(3).page(3)
assert_equal 6, @response.offset
end
end
context "#length method" do
should "return count of paginated results" do
@response.per_page(3).page(3)
assert_equal 3, @response.length
end
end

context "#paginate method" do
should "set from/size using defaults" do
@response.klass.client
Expand Down