Skip to content

Commit

Permalink
small refactoring of the trim extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Sep 19, 2018
1 parent df8b8d4 commit a62b222
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
9 changes: 2 additions & 7 deletions docs/extras/trim.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

21 changes: 12 additions & 9 deletions lib/pagy/extras/trim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %(<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}"
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
8 changes: 4 additions & 4 deletions test/pagy/extras/trim_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a62b222

Please sign in to comment.