From 98a5254157b1d606c6ab5238881ac2aaf9c1833d Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Tue, 9 Mar 2021 13:50:36 +0700 Subject: [PATCH] refactoring of the elasticsearch_rails extra matching the actual params of the search method --- lib/pagy/extras/elasticsearch_rails.rb | 20 +++++++++----------- test/pagy/extras/elasticsearch_rails_test.rb | 11 +++++------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/pagy/extras/elasticsearch_rails.rb b/lib/pagy/extras/elasticsearch_rails.rb index 431462b76..cc0d23ba1 100644 --- a/lib/pagy/extras/elasticsearch_rails.rb +++ b/lib/pagy/extras/elasticsearch_rails.rb @@ -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 @@ -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 diff --git a/test/pagy/extras/elasticsearch_rails_test.rb b/test/pagy/extras/elasticsearch_rails_test.rb index 02b30d195..17430de07 100644 --- a/test/pagy/extras/elasticsearch_rails_test.rb +++ b/test/pagy/extras/elasticsearch_rails_test.rb @@ -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