Module: Pagy::TrimExtra

Included in:
Frontend
Defined in:
lib/pagy/extras/trim.rb

Overview

Remove the page=1 param from the first page link

Instance Method Summary collapse

Instance Method Details

#pagy_anchor(pagy) ⇒ Object

Override the original pagy_a_proc. Call the pagy_trim method for page 1 if the trim_extra is enabled



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pagy/extras/trim.rb', line 11

def pagy_anchor(pagy)
  a_proc = super(pagy)
  return a_proc unless pagy.vars[:trim_extra]

  lambda do |page, text = pagy.label_for(page), **opts|
    a = +a_proc.(page, text, **opts)
    return a unless page.to_s == '1'

    pagy_trim(pagy, a) # in method for isolated testing
  end
end

#pagy_trim(pagy, a) ⇒ Object

Remove the the :page_param param from the first page anchor



24
25
26
# File 'lib/pagy/extras/trim.rb', line 24

def pagy_trim(pagy, a)
  a.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
end