Skip to content

Commit

Permalink
refactoring of elasticsearch extras
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 8, 2021
1 parent aecb565 commit 6966fe3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Version 4.0.0

### Breaking Changes

- renamed `Pagy::Search` as `Pagy::Searchkick` or `Pagy::ElasticsearchRails`

### Changes

- Dropped support for all jruby versions and ruby version <3.0 (use pagy 3.x instead)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@ Pagy is the ultimate pagination gem that outperforms the others in each and ever
## New in 4.0+

- **This version requires `ruby 3.0+`. For `ruby <3.0` use `pagy <4.0`**
- Updating `pagy` from `3.0+` to `4.0+` does not require any change in your code
- Updating `pagy` from `3.0+` to `4.0+` require a single renaming in your code, but only if it uses the `searchkick` or the `elasticsearch_rails` extras (see the [Changelog](https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md))
- Added the docker development environment to ease contributions

## Comparison with other gems
Expand Down
10 changes: 5 additions & 5 deletions docs/extras/elasticsearch_rails.md
Expand Up @@ -31,7 +31,7 @@ If you want Pagy to control the pagination, getting the page from the params, an
Extend your model:

```ruby
extend Pagy::Search
extend Pagy::ElasticsearchRails
```

In a controller use `pagy_search` in place of `search`:
Expand All @@ -56,15 +56,15 @@ This constructor accepts an `Elasticsearch::Model::Response::Response` as the fi

**Notice**: you have to take care of manually manage all the params for your search, however the method extracts/calculates the `:items`, `:page` and `:count` from the response object, so you don't need to pass that again. If you prefer to manage the pagination automatically, see below.

## Pagy::Search
## Pagy::ElasticsearchRails

Extend your model with the `Pagy::Search` micro-moudule (see [pagy_search.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/pagy_search.rb))
Extend your model with the `Pagy::ElasticsearchRails` micro-moudule:

```ruby
extend Pagy::Search
extend Pagy::ElasticsearchRails
```

The `Pagy::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.
The `Pagy::ElasticsearchRails` 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(...)

Expand Down
10 changes: 5 additions & 5 deletions docs/extras/searchkick.md
Expand Up @@ -31,7 +31,7 @@ If you want Pagy to control the pagination, getting the page from the params, an
Extend your model:

```ruby
extend Pagy::Search
extend Pagy::Searchkick
```

In a controller use `pagy_search` in place of `search`:
Expand All @@ -56,15 +56,15 @@ This constructor accepts a `Searchkick::Results` as the first argument, plus the

**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::Search
## Pagy::Searchkick

Extend your model with the `Pagy::Search` micro-moudule (see [pagy_search.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/pagy_search.rb))
Extend your model with the Pagy::Searchkick` micro-moudule:

```ruby
extend Pagy::Search
extend Pagy::Searchkick
```

The `Pagy::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.
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(...)

Expand Down
15 changes: 13 additions & 2 deletions lib/pagy/extras/elasticsearch_rails.rb
Expand Up @@ -2,10 +2,21 @@
# encoding: utf-8
# frozen_string_literal: true

require 'pagy/extras/pagy_search'

class Pagy

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
# the last search argument must be a hash option
def pagy_search(*search_args, &block)
search_args << {} unless search_args[-1].is_a?(Hash)
[self, search_args, block].tap do |args|
args.define_singleton_method(:method_missing){|*a| args += a}
end
end
end

# create a Pagy object from an Elasticsearch::Model::Response::Response object
def self.new_from_elasticsearch_rails(response, vars={})
vars[:items] = response.search.options[:size] || 10
Expand Down
18 changes: 0 additions & 18 deletions lib/pagy/extras/pagy_search.rb

This file was deleted.

25 changes: 17 additions & 8 deletions lib/pagy/extras/searchkick.rb
Expand Up @@ -2,10 +2,19 @@
# encoding: utf-8
# frozen_string_literal: true

require 'pagy/extras/pagy_search'

class Pagy

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)
[self, term, options, block].tap do |args|
args.define_singleton_method(:method_missing){|*a| args += a}
end
end
end

# create a Pagy object from a Searchkick::Results object
def self.new_from_searchkick(results, vars={})
vars[:items] = results.options[:per_page]
Expand All @@ -19,12 +28,12 @@ module Backend ; private

# Return Pagy object and results
def pagy_searchkick(pagy_search_args, vars={})
model, search_args, block, *called = pagy_search_args
vars = pagy_searchkick_get_vars(nil, vars)
search_args[-1][:per_page] = vars[:items]
search_args[-1][:page] = vars[:page]
results = model.search(*search_args, &block)
vars[:count] = results.total_count
model, term, options, block, *called = pagy_search_args
vars = pagy_searchkick_get_vars(nil, vars)
options[:per_page] = vars[:items]
options[:page] = vars[:page]
results = model.search(term, **options, &block)
vars[:count] = results.total_count
pagy = Pagy.new(vars)
# with :last_page overflow we need to re-run the method in order to get the hits
if defined?(Pagy::Overflow) && pagy.overflow? && pagy.vars[:overflow] == :last_page
Expand Down
4 changes: 2 additions & 2 deletions test/mock_helpers/elasticsearch_rails.rb
Expand Up @@ -41,7 +41,7 @@ def self.search(*args)
Response.new(*args)
end

extend Pagy::Search
extend Pagy::ElasticsearchRails
end

class ResponseES7 < Response
Expand All @@ -61,7 +61,7 @@ def self.search(*args)

end

class ResponseES5
class ResponseES5

attr_reader :search, :response

Expand Down
2 changes: 1 addition & 1 deletion test/mock_helpers/searchkick.rb
Expand Up @@ -35,7 +35,7 @@ def self.search(*args, &block)
Results.new(*args, &block)
end

extend Pagy::Search
extend Pagy::Searchkick
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/pagy/extras/elasticsearch_rails_test.rb
Expand Up @@ -7,7 +7,7 @@

SimpleCov.command_name 'elasticsearch' if ENV['RUN_SIMPLECOV'] == 'true'

describe Pagy::Search do
describe Pagy::ElasticsearchRails do

describe '#pagy_search' do

Expand Down
18 changes: 9 additions & 9 deletions test/pagy/extras/searchkick_test.rb
Expand Up @@ -7,7 +7,7 @@

SimpleCov.command_name 'elasticsearch' if ENV['RUN_SIMPLECOV'] == 'true'

describe Pagy::Search do
describe Pagy::Searchkick do

describe '#pagy_search' do

Expand All @@ -16,29 +16,29 @@
end

it 'returns class and arguments' do
_(MockSearchkick::Model.pagy_search('a', b:2)).must_equal [MockSearchkick::Model, ['a', {b: 2}], nil]
_(MockSearchkick::Model.pagy_search('a', b:2)).must_equal [MockSearchkick::Model, 'a', {b: 2}, nil]
args = MockSearchkick::Model.pagy_search('a', b:2){|a| a*2}
block = args[-1]
_(args).must_equal [MockSearchkick::Model, ['a', {b: 2}], block]
_(args).must_equal [MockSearchkick::Model, 'a', {b: 2}, block]
end

it 'allows the term argument to be optional' do
_(MockSearchkick::Model.pagy_search(b:2)).must_equal [MockSearchkick::Model, [{b: 2}], nil]
_(MockSearchkick::Model.pagy_search(b:2)).must_equal [MockSearchkick::Model, '*', {b: 2}, nil]
args = MockSearchkick::Model.pagy_search(b:2){|a| a*2}
block = args[-1]
_(args).must_equal [MockSearchkick::Model, [{b: 2}], block]
_(args).must_equal [MockSearchkick::Model, '*', {b: 2}, block]
end

it 'adds an empty option hash' do
_(MockSearchkick::Model.pagy_search('a')).must_equal [MockSearchkick::Model, ['a', {}], nil]
_(MockSearchkick::Model.pagy_search('a')).must_equal [MockSearchkick::Model, 'a', {}, nil]
args = MockSearchkick::Model.pagy_search('a'){|a| a*2}
block = args[-1]
_(args).must_equal [MockSearchkick::Model, ['a', {}], block]
_(args).must_equal [MockSearchkick::Model, 'a', {}, block]
end

it 'adds the caller and arguments' do
_(MockSearchkick::Model.pagy_search('a', b:2).results).must_equal [MockSearchkick::Model, ['a', {b: 2}], nil, :results]
_(MockSearchkick::Model.pagy_search('a', b:2).a('b', 2)).must_equal [MockSearchkick::Model, ['a', {b: 2}], nil, :a, 'b', 2]
_(MockSearchkick::Model.pagy_search('a', b:2).results).must_equal [MockSearchkick::Model, 'a', {b: 2}, nil, :results]
_(MockSearchkick::Model.pagy_search('a', b:2).a('b', 2)).must_equal [MockSearchkick::Model, 'a', {b: 2}, nil, :a, 'b', 2]
end

end
Expand Down

0 comments on commit 6966fe3

Please sign in to comment.