Skip to content

Commit

Permalink
added pagy_search name customization to elasticsearch extras
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 9, 2021
1 parent 98a5254 commit 296f060
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/extras/elasticsearch_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ The `Pagy::ElasticsearchRails` adds the `pagy_search` class method that you must

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 `:from` and `:size` options before passing them to the standard `search` method internally.

## Variables

| Variable | Description | Default |
|:-------------------------------------|:-------------------------------------------------------------------------------------------|:-------------|
| `:elasticsearch_rails_search_method` | customizable name of the `:pagy_search` method | :pagy_search |

## Methods

This extra adds the `pagy_elasticsearch_rails` method to the `Pagy::Backend` to be used when you have to paginate a `ElasticsearchRails` object. It also adds a `pagy_elasticsearch_rails_get_variables` sub-method, used for easy customization of variables by overriding.
Expand Down
6 changes: 6 additions & 0 deletions docs/extras/searchkick.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ The `Pagy::ElasticsearchRails::Search` adds the `pagy_search` class method that

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 `:page` and `:per_page` options before passing them to the standard `search` method internally.

## Variables

| Variable | | Description | Default |
|:----------------------------|:-----------------------------------------------|:---------------|:--------|
| `:searchkick_search_method` | customizable name of the `:pagy_search` method | `:pagy_search` | |

## Methods

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.
Expand Down
7 changes: 6 additions & 1 deletion lib/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@

# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
# default :pagy_search method: change only if you use
# also the searchkick extra that defines the same
# VARS[:elasticsearch_rails_search_method] = :pagy_search
# require 'pagy/extras/elasticsearch_rails'

# Searchkick extra: Paginate `Searchkick::Results` objects
# See https://ddnexus.github.io/pagy/extras/searchkick
# default :pagy_search method: change only if you use
# also the elasticsearch_rails extra that defines the same
# VARS[:searchkick_search_method] = :pagy_search
# require 'pagy/extras/searchkick'


# Frontend Extras

# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
Expand Down
7 changes: 5 additions & 2 deletions lib/pagy/extras/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

class Pagy

VARS[:elasticsearch_rails_search_method] ||= :pagy_search

module ElasticsearchRails
# returns an array used to delay the call of #search
# after the pagination variables are merged to the options
# it also pushes to the same array an eventually called method and arguments
def pagy_search(query_or_payload, **options)
# it also pushes to the same array an eventually called method
def pagy_elasticsearch_rails(query_or_payload, **options)
[self, query_or_payload, options].tap do |args|
args.define_singleton_method(:method_missing){|*a| args += a}
end
end
alias_method VARS[:elasticsearch_rails_search_method], :pagy_elasticsearch_rails
end

# create a Pagy object from an Elasticsearch::Model::Response::Response object
Expand Down
7 changes: 5 additions & 2 deletions lib/pagy/extras/searchkick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

class Pagy

VARS[:searchkick_search_method] ||= :pagy_search

module Searchkick
# returns an array used to delay the call of #search
# after the pagination variables are merged to the options
# it also pushes to the same array an eventually called method and arguments
def pagy_search(term = "*", **options, &block)
# it also pushes to the same array an eventually called method
def pagy_searchkick(term = "*", **options, &block)
[self, term, options, block].tap do |args|
args.define_singleton_method(:method_missing){|*a| args += a}
end
end
alias_method VARS[:searchkick_search_method], :pagy_searchkick
end

# create a Pagy object from a Searchkick::Results object
Expand Down

0 comments on commit 296f060

Please sign in to comment.