Skip to content

Commit

Permalink
refactoring of pagy_search and its usage in order to allow optional t…
Browse files Browse the repository at this point in the history
…erm argument for searchkick (#169)
  • Loading branch information
ddnexus committed Jul 22, 2019
1 parent 389755b commit 0ac5c27
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
16 changes: 8 additions & 8 deletions lib/pagy/extras/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def self.new_from_elasticsearch_rails(response, vars={})
module Backend ; private

# Return Pagy object and items
def pagy_elasticsearch_rails(search_args, vars={})
model, query_or_payload, options, _block, *called = 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)
vars[:count] = response.raw_response['hits']['total']
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)
vars[:count] = response.raw_response['hits']['total']
pagy = Pagy.new(vars)
# with :last_page overflow we need to re-run the method in order to get the hits
if defined?(OVERFLOW) && pagy.overflow? && pagy.vars[:overflow] == :last_page
return pagy_elasticsearch_rails(search_args, vars.merge(page: pagy.page))
return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page))
end
return pagy, called.empty? ? response : response.send(*called)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/pagy/extras/pagy_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ module Search
# 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(arg, options={}, &block)
[self, arg, options, block].tap do |args|
# 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
Expand Down
16 changes: 8 additions & 8 deletions lib/pagy/extras/searchkick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def self.new_from_searchkick(results, vars={})
module Backend ; private

# Return Pagy object and results
def pagy_searchkick(search_args, vars={})
model, term, options, block, *called = 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
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
pagy = Pagy.new(vars)
# with :last_page overflow we need to re-run the method in order to get the hits
if defined?(OVERFLOW) && pagy.overflow? && pagy.vars[:overflow] == :last_page
return pagy_searchkick(search_args, vars.merge(page: pagy.page))
return pagy_searchkick(pagy_search_args, vars.merge(page: pagy.page))
end
return pagy, called.empty? ? results : results.send(*called)
end
Expand Down
8 changes: 4 additions & 4 deletions test/pagy/extras/elasticsearch_rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
end

it 'returns class and arguments' do
MockElasticsearchRails::Model.pagy_search('a', b:2).must_equal [MockElasticsearchRails::Model, 'a', {b: 2}, nil]
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]
args.must_equal [MockElasticsearchRails::Model, ['a', {b: 2}], block]
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}], nil, :records]
MockElasticsearchRails::Model.pagy_search('a', b:2).a('b', 2).must_equal [MockElasticsearchRails::Model, ['a', {b: 2}], nil, :a, 'b', 2]
end

end
Expand Down
22 changes: 18 additions & 4 deletions test/pagy/extras/searchkick_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +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]
args = MockSearchkick::Model.pagy_search(b:2){|a| a*2}
block = args[-1]
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]
args = MockSearchkick::Model.pagy_search('a'){|a| a*2}
block = args[-1]
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 0ac5c27

Please sign in to comment.