Sha256: f7967d1904de5749165f6ffa0d17a52034d481fe3d7befd0cad0d7dc71bc6962

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 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 Pagination
    class Kaminari < ::Kaminari::PaginatableArray
      def initialize(array, options)
        if RUBY_VERSION >= '3'
          super(array, **options)
        else
          super(array, options)
        end
      end

      def limit(_num)
        # noop
        self
      end

      def offset(_num)
        # noop
        self
      end

      class << self
        def 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 = ([nil] * 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

3 entries across 3 versions & 1 rubygems

Version Path
meilisearch-rails-0.4.1 lib/meilisearch/pagination/kaminari.rb
meilisearch-rails-0.4.0 lib/meilisearch/pagination/kaminari.rb
meilisearch-rails-0.3.0 lib/meilisearch/pagination/kaminari.rb