-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
396 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
pagy (4.8.1) | ||
pagy (4.9.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
title: Meilisearch | ||
--- | ||
# Meilisearch Extra | ||
|
||
This extra deals with the pagination of `Meilisearch` results either by creating a `Pagy` object out of an (already paginated) `Meilisearch` results or by creating the `Pagy` and `Meilisearch` results from the backend params. | ||
|
||
## Synopsis | ||
|
||
See [extras](../extras.md) for general usage info. | ||
|
||
Require the extra in the `pagy.rb` initializer: | ||
|
||
```ruby | ||
require 'pagy/extras/meilisearch' | ||
``` | ||
|
||
### Passive mode | ||
|
||
If you have an already paginated `Meilisearch` results, you can get the `Pagy` object out of it: | ||
|
||
```ruby | ||
@results = Model.search(nil, offset: 10, limit: 10, ...) | ||
@pagy = Pagy.new_from_meilisearch(@results, ...) | ||
``` | ||
|
||
### Active Mode | ||
|
||
If you want Pagy to control the pagination, getting the page from the params, and returning both the `Pagy` and the Meilisearch results automatically (from the backend params): | ||
|
||
Extend your model: | ||
|
||
```ruby | ||
extend Pagy::Meilisearch | ||
``` | ||
|
||
In a controller use `pagy_search` in place of `search`: | ||
|
||
```ruby | ||
results = Article.pagy_search(params[:q]) | ||
@pagy, @results = pagy_meilisearch(results, items: 10) | ||
``` | ||
|
||
## Files | ||
|
||
- [meilisearch.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/meilisearch.rb) | ||
|
||
## Pagy.new_from_meilisearch | ||
|
||
This constructor accepts a Meilisearch as the first argument, plus the usual optional variable hash. It sets the `:items`, `:page` and `:count` pagy variables extracted/calculated out of the Meilisearch object. | ||
|
||
```ruby | ||
@results = Model.search(nil, offset: 10, limit: 10, ...) | ||
@pagy = Pagy.new_from_meilisearch(@results, ...) | ||
``` | ||
|
||
**Notice**: you have to take care of manually manage all the params for your search, however the method extracts the `:items`, `:page` and `:count` from the results object, so you don't need to pass that again. If you prefer to manage the pagination automatically, see below. | ||
|
||
## Pagy::Meilisearch | ||
|
||
Extend your model with the Pagy::Meilisearch` micro-moudule: | ||
|
||
```ruby | ||
extend Pagy::Meilisearch | ||
``` | ||
|
||
The `Pagy::ElasticsearchRails::Search` 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. | ||
|
||
### pagy_search(...) | ||
|
||
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 `:offset` and `:limit` options before passing them to the standard `search` method internally. | ||
|
||
## Variables | ||
|
||
| Variable | | Description | Default | | ||
|:----------------------------|:-----------------------------------------------|:---------------|:--------| | ||
| `:meilisearch_search_method` | customizable name of the `:pagy_search` method | `:pagy_search` | | | ||
|
||
## Methods | ||
|
||
This extra adds the `pagy_meilisearch` method to the `Pagy::Backend` to be used when you have to paginate a Meilisearch object. It also adds a `pagy_meilisearch_get_vars` sub-method, used for easy customization of variables by overriding. | ||
|
||
### pagy_meilisearch(Model.pagy_search(...), vars={}}) | ||
|
||
This method is similar to the generic `pagy` method, but specialized for Meilisearch. (see the [pagy doc](../api/backend.md#pagycollection-varsnil)) | ||
|
||
It expects to receive a `Model.pagy_search(...)` result and returns a paginated response. You can use it in a couple of ways: | ||
|
||
```ruby | ||
@pagy, @results = pagy_meilisearch(Model.pagy_search(params[:q]), ...) | ||
... | ||
@records = @results.results | ||
|
||
# or directly with the collection you need (e.g. records) | ||
@pagy, @records = pagy_meilisearch(Model.pagy_search(params[:q]).results, ...) | ||
``` | ||
|
||
### pagy_meilisearch_get_vars(array) | ||
|
||
This sub-method is similar to the `pagy_get_vars` sub-method, but it is called only by the `pagy_meilisearch` method. (see the [pagy_get_vars doc](../api/backend.md#pagy_get_varscollection-vars)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
function Pagy(){} | ||
|
||
Pagy.version = '4.8.1' | ||
Pagy.version = '4.9.0' | ||
|
||
Pagy.delay = 100 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class Pagy | ||
|
||
VARS[:meilisearch_search_method] ||= :pagy_search | ||
|
||
module Meilisearch | ||
# returns an array used to delay the call of #search | ||
# after the pagination variables are merged to the options | ||
def pagy_meilisearch(term = nil, **vars) | ||
[self, term, vars] | ||
end | ||
alias_method VARS[:meilisearch_search_method], :pagy_meilisearch | ||
end | ||
|
||
# create a Pagy object from a Meilisearch results | ||
def self.new_from_meilisearch(results, vars={}) | ||
vars[:items] = results.raw_answer[:limit] | ||
vars[:page] = [results.raw_answer[:offset] / vars[:items], 1].max | ||
vars[:count] = results.raw_answer[:nbHits] | ||
new(vars) | ||
end | ||
|
||
# Add specialized backend methods to paginate Meilisearch results | ||
module Backend | ||
private | ||
|
||
# Return Pagy object and results | ||
def pagy_meilisearch(pagy_search_args, vars = {}) | ||
model, term, options = pagy_search_args | ||
vars = pagy_meilisearch_get_vars(nil, vars) | ||
options[:limit] = vars[:items] | ||
options[:offset] = (vars[:page] - 1) * vars[:items] | ||
results = model.search(term, **options) | ||
vars[:count] = results.raw_answer[:nbHits] | ||
|
||
pagy = Pagy.new(vars) | ||
# with :last_page overflow we need to re-run the method in order to get the hits | ||
return pagy_meilisearch(pagy_search_args, vars.merge(page: pagy.page)) \ | ||
if defined?(Pagy::UseOverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page | ||
|
||
[ pagy, results ] | ||
end | ||
|
||
# Sub-method called only by #pagy_meilisearch: here for easy customization of variables by overriding | ||
# the _collection argument is not available when the method is called | ||
def pagy_meilisearch_get_vars(_collection, vars) | ||
pagy_set_items_from_params(vars) if defined?(UseItemsExtra) | ||
vars[:items] ||= VARS[:items] | ||
vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i | ||
vars | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ test_tasks = {} | |
i18n | ||
items | ||
items_trim | ||
meilisearch | ||
overflow | ||
searchkick | ||
shared_json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pagy/extras/meilisearch' | ||
|
||
module MockMeilisearch | ||
|
||
RESULTS = { 'a' => ('a-1'..'a-1000').to_a, | ||
'b' => ('b-1'..'b-1000').to_a }.freeze | ||
|
||
class Results < Array | ||
|
||
def initialize(query, params = {}) | ||
@query = query | ||
@params = { offset: 0, limit: 10_000 }.merge(params) | ||
super RESULTS[@query].slice(@params[:offset], @params[:limit]) || [] | ||
end | ||
|
||
def raw_answer | ||
{ | ||
hits: self, | ||
offset: @params[:offset], | ||
limit: @params[:limit], | ||
nbHits: RESULTS[@query].length | ||
} | ||
end | ||
end | ||
|
||
class Model | ||
|
||
def self.search(*args) | ||
Results.new(*args) | ||
end | ||
|
||
extend Pagy::Meilisearch | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.