Skip to content

Commit

Permalink
refactoring of the elasticsearch_rails extra matching the actual para…
Browse files Browse the repository at this point in the history
…ms of the search method
  • Loading branch information
ddnexus committed Mar 9, 2021
1 parent 6966fe3 commit 98a5254
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
20 changes: 9 additions & 11 deletions lib/pagy/extras/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ 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|
def pagy_search(query_or_payload, **options)
[self, query_or_payload, options].tap do |args|
args.define_singleton_method(:method_missing){|*a| args += a}
end
end
Expand All @@ -31,13 +29,13 @@ module Backend ; private

# Return Pagy object and items
def pagy_elasticsearch_rails(pagy_search_args, vars={})
model, search_args, _block, *called = pagy_search_args
vars = pagy_elasticsearch_rails_get_vars(nil, vars)
search_args[-1][:size] = vars[:items]
search_args[-1][:from] = vars[:items] * (vars[:page] - 1)
response = model.search(*search_args)
total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
vars[:count] = total.is_a?(Hash) ? total['value'] : total
model, query_or_payload, options, *called = pagy_search_args
vars = pagy_elasticsearch_rails_get_vars(nil, vars)
options[:size] = vars[:items]
options[:from] = vars[:items] * (vars[:page] - 1)
response = model.search(query_or_payload, **options)
total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
vars[:count] = total.is_a?(Hash) ? total['value'] : total
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
11 changes: 5 additions & 6 deletions test/pagy/extras/elasticsearch_rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
end

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

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

end
Expand Down

0 comments on commit 98a5254

Please sign in to comment.