Module: Pagy::SearchkickPaginator

Defined in:
lib/pagy/toolbox/paginators/searchkick.rb

Class Method Summary collapse

Class Method Details

.paginate(search, options) ⇒ Object

Paginate from the search object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pagy/toolbox/paginators/searchkick.rb', line 10

def paginate(search, options)
  if search.is_a?(Search::Arguments)
    # The search is the array of pagy_search arguments
    Searcher.wrap(search, options) do
      model, term, search_options, block = search
      search_options[:per_page] = options[:limit]
      search_options[:page]     = options[:page]
      results                   = model.send(options[:search_method] || Searchkick::DEFAULT[:search_method],
                                             term || '*', **search_options, &block)
      options[:count]           = results.total_count
      [Searchkick.new(**options), results]
    end
  else
    # The search is a searchkick results object
    options[:limit] = search.respond_to?(:options) ? search.options[:per_page] : search.per_page
    options[:page]  = search.respond_to?(:options) ? search.options[:page]     : search.current_page
    options[:count] = search.total_count
    Searchkick.new(**options)
  end
end