Sha256: c6dd2f3508730ab5941d386a52f94a84447469c3d0a8e351e492833dd48c0f9c

Contents?: true

Size: 1.72 KB

Versions: 20

Compression:

Stored size: 1.72 KB

Contents

# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/searchkick
# encoding: utf-8
# frozen_string_literal: true

require 'pagy/extras/pagy_search'

class Pagy

  # used by the items extra
  SEARCHKICK = true

  # create a Pagy object from a Searchkick::Results object
  def self.new_from_searchkick(results, vars={})
    vars[:items] = results.options[:per_page]
    vars[:page]  = results.options[:page]
    vars[:count] = results.total_count
    new(vars)
  end

  # Add specialized backend methods to paginate Searchkick::Results
  module Backend ; private

    # Return Pagy object and results
    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(pagy_search_args, vars.merge(page: pagy.page))
      end
      return pagy, called.empty? ? results : results.send(*called)
    end

    # Sub-method called only by #pagy_searchkick: here for easy customization of variables by overriding
    # the _collection argument is not available when the method is called
    def pagy_searchkick_get_vars(_collection, vars)
      vars[:items] ||= VARS[:items]
      vars[:page]  ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
      vars
    end

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
pagy-3.14.0 lib/pagy/extras/searchkick.rb
pagy-3.13.0 lib/pagy/extras/searchkick.rb
pagy-3.12.0 lib/pagy/extras/searchkick.rb
pagy-3.11.0 lib/pagy/extras/searchkick.rb
pagy-3.10.0 lib/pagy/extras/searchkick.rb
pagy-3.9.0 lib/pagy/extras/searchkick.rb
pagy-3.8.3 lib/pagy/extras/searchkick.rb
pagy-3.8.2 lib/pagy/extras/searchkick.rb
pagy-3.8.1 lib/pagy/extras/searchkick.rb
pagy-3.8.0 lib/pagy/extras/searchkick.rb
pagy-3.7.5 lib/pagy/extras/searchkick.rb
pagy-3.7.4 lib/pagy/extras/searchkick.rb
pagy-3.7.3 lib/pagy/extras/searchkick.rb
pagy-3.7.2 lib/pagy/extras/searchkick.rb
pagy-3.7.1 lib/pagy/extras/searchkick.rb
pagy-3.7.0 lib/pagy/extras/searchkick.rb
pagy-3.6.0 lib/pagy/extras/searchkick.rb
pagy-3.5.1 lib/pagy/extras/searchkick.rb
pagy-3.5.0 lib/pagy/extras/searchkick.rb
pagy-3.4.1 lib/pagy/extras/searchkick.rb