File tree 3 files changed +54
-7
lines changed
lib/elasticsearch/model/response
3 files changed +54
-7
lines changed Original file line number Diff line number Diff line change @@ -30,8 +30,12 @@ def #{::Kaminari.config.page_method_name}(num=nil)
30
30
@results = nil
31
31
@records = nil
32
32
@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
+
35
39
self
36
40
end
37
41
RUBY
@@ -69,7 +73,10 @@ def limit(value)
69
73
@results = nil
70
74
@records = nil
71
75
@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
73
80
self
74
81
end
75
82
@@ -79,6 +86,7 @@ def offset(value)
79
86
@results = nil
80
87
@records = nil
81
88
@response = nil
89
+ @page = nil
82
90
search . definition . update :from => value
83
91
self
84
92
end
Original file line number Diff line number Diff line change @@ -102,16 +102,28 @@ class ::ArticleForPagination < ActiveRecord::Base
102
102
assert_equal 12 , records . size
103
103
end
104
104
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
+
105
117
context "with specific model settings" do
106
118
teardown do
107
119
ArticleForPagination . instance_variable_set ( :@_default_per_page , nil )
108
120
end
109
- end
110
121
111
- should "respect paginates_per" do
112
- ArticleForPagination . paginates_per 50
122
+ should "respect paginates_per" do
123
+ ArticleForPagination . paginates_per 50
113
124
114
- assert_equal 50 , ArticleForPagination . search ( '*' ) . page ( 1 ) . records . size
125
+ assert_equal 50 , ArticleForPagination . search ( '*' ) . page ( 1 ) . records . size
126
+ end
115
127
end
116
128
end
117
129
Original file line number Diff line number Diff line change @@ -103,6 +103,33 @@ def self.document_type; 'bar'; end
103
103
end
104
104
end
105
105
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
+
106
133
context "offset setter" do
107
134
setup do
108
135
@response . records
You can’t perform that action at this time.
0 commit comments