Sha256: f325a4cb95840db999c854658a9059de4f656579677a8ec2cddaaecb90ad4feb

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/trim
# frozen_string_literal: true

class Pagy

  module Frontend

    # boolean used by the compact extra
    TRIM = true

    def pagy_trim_url(url, param_string)
      url.sub(/((?:[?&])#{param_string}\z|\b(?<=[?&])#{param_string}&)/, '')
    end

    alias_method :pagy_link_proc_without_trim, :pagy_link_proc
    def pagy_link_proc_with_trim(pagy, link_extra='')
      p_prev, p_next, p_vars = pagy.prev, pagy.next, pagy.vars
      url   = pagy_url_for(MARKER, pagy)
      p1url = pagy_trim_url(url, "#{p_vars[:page_param]}=#{MARKER}")
      p1    = %(<a href="#{p1url}" #{p_vars[:link_extra]} #{link_extra})
      a, b = %(<a href="#{url}" #{p_vars[:link_extra]} #{link_extra}).split(MARKER, 2)
      -> (n, text=n, extra='') { start = n.to_i == 1 ? p1 : "#{a}#{n}#{b}"
                                 "#{start}#{ if    n == p_prev ; ' rel="prev"'
                                             elsif n == p_next ; ' rel="next"'
                                             else                           '' end } #{extra}>#{text}</a>" }
    end
    alias_method :pagy_link_proc, :pagy_link_proc_with_trim

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pagy-0.19.0 lib/pagy/extras/trim.rb
pagy-0.18.0 lib/pagy/extras/trim.rb
pagy-0.17.0 lib/pagy/extras/trim.rb
pagy-0.16.0 lib/pagy/extras/trim.rb
pagy-0.15.1 lib/pagy/extras/trim.rb