Sha256: 3f334eaed3d43a48a35fb321df30cf9d836f20d3bc3c4223b9896691582d86e2

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

unless defined? Kaminari
  raise(MeiliSearch::BadConfiguration,
        "Meilisearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
end

require 'kaminari/models/array_extension'

module MeiliSearch
  module Rails
    module Pagination
      class Kaminari < ::Kaminari::PaginatableArray
        def initialize(array, options)
          if RUBY_VERSION >= '3'
            super(array, **options)
          else
            super(array, options)
          end
        end

        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

          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
          end

          array
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
meilisearch-rails-0.14.1 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.14.0 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.12.0 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.11.1 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.11.0 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.10.2 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.10.1 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.10.0 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.9.1 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.9.0 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.8.1 lib/meilisearch/rails/pagination/kaminari.rb
meilisearch-rails-0.8.0 lib/meilisearch/rails/pagination/kaminari.rb