Skip to content

Commit fa37bc6

Browse files
joker1007karmi
authored andcommitted
Fixed the incorrect behaviour of limit() and page() methods
Previously: When klass.default_per_page is 25, response.limit(35).page(3) => :size is back to 25, :from is 25 * 2 response.page(3).limit(35) => :size is 35, but :from is 25 * 2 After this patch: response.limit(35).page(3) => :size is 35, :from is 35 * 2 response.page(3).limit(35) => :size is 35, :from is 35 * 2 Closes elastic#42 Related: elastic#72, elastic#55
1 parent f5708eb commit fa37bc6

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

Diff for: elasticsearch-model/lib/elasticsearch/model/response/pagination.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ def #{::Kaminari.config.page_method_name}(num=nil)
3030
@results = nil
3131
@records = nil
3232
@response = nil
33-
self.search.definition.update size: klass.default_per_page,
34-
from: klass.default_per_page * ([num.to_i, 1].max - 1)
33+
@page = [num.to_i, 1].max
34+
@per_page ||= klass.default_per_page
35+
36+
self.search.definition.update size: @per_page,
37+
from: @per_page * (@page - 1)
38+
3539
self
3640
end
3741
RUBY
@@ -69,7 +73,10 @@ def limit(value)
6973
@results = nil
7074
@records = nil
7175
@response = nil
72-
search.definition.update :size => value
76+
@per_page = value
77+
78+
search.definition.update :size => @per_page
79+
search.definition.update :from => @per_page * (@page - 1) if @page
7380
self
7481
end
7582

@@ -79,6 +86,7 @@ def offset(value)
7986
@results = nil
8087
@records = nil
8188
@response = nil
89+
@page = nil
8290
search.definition.update :from => value
8391
self
8492
end

Diff for: elasticsearch-model/test/integration/active_record_pagination_test.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,28 @@ class ::ArticleForPagination < ActiveRecord::Base
102102
assert_equal 12, records.size
103103
end
104104

105+
should "set the limit per request" do
106+
records = ArticleForPagination.search('title:test').limit(50).page(2).records
107+
108+
assert_equal 18, records.size
109+
assert_equal 2, records.current_page
110+
assert_equal 1, records.prev_page
111+
assert_equal nil, records.next_page
112+
assert_equal 2, records.total_pages
113+
114+
assert records.last_page?, "Should be the last page"
115+
end
116+
105117
context "with specific model settings" do
106118
teardown do
107119
ArticleForPagination.instance_variable_set(:@_default_per_page, nil)
108120
end
109-
end
110121

111-
should "respect paginates_per" do
112-
ArticleForPagination.paginates_per 50
122+
should "respect paginates_per" do
123+
ArticleForPagination.paginates_per 50
113124

114-
assert_equal 50, ArticleForPagination.search('*').page(1).records.size
125+
assert_equal 50, ArticleForPagination.search('*').page(1).records.size
126+
end
115127
end
116128
end
117129

Diff for: elasticsearch-model/test/unit/response_pagination_test.rb

+27
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,33 @@ def self.document_type; 'bar'; end
103103
end
104104
end
105105

106+
context "with the page() and limit() methods" do
107+
setup do
108+
@response.records
109+
@response.results
110+
end
111+
112+
should "set the values" do
113+
@response.page(3).limit(35)
114+
assert_equal 35, @response.search.definition[:size]
115+
assert_equal 70, @response.search.definition[:from]
116+
end
117+
118+
should "set the values when limit is called first" do
119+
@response.limit(35).page(3)
120+
assert_equal 35, @response.search.definition[:size]
121+
assert_equal 70, @response.search.definition[:from]
122+
end
123+
124+
should "reset the instance variables" do
125+
@response.page(3).limit(35)
126+
127+
assert_nil @response.instance_variable_get(:@response)
128+
assert_nil @response.instance_variable_get(:@records)
129+
assert_nil @response.instance_variable_get(:@results)
130+
end
131+
end
132+
106133
context "offset setter" do
107134
setup do
108135
@response.records

0 commit comments

Comments
 (0)