Skip to content

Properly set limit in Kaminari #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/meilisearch/rails/pagination/kaminari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ def initialize(array, options)
end

def self.create(results, total_hits, options = {})
offset = ((options[:page] - 1) * options[:per_page])
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
unless MeiliSearch::Rails.active?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unless MeiliSearch::Rails.active?
unless Meilisearch::Rails.active?

To maintain compatibility with #384

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would if I knew which one would be merged first... Besides, there could be a patch level release with this PR while the rename is done in 0.15, that way 0.14 can have a stabler final version.

total_hits = 0
options[:page] = 1
options[:per_page] = 1
end

offset = (options[:page] - 1) * options[:per_page]
array = new results, limit: options[:per_page], 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
Expand Down
6 changes: 6 additions & 0 deletions spec/pagination/kaminari_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@
it 'respects max_total_hits' do
expect(Restaurant.search('*').count).to eq(2)
end

it 'correctly forwards parameters' do
# Kaminari only makes current_page available if
# limit and offset have been passed
expect(Restaurant.search('*').current_page).to eq(1)
end
end