Skip to content

Commit 6b81a77

Browse files
ryanschkarmi
authored andcommitted
[MODEL] Added, that String pagination parameters are converted to numbers
Closes elastic#110
1 parent a78f492 commit 6b81a77

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ def offset_value
6666
# Set the "limit" (`size`) value
6767
#
6868
def limit(value)
69+
return self if value.to_i <= 0
6970
@results = nil
7071
@records = nil
7172
@response = nil
72-
@per_page = value
73+
@per_page = value.to_i
7374

7475
search.definition.update :size => @per_page
7576
search.definition.update :from => @per_page * (@page - 1) if @page
@@ -79,11 +80,12 @@ def limit(value)
7980
# Set the "offset" (`from`) value
8081
#
8182
def offset(value)
83+
return self if value.to_i < 0
8284
@results = nil
8385
@records = nil
8486
@response = nil
8587
@page = nil
86-
search.definition.update :from => value
88+
search.definition.update :from => value.to_i
8789
self
8890
end
8991

elasticsearch-model/test/unit/response_pagination_kaminari_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ def self.document_type; 'bar'; end
104104
assert_nil @response.instance_variable_get(:@records)
105105
assert_nil @response.instance_variable_get(:@results)
106106
end
107+
108+
should 'coerce string parameters' do
109+
@response.limit("35")
110+
assert_equal 35, @response.search.definition[:size]
111+
end
112+
113+
should 'ignore invalid string parameters' do
114+
@response.limit(35)
115+
@response.limit("asdf")
116+
assert_equal 35, @response.search.definition[:size]
117+
end
107118
end
108119

109120
context "with the page() and limit() methods" do
@@ -151,6 +162,17 @@ def self.document_type; 'bar'; end
151162
assert_nil @response.instance_variable_get(:@records)
152163
assert_nil @response.instance_variable_get(:@results)
153164
end
165+
166+
should 'coerce string parameters' do
167+
@response.offset("35")
168+
assert_equal 35, @response.search.definition[:from]
169+
end
170+
171+
should 'coerce invalid string parameters' do
172+
@response.offset(35)
173+
@response.offset("asdf")
174+
assert_equal 0, @response.search.definition[:from]
175+
end
154176
end
155177

156178
context "total" do

0 commit comments

Comments
 (0)