-
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.
Added Elasticsearch Rails extra (#96)
- Loading branch information
Showing
7 changed files
with
159 additions
and
16 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
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,43 @@ | ||
--- | ||
title: Elasticsearch Rails | ||
--- | ||
# Elasticsearch Rails Extra | ||
|
||
Paginate `ElasticsearchRails::Results` objects efficiently avoiding expensive object-wrapping and without overriding. | ||
|
||
## Synopsys | ||
|
||
See [extras](../extras.md) for general usage info. | ||
|
||
In a controller: | ||
|
||
```ruby | ||
def search | ||
@pagy, @articles = pagy_elasticsearch_rails(Article.search(params[:q]).records, items: 10) | ||
|
||
render action: "index" | ||
end | ||
|
||
# independently paginate some other collections as usual | ||
@pagy_b, @records = pagy(some_scope, ...) | ||
``` | ||
|
||
## Files | ||
|
||
This extra is composed of 1 file: | ||
|
||
- [elasticsearch_rails.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/elasticsearch_rails.rb) | ||
|
||
## Methods | ||
|
||
This extra adds the `pagy_elasticsearch_rails` method to the `Pagy::Backend` to be used in place (or in parallel) of the `pagy` method when you have to paginate a `ElasticsearchRails::Results` object. It also adds a `pagy_elasticsearch_rails_get_variables` sub-method, used for easy customization of variables by overriding. | ||
|
||
**Notice**: there is no `pagy_elasticsearch_rails_get_items` method to override, since the items are fetched directly by Elasticsearch Rails. | ||
|
||
### pagy_elasticsearch_rails(Model.search(...), vars=nil) | ||
|
||
This method is the same as the generic `pagy` method, but specialized for Elasticsearch Rails. (see the [pagy doc](../api/backend.md#pagycollection-varsnil)) | ||
|
||
### pagy_elastic_search_rails_get_vars(array) | ||
|
||
This sub-method is the same as the `pagy_get_vars` sub-method, but it is called only by the `pagy_elasticsearch_rails` 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/elasticsearch_rails | ||
|
||
class Pagy | ||
# Add specialized backend methods to paginate ElasticsearchRails::Results | ||
module Backend ; private | ||
# Return Pagy object and items | ||
def pagy_elasticsearch_rails(results, vars={}) | ||
pagy = Pagy.new(pagy_elasticsearch_rails_get_vars(results, vars)) | ||
return pagy, results.offset(pagy.offset).limit(pagy.items) | ||
end | ||
|
||
def pagy_elasticsearch_rails_get_vars(results, vars) | ||
# Return the merged variables to initialize the Pagy object | ||
{ count: results.total, | ||
page: (params[:page] || 1)}.merge!(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require_relative '../../test_helper' | ||
require 'pagy/extras/elasticsearch_rails' | ||
|
||
SingleCov.covered! | ||
|
||
describe Pagy::Backend do | ||
|
||
let(:backend) { TestController.new } | ||
|
||
class ElasticsearchRails | ||
class Results | ||
def initialize(params); @params = params; end | ||
def total; 1000; end | ||
def offset(value); self; end | ||
def limit(value); 25; end | ||
def page | ||
@params[:page] || 1 | ||
end | ||
end | ||
end | ||
|
||
describe "#pagy_elasticsearch_rails" do | ||
|
||
before do | ||
@collection = ElasticsearchRails::Results.new(backend.params) | ||
end | ||
|
||
it 'paginates with defaults' do | ||
pagy, _items = backend.send(:pagy_elasticsearch_rails, @collection) | ||
pagy.must_be_instance_of Pagy | ||
pagy.count.must_equal 1000 | ||
pagy.items.must_equal 20 # Pagy default | ||
pagy.page.must_equal backend.params[:page] | ||
end | ||
|
||
it 'paginates with vars' do | ||
pagy, _items = backend.send(:pagy_elasticsearch_rails, @collection, link_extra: 'X') | ||
pagy.must_be_instance_of Pagy | ||
pagy.count.must_equal 1000 | ||
pagy.items.must_equal 20 # Pagy default | ||
pagy.vars[:link_extra].must_equal 'X' | ||
end | ||
|
||
end | ||
|
||
describe "#pagy_elasticsearch_rails_get_vars" do | ||
|
||
before do | ||
@collection = ElasticsearchRails::Results.new(backend.params) | ||
end | ||
|
||
it 'gets defaults' do | ||
vars = {} | ||
merged = backend.send :pagy_elasticsearch_rails_get_vars, @collection, vars | ||
merged.keys.must_include :count | ||
merged.keys.must_include :page | ||
merged[:count].must_equal 1000 | ||
merged[:items].must_be_nil | ||
merged[:page].must_equal 3 | ||
end | ||
|
||
it 'gets vars' do | ||
vars = { items: 25, link_extra: 'X' } | ||
merged = backend.send :pagy_elasticsearch_rails_get_vars, @collection, vars | ||
merged.keys.must_include :count | ||
merged.keys.must_include :page | ||
merged.keys.must_include :link_extra | ||
merged[:count].must_equal 1000 | ||
merged[:items].must_equal 25 | ||
merged[:link_extra].must_equal 'X' | ||
end | ||
|
||
end | ||
|
||
end |