diff --git a/docs/extras/trim.md b/docs/extras/trim.md index 5789a0e97..fe9c6a806 100644 --- a/docs/extras/trim.md +++ b/docs/extras/trim.md @@ -24,14 +24,9 @@ This extra is composed of 1 small file: ## Methods -The `trim` extra overrides one method and adds a utility helper to the `Pagy::Frontend` module. The overridden method is alias-chained with `*_with_trim` and `*_without_trim`) +The `trim` extra overrides the `pagy_link_proc` method in the `Pagy::Frontend` module. ### pagy_link_proc(pagy, link_extra='') -This extra overrides the `pagy_link_proc` method of the `Pagy::Frontend` module in order to trim the `:page_param` param from the first page link. - -### pagy_trim_url(url, param_string) - -This is the utility helper used internally in order to remove the `param_string` from the `url`. The `param_string` must be the complete string of name and value: e.g. `"page=1"`. - +This method trims the `:page_param` param from the first page link. It is alias-chained with `*_with_trim` and `*_without_trim`. diff --git a/lib/pagy/extras/trim.rb b/lib/pagy/extras/trim.rb index 0e321ed73..874b25502 100644 --- a/lib/pagy/extras/trim.rb +++ b/lib/pagy/extras/trim.rb @@ -8,23 +8,26 @@ module Frontend # boolean used by the compact navs 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 = %( (n, text=n, extra='') { start = n.to_i == 1 ? p1 : "#{a}#{n}#{b}" + marker_url = pagy_url_for(MARKER, pagy) + page1_url = pagy_trim_url(marker_url, "#{p_vars[:page_param]}=#{MARKER}") + page1_link = %( (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}" } 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 diff --git a/test/pagy/extras/trim_test.rb b/test/pagy/extras/trim_test.rb index adb5b4b01..b8a3af4f2 100644 --- a/test/pagy/extras/trim_test.rb +++ b/test/pagy/extras/trim_test.rb @@ -16,10 +16,10 @@ describe "#pagy_trim_url" do it 'trims urls' do - frontend.pagy_trim_url('foo/bar?page=1', 'page=1').must_equal('foo/bar') - frontend.pagy_trim_url('foo/bar?a=page&page=1', 'page=1').must_equal('foo/bar?a=page') - frontend.pagy_trim_url('foo/bar?a=page&page=1&b=4', 'page=1').must_equal('foo/bar?a=page&b=4') - frontend.pagy_trim_url('foo/bar?a=page&page=1&b=4&my_page=1', 'page=1').must_equal('foo/bar?a=page&b=4&my_page=1') + frontend.send(:pagy_trim_url, 'foo/bar?page=1', 'page=1').must_equal('foo/bar') + frontend.send(:pagy_trim_url, 'foo/bar?a=page&page=1', 'page=1').must_equal('foo/bar?a=page') + frontend.send(:pagy_trim_url, 'foo/bar?a=page&page=1&b=4', 'page=1').must_equal('foo/bar?a=page&b=4') + frontend.send(:pagy_trim_url, 'foo/bar?a=page&page=1&b=4&my_page=1', 'page=1').must_equal('foo/bar?a=page&b=4&my_page=1') end end