Sha256: f43018b513850fb6b418852916bf7c37cc9dce9ef2118124ccc1b9c98933564f

Contents?: true

Size: 930 Bytes

Versions: 18

Compression:

Stored size: 930 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_anchor.
    # Call the pagy_trim method for page 1 if the trim_extra is enabled
    def pagy_anchor(pagy, **_)
      a_proc = super
      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

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pagy-9.3.2 lib/pagy/extras/trim.rb
pagy-9.3.1 lib/pagy/extras/trim.rb
pagy-9.3.0 lib/pagy/extras/trim.rb
pagy-9.2.2 lib/pagy/extras/trim.rb
pagy-9.2.1 lib/pagy/extras/trim.rb
pagy-9.2.0 lib/pagy/extras/trim.rb
pagy-9.1.1 lib/pagy/extras/trim.rb
pagy-9.1.0 lib/pagy/extras/trim.rb
pagy-9.0.9 lib/pagy/extras/trim.rb
pagy-9.0.8 lib/pagy/extras/trim.rb
pagy-9.0.7 lib/pagy/extras/trim.rb
pagy-9.0.6 lib/pagy/extras/trim.rb
pagy-9.0.5 lib/pagy/extras/trim.rb
pagy-9.0.4 lib/pagy/extras/trim.rb
pagy-9.0.3 lib/pagy/extras/trim.rb
pagy-9.0.2 lib/pagy/extras/trim.rb
pagy-9.0.1 lib/pagy/extras/trim.rb
pagy-9.0.0 lib/pagy/extras/trim.rb