Skip to content

Commit

Permalink
Merge #376
Browse files Browse the repository at this point in the history
376: For kaminari fixes zero results bug r=brunoocasali a=bendangelo

# Pull Request

## Related issue
Fixes [#<issue_number>](#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]>
  • Loading branch information
4 people authored Jan 14, 2025
2 parents 0086588 + f38ac9d commit 4aa55a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/meilisearch/rails/pagination/kaminari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ def initialize(array, options)

def self.create(results, total_hits, options = {})
offset = ((options[:page] - 1) * options[:per_page])
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
total_hits = 0 if total_hits.nil?
offset = 0 if offset.nil?
limit = 0 if options[:per_page].nil?
array = new results, limit: limit, offset: offset, total_count: total_hits

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

array
Expand Down
11 changes: 11 additions & 0 deletions spec/pagination/kaminari_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
expect(p2).to contain_exactly(second)
end

it "doesn't crash when meilisearch is disabled" do
MeiliSearch::Rails.configuration[:active] = false

expect do
Restaurant.search ''
end.not_to raise_error

ensure
MeiliSearch::Rails.configuration[:active] = true
end

it 'returns number of total results' do
hits = Restaurant.search ''
expect(hits.total_count).to eq(2)
Expand Down

0 comments on commit 4aa55a7

Please sign in to comment.