Sha256: 5e71c225da68c96a5d31505053e9575b1a178b42ef9f84d67c85644bc897637d

Contents?: true

Size: 952 Bytes

Versions: 12

Compression:

Stored size: 952 Bytes

Contents

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

class Pagy # :nodoc:
  DEFAULT[:trim_extra] = true   # extra enabled by default

  # Remove the page=1 param from the first page link
  module TrimExtra
    # Override the original pagy_link_proc.
    # Call the pagy_trim method if the trim_extra is enabled.
    def pagy_link_proc(pagy, link_extra: '')
      link_proc = super(pagy, link_extra:)
      return link_proc unless pagy.vars[:trim_extra]

      lambda do |page, text = pagy.label_for(page), extra = ''|
        link = +link_proc.call(page, text, extra)
        return link unless page.to_s == '1'

        pagy_trim(pagy, link)
      end
    end

    # Remove the the :page_param param from the first page link
    def pagy_trim(pagy, link)
      link.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
    end
  end
  Frontend.prepend TrimExtra
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pagy-7.0.11 lib/pagy/extras/trim.rb
pagy-7.0.10 lib/pagy/extras/trim.rb
pagy-7.0.9 lib/pagy/extras/trim.rb
pagy-7.0.8 lib/pagy/extras/trim.rb
pagy-7.0.7 lib/pagy/extras/trim.rb
pagy-7.0.6 lib/pagy/extras/trim.rb
pagy-7.0.5 lib/pagy/extras/trim.rb
pagy-7.0.4 lib/pagy/extras/trim.rb
pagy-7.0.3 lib/pagy/extras/trim.rb
pagy-7.0.2 lib/pagy/extras/trim.rb
pagy-7.0.1 lib/pagy/extras/trim.rb
pagy-7.0.0 lib/pagy/extras/trim.rb