-
Notifications
You must be signed in to change notification settings - Fork 802
Support for will_paginate #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,47 @@ def total_count | |
results.total | ||
end | ||
end | ||
|
||
# Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate] | ||
# | ||
module WillPaginate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add just a small preamble comment here, similar as what we have for Kaminari at L8? |
||
def self.included(base) | ||
base.__send__ :include, ::WillPaginate::CollectionMethods | ||
|
||
methods = [:current_page, :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 paginate(options) | ||
page = [options[:page].to_i, 1].max | ||
per_page = (options[:per_page] || klass.per_page).to_i | ||
|
||
search.definition.update size: per_page, | ||
from: (page - 1) * per_page | ||
self | ||
end | ||
|
||
def current_page | ||
search.definition[:from] / per_page + 1 if search.definition[:from] && per_page | ||
end | ||
|
||
def page(num) | ||
paginate(page: num, per_page: per_page) # shorthand | ||
end | ||
|
||
def per_page(num = nil) | ||
if num.nil? | ||
search.definition[:size] | ||
else | ||
paginate(page: current_page, per_page: num) # shorthand | ||
end | ||
end | ||
|
||
def total_entries | ||
results.total | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
2 changes: 1 addition & 1 deletion
2
...del/test/unit/response_pagination_test.rb → ...unit/response_pagination_kaminari_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
elasticsearch-model/test/unit/response_pagination_will_paginate_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
require 'test_helper' | ||
require 'will_paginate' | ||
require 'will_paginate/collection' | ||
|
||
class Elasticsearch::Model::ResponsePaginationWillPaginateTest < Test::Unit::TestCase | ||
context "Response pagination" do | ||
class ModelClass | ||
def self.index_name; 'foo'; end | ||
def self.document_type; 'bar'; end | ||
|
||
# WillPaginate adds this method to models (see WillPaginate::PerPage module) | ||
def self.per_page | ||
33 | ||
end | ||
end | ||
|
||
# Subsclass Response so we can include WillPaginate module without conflicts with Kaminari. | ||
class WillPaginateResponse < Elasticsearch::Model::Response::Response | ||
include Elasticsearch::Model::Response::Pagination::WillPaginate | ||
end | ||
|
||
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, | ||
'hits' => { 'total' => 100, 'hits' => (1..100).to_a.map { |i| { _id: i } } } } | ||
|
||
setup do | ||
@search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*' | ||
@response = WillPaginateResponse.new ModelClass, @search, RESPONSE | ||
@response.klass.stubs(:client).returns mock('client') | ||
|
||
@expected_methods = [ | ||
# methods needed by WillPaginate::CollectionMethods | ||
:current_page, | ||
:per_page, | ||
:total_entries, | ||
|
||
# methods defined by WillPaginate::CollectionMethods | ||
:total_pages, | ||
:previous_page, | ||
:next_page, | ||
:out_of_bounds?, | ||
] | ||
end | ||
|
||
should "have pagination methods" do | ||
assert_respond_to @response, :paginate | ||
|
||
@expected_methods.each do |method| | ||
assert_respond_to @response, method | ||
end | ||
end | ||
|
||
context "response.results" do | ||
should "have pagination methods" do | ||
@expected_methods.each do |method| | ||
assert_respond_to @response.results, method | ||
end | ||
end | ||
end | ||
|
||
context "response.records" do | ||
should "have pagination methods" do | ||
@expected_methods.each do |method| | ||
@response.klass.stubs(:find).returns([]) | ||
assert_respond_to @response.records, method | ||
end | ||
end | ||
end | ||
|
||
context "#paginate method" do | ||
should "set from/size using defaults" do | ||
@response.klass.client | ||
.expects(:search) | ||
.with do |definition| | ||
assert_equal 0, definition[:from] | ||
assert_equal 33, definition[:size] | ||
end | ||
.returns(RESPONSE) | ||
|
||
assert_nil @response.search.definition[:from] | ||
assert_nil @response.search.definition[:size] | ||
|
||
@response.paginate(page: nil).to_a | ||
assert_equal 0, @response.search.definition[:from] | ||
assert_equal 33, @response.search.definition[:size] | ||
end | ||
|
||
should "set from/size using default per_page" do | ||
@response.klass.client | ||
.expects(:search) | ||
.with do |definition| | ||
assert_equal 33, definition[:from] | ||
assert_equal 33, definition[:size] | ||
end | ||
.returns(RESPONSE) | ||
|
||
assert_nil @response.search.definition[:from] | ||
assert_nil @response.search.definition[:size] | ||
|
||
@response.paginate(page: 2).to_a | ||
assert_equal 33, @response.search.definition[:from] | ||
assert_equal 33, @response.search.definition[:size] | ||
end | ||
|
||
should "set from/size using custom page and per_page" do | ||
@response.klass.client | ||
.expects(:search) | ||
.with do |definition| | ||
assert_equal 18, definition[:from] | ||
assert_equal 9, definition[:size] | ||
end | ||
.returns(RESPONSE) | ||
|
||
assert_nil @response.search.definition[:from] | ||
assert_nil @response.search.definition[:size] | ||
|
||
@response.paginate(page: 3, per_page: 9).to_a | ||
assert_equal 18, @response.search.definition[:from] | ||
assert_equal 9, @response.search.definition[:size] | ||
end | ||
|
||
should "searches for page 1 if specified page is < 1" do | ||
@response.klass.client | ||
.expects(:search) | ||
.with do |definition| | ||
assert_equal 0, definition[:from] | ||
assert_equal 33, definition[:size] | ||
end | ||
.returns(RESPONSE) | ||
|
||
assert_nil @response.search.definition[:from] | ||
assert_nil @response.search.definition[:size] | ||
|
||
@response.paginate(page: "-1").to_a | ||
assert_equal 0, @response.search.definition[:from] | ||
assert_equal 33, @response.search.definition[:size] | ||
end | ||
end | ||
|
||
context "#page and #per_page shorthand methods" do | ||
should "set from/size using default per_page" do | ||
@response.page(5) | ||
assert_equal 132, @response.search.definition[:from] | ||
assert_equal 33, @response.search.definition[:size] | ||
end | ||
|
||
should "set from/size when calling #page then #per_page" do | ||
@response.page(5).per_page(3) | ||
assert_equal 12, @response.search.definition[:from] | ||
assert_equal 3, @response.search.definition[:size] | ||
end | ||
|
||
should "set from/size when calling #per_page then #page" do | ||
@response.per_page(3).page(5) | ||
assert_equal 12, @response.search.definition[:from] | ||
assert_equal 3, @response.search.definition[:size] | ||
end | ||
end | ||
|
||
context "#current_page method" do | ||
should "return 1 by default" do | ||
@response.paginate({}) | ||
assert_equal 1, @response.current_page | ||
end | ||
|
||
should "return current page number" do | ||
@response.paginate(page: 3, per_page: 9) | ||
assert_equal 3, @response.current_page | ||
end | ||
|
||
should "return nil if not pagination set" do | ||
assert_equal nil, @response.current_page | ||
end | ||
end | ||
|
||
context "#per_page method" do | ||
should "return value set in paginate call" do | ||
@response.paginate(per_page: 8) | ||
assert_equal 8, @response.per_page | ||
end | ||
end | ||
|
||
context "#total_entries method" do | ||
should "return total from response" do | ||
@response.expects(:results).returns(mock('results', total: 100)) | ||
assert_equal 100, @response.total_entries | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If desired, I can remove this development dependency and instead stub
WillPaginate::CollectionMethods
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's perfectly fine to have it as a development dependency.