Sha256: d8eb26992af3837fe841b5c5ac68e2d96ebf5978359235cd5d77eb36c16d5dae

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 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 navs
    TRIM = true

    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
      marker_url = pagy_url_for(MARKER, pagy)
      page1_url  = pagy_trim_url(marker_url, "#{p_vars[:page_param]}=#{MARKER}")
      page1_link = %(<a href="#{page1_url}" #{p_vars[:link_extra]} #{link_extra})
      a, b = %(<a href="#{marker_url}" #{p_vars[:link_extra]} #{link_extra}).split(MARKER, 2)
      -> (n, text=n, extra='') { start = n.to_i == 1 ? page1_link : "#{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

    private

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

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pagy-1.3.3 lib/pagy/extras/trim.rb
pagy-1.3.2 lib/pagy/extras/trim.rb
pagy-1.3.1 lib/pagy/extras/trim.rb
pagy-1.3 lib/pagy/extras/trim.rb
pagy-1.2.1 lib/pagy/extras/trim.rb
pagy-1.2.0 lib/pagy/extras/trim.rb
pagy-1.1.0 lib/pagy/extras/trim.rb
pagy-1.0.0 lib/pagy/extras/trim.rb
pagy-0.23.1 lib/pagy/extras/trim.rb
pagy-0.23.0 lib/pagy/extras/trim.rb
pagy-0.22.0 lib/pagy/extras/trim.rb
pagy-0.21.0 lib/pagy/extras/trim.rb
pagy-0.20.0 lib/pagy/extras/trim.rb
pagy-0.19.4 lib/pagy/extras/trim.rb
pagy-0.19.3 lib/pagy/extras/trim.rb
pagy-0.19.2 lib/pagy/extras/trim.rb