Skip to content

Commit 6afcd40

Browse files
395: Properly set `limit` in Kaminari r=ellnix a=ellnix Fixes regression in meilisearch#376 Fixes meilisearch#394 Co-authored-by: ellnix <[email protected]>
2 parents 385b278 + 0e06892 commit 6afcd40

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/meilisearch/rails/pagination/kaminari.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ def initialize(array, options)
1818
end
1919

2020
def self.create(results, total_hits, options = {})
21-
offset = ((options[:page] - 1) * options[:per_page])
22-
total_hits = 0 if total_hits.nil?
23-
offset = 0 if offset.nil?
24-
limit = 0 if options[:per_page].nil?
25-
array = new results, limit: limit, offset: offset, total_count: total_hits
21+
unless MeiliSearch::Rails.active?
22+
total_hits = 0
23+
options[:page] = 1
24+
options[:per_page] = 1
25+
end
26+
27+
offset = (options[:page] - 1) * options[:per_page]
28+
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
2629

2730
if array.empty? && !results.empty?
2831
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param

spec/pagination/kaminari_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@
7070
it 'respects max_total_hits' do
7171
expect(Restaurant.search('*').count).to eq(2)
7272
end
73+
74+
it 'correctly forwards parameters' do
75+
# Kaminari only makes current_page available if
76+
# limit and offset have been passed
77+
expect(Restaurant.search('*').current_page).to eq(1)
78+
end
7379
end

0 commit comments

Comments
 (0)