This extra deals with the pagination of
Searchkick::Results objects either by creating a
Pagy object out of an (already paginated)
Searchkick::Results object or by creating the
Searchkick::Results objects from the backend params.
See extras for general usage info.
Require the extra in the
require 'pagy/extras/searchkick' Searchkick.extend Pagy::Searchkick # optional
If you have an already paginated
Searchkick::Results object, you can get the
Pagy object out of it:
@results = Model.search('*', page: 1, per_page: 10, ...) @pagy = Pagy.new_from_searchkick(@results, ...)
If you want Pagy to control the pagination, getting the page from the params, and returning both the
Pagy and the
Searchkick::Results objects automatically (from the backend params):
Extend your model:
In a controller use
pagy_search in place of
results = Article.pagy_search(params[:q]) @pagy, @results = pagy_searchkick(results, items: 10)
Extend also the
Searchkick module if you are going to use
# config/initializers/pagy.rb Searchkick.extend Pagy::Searchkick
pagy_search in place of
results = Searchkick.pagy_search(params[:q], models: [Article, Categories]) @pagy, @results = pagy_searchkick(results, items: 10)
This constructor accepts a
Searchkick::Results as the first argument, plus the usual optional variable hash. It sets the
:count pagy variables extracted/calculated out of the
@results = Model.search('*', page: 2, per_page: 10, ...) @pagy = Pagy.new_from_searchkick(@results, ...)
Notice: you have to take care of manually manage all the params for your search, however the method extracts the
:count from the results object, so you don’t need to pass that again. If you prefer to manage the pagination automatically, see below.
Extend your model with the
Pagy::Searchkick adds the
pagy_search class method that you must use in place of the standard
search method when you want to paginate the search response.
This method accepts the same arguments of the
search method and you must use it in its place. This extra uses it in order to capture the arguments, automatically merging the calculated
:per_page options before passing them to the standard
search method internally.
||customizable name of the search method||
This extra adds the
pagy_searchkick method to the
Pagy::Backend to be used when you have to paginate a
Searchkick::Results object. It also adds a
pagy_searchkick_get_vars sub-method, used for easy customization of variables by overriding.
This method is similar to the generic
pagy method, but specialized for Searchkick. (see the pagy doc)
It expects to receive a
Model.pagy_search(...) result and returns a paginated response. You can use it in a couple of ways:
@pagy, @results = pagy_searchkick(Model.pagy_search(params[:q]), ...) ... @records = @results.results # or directly with the collection you need (e.g. records) @pagy, @records = pagy_searchkick(Model.pagy_search(params[:q]).results, ...)
This sub-method is similar to the
pagy_get_vars sub-method, but it is called only by the
pagy_searchkick method. (see the pagy_get_vars doc).