Module: Pagy::StandaloneExtra

Included in:
Backend, Frontend
Defined in:
lib/pagy/extras/standalone.rb

Overview

Use pagy without any request object, nor Rack environment/gem, nor any defined params method, even in the irb/rails console without any app or config.

Defined Under Namespace

Modules: QueryUtils

Instance Method Summary collapse

Instance Method Details

#pagy_url_for(pagy, page, absolute: nil) ⇒ Object

Return the URL for the page. If there is no pagy.vars it works exactly as the regular #pagy_url_for, relying on the params method and Rack. If there is a defined pagy.vars variable it does not need the params method nor Rack.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pagy/extras/standalone.rb', line 39

def pagy_url_for(pagy, page, absolute: nil)
  return super unless pagy.vars[:url]

  vars                = pagy.vars
  page_param          = vars[:page_param].to_s
  items_param         = vars[:items_param].to_s
  params              = pagy.params.is_a?(Hash) ? pagy.params.clone : {}  # safe when it gets reused
  params[page_param]  = page
  params[items_param] = vars[:items] if vars[:items_extra]
  query_string        = "?#{QueryUtils.build_nested_query(pagy_deprecated_params(pagy, params))}"  # remove in 6.0
  # params              = pagy.params.call(params) if pagy.params.is_a?(Proc)                      # add in 6.0
  # query_string        = "?#{Rack::Utils.build_nested_query(params)}"                             # add in 6.0
  "#{vars[:url]}#{query_string}#{vars[:fragment]}"
end