Skip to content

Commit 4aa55a7

Browse files
meili-bors[bot]bendangeloellnixbrunoocasali
authored
376: For kaminari fixes zero results bug r=brunoocasali a=bendangelo # Pull Request ## Related issue Fixes [#<issue_number>](meilisearch#375) ## What does this PR do? Fixes this error if no results are found (nullobject is made into zero) ``` TypeError: no implicit conversion of MeiliSearch::Rails::NullObject into Integer ``` ## PR checklist Please check if your PR fulfills the following requirements: - [ x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [ x] Have you read the contributing guidelines? - [ x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Ben D'Angelo <[email protected]> Co-authored-by: ellnix <[email protected]> Co-authored-by: Bruno Casali <[email protected]>
2 parents 0086588 + f38ac9d commit 4aa55a7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/meilisearch/rails/pagination/kaminari.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ def initialize(array, options)
1919

2020
def self.create(results, total_hits, options = {})
2121
offset = ((options[:page] - 1) * options[:per_page])
22-
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
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
2326

2427
if array.empty? && !results.empty?
2528
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
2629
# otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
2730
results = Array.new(offset) + results
28-
array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
31+
array = new results, offset: offset, limit: limit, total_count: total_hits
2932
end
3033

3134
array

spec/pagination/kaminari_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
expect(p2).to contain_exactly(second)
3131
end
3232

33+
it "doesn't crash when meilisearch is disabled" do
34+
MeiliSearch::Rails.configuration[:active] = false
35+
36+
expect do
37+
Restaurant.search ''
38+
end.not_to raise_error
39+
40+
ensure
41+
MeiliSearch::Rails.configuration[:active] = true
42+
end
43+
3344
it 'returns number of total results' do
3445
hits = Restaurant.search ''
3546
expect(hits.total_count).to eq(2)

0 commit comments

Comments
 (0)