Skip to content

Commit

Permalink
trim feature: added :trim variable to opt-out to trim even with the t…
Browse files Browse the repository at this point in the history
…rim extra loaded, or invert the logic to out-in by setting Pagy::VARS[:trim] to false
  • Loading branch information
ddnexus committed Apr 19, 2021
1 parent ece4daa commit 1051e6d
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 67 deletions.
2 changes: 1 addition & 1 deletion docs/extras/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pagy(scope, items: 30)

## Methods

The `items` extra overrides a couple of builtin methods and adds a helper to the `Pagy::Frontend` module. All the overridden methods are alias-chained with `*_with_items` and `*_without_items`)
The `items` extra overrides a couple of builtin methods and adds a helper to the `Pagy::Frontend` module.

### pagy_get_vars(collection, vars)

Expand Down
14 changes: 12 additions & 2 deletions docs/extras/trim.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Trim

This extra removes the `page=1` param from the link of the first page. You need only to require the extra in the initializer file.

This extra is needed only for very specific scenarios, for example if you need to avoid frontend cache duplicates.
This extra is needed only for very specific scenarios, for example if you need to avoid frontend cache duplicates of the first page.

## Synopsis

Expand All @@ -21,11 +21,21 @@ require 'pagy/extras/trim'

- [trim.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/trim.rb)

## Variables

| Variable | Description | Default |
|:---------|:-----------------|:--------|
| `:trim` | is trim enabled? | `true` |

You can use the `:trim` variable to opt-out of trimming even when the extra is required (trimming by default).

You can set the `Pagy::VARS[:trim]` default to `false` if you want to explicitly pass the `trim: true` variable in order to trim the param.

## Methods

The `trim` extra overrides the `pagy_link_proc` method in the `Pagy::Frontend` module.

### pagy_link_proc(pagy, link_extra='')

This method trims the `:page_param` param from the first page link. It is alias-chained with `*_with_trim` and `*_without_trim`.
This method trims the `:page_param` param from the first page link.

4 changes: 3 additions & 1 deletion lib/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
# Trim extra: Remove the page=1 param from links
# See https://ddnexus.github.io/pagy/extras/trim
# require 'pagy/extras/trim'

# after requiring it will trim by default
# set to false if you want to make trim an opt-in variable
# Pagy::VARS[:trim] = true # default


# Pagy Variables
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ module Frontend
if defined?(Oj)
# it returns a script tag with the JSON-serialized args generated with the faster oj gem
def pagy_json_tag(pagy, *args)
args << ( defined?(UseTrimExtra) && pagy.vars[:page_param] )
args << pagy.vars[:page_param] if pagy.vars[:trim]
%(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
end
else
require 'json'
# it returns a script tag with the JSON-serialized args generated with the slower to_json
def pagy_json_tag(pagy, *args)
args << ( defined?(UseTrimExtra) && pagy.vars[:page_param] )
args << pagy.vars[:page_param] if pagy.vars[:trim]
%(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/pagy/extras/trim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

class Pagy

VARS[:trim] = true

module UseTrimExtra

def pagy_link_proc(pagy, link_extra='')
link_proc = super(pagy, link_extra)
return link_proc unless pagy.vars[:trim]
lambda do |num, text=num, extra=''|
link = link_proc.call(num, text, extra)
if num == 1
link.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
else
link
end
return link unless num == 1
link.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
end
end

Expand Down
14 changes: 7 additions & 7 deletions test/pagy/extras/bootstrap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@
it 'renders first page' do
pagy = Pagy.new(count: 1000, page: 1)
_(view.pagy_bootstrap_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=2\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[\"1\",2,3,4,5,\"gap\",50]},null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=2\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[\"1\",2,3,4,5,\"gap\",50]}]</script>"
end

it 'renders intermediate page' do
pagy = Pagy.new(count: 1000, page: 20)
_(view.pagy_bootstrap_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=19\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=21\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",16,17,18,19,\"20\",21,22,23,24,\"gap\",50]},null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=19\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=21\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",16,17,18,19,\"20\",21,22,23,24,\"gap\",50]}]</script>"
end

it 'renders last page' do
pagy = Pagy.new(count: 1000, page: 50)
_(view.pagy_bootstrap_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=49\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",46,47,48,49,\"50\"]},null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=49\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",46,47,48,49,\"50\"]}]</script>"
end

it 'renders with :steps' do
pagy = Pagy.new(count: 1000, page: 20, steps: {0 => [1,2,2,1], 500 => [2,3,3,2]})
_(view.pagy_bootstrap_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=19\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=21\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",18,19,\"20\",21,22,\"gap\",50],\"500\":[1,2,\"gap\",17,18,19,\"20\",21,22,23,\"gap\",49,50]},null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-nav-js\" role=\"navigation\" aria-label=\"pager\"></nav><script type=\"application/json\" class=\"pagy-json\">[\"nav\",\"test-id\",{\"before\":\"<ul class=\\\"pagination\\\"><li class=\\\"page-item prev\\\"><a href=\\\"/foo?page=19\\\" class=\\\"page-link\\\" rel=\\\"prev\\\" aria-label=\\\"previous\\\">&lsaquo;&nbsp;Prev</a></li>\",\"link\":\"<li class=\\\"page-item\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"active\":\"<li class=\\\"page-item active\\\"><a href=\\\"/foo?page=__pagy_page__\\\" class=\\\"page-link\\\" >__pagy_page__</a></li>\",\"gap\":\"<li class=\\\"page-item gap disabled\\\"><a href=\\\"#\\\" class=\\\"page-link\\\">&hellip;</a></li>\",\"after\":\"<li class=\\\"page-item next\\\"><a href=\\\"/foo?page=21\\\" class=\\\"page-link\\\" rel=\\\"next\\\" aria-label=\\\"next\\\">Next&nbsp;&rsaquo;</a></li></ul>\"},{\"0\":[1,\"gap\",18,19,\"20\",21,22,\"gap\",50],\"500\":[1,2,\"gap\",17,18,19,\"20\",21,22,23,\"gap\",49,50]}]</script>"
end

end
Expand All @@ -63,19 +63,19 @@
it 'renders first page' do
pagy = Pagy.new(count: 103, page: 1)
_(view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a class=\"prev btn btn-primary disabled\" href=\"#\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"1\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a href=\"/foo?page=2\" rel=\"next\" aria-label=\"next\" class=\"next btn btn-primary\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",1,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\",null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a class=\"prev btn btn-primary disabled\" href=\"#\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"1\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a href=\"/foo?page=2\" rel=\"next\" aria-label=\"next\" class=\"next btn btn-primary\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",1,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\"]</script>"
end

it 'renders intermediate page' do
pagy = Pagy.new(count: 103, page: 3)
_(view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a href=\"/foo?page=2\" rel=\"prev\" aria-label=\"previous\" class=\"prev btn btn-primary\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"3\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a href=\"/foo?page=4\" rel=\"next\" aria-label=\"next\" class=\"next btn btn-primary\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",3,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\",null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a href=\"/foo?page=2\" rel=\"prev\" aria-label=\"previous\" class=\"prev btn btn-primary\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"3\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a href=\"/foo?page=4\" rel=\"next\" aria-label=\"next\" class=\"next btn btn-primary\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",3,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\"]</script>"
end

it 'renders last page' do
pagy = Pagy.new(count: 103, page: 6)
_(view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id)).must_equal \
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a href=\"/foo?page=5\" rel=\"prev\" aria-label=\"previous\" class=\"prev btn btn-primary\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"6\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a class=\"next btn btn-primary disabled\" href=\"#\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",6,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\",null]</script>"
"<nav id=\"test-id\" class=\"pagy-bootstrap-combo-nav-js pagination\" role=\"navigation\" aria-label=\"pager\"><div class=\"btn-group\" role=\"group\"><a href=\"/foo?page=5\" rel=\"prev\" aria-label=\"previous\" class=\"prev btn btn-primary\">&lsaquo;&nbsp;Prev</a><div class=\"pagy-combo-input btn btn-primary disabled\" style=\"white-space: nowrap;\">Page <input type=\"number\" min=\"1\" max=\"6\" value=\"6\" class=\"text-primary\" style=\"padding: 0; border: none; text-align: center; width: 2rem;\"> of 6</div><a class=\"next btn btn-primary disabled\" href=\"#\">Next&nbsp;&rsaquo;</a></div></nav><script type=\"application/json\" class=\"pagy-json\">[\"combo_nav\",\"test-id\",6,\"<a href=\\\"/foo?page=__pagy_page__\\\" style=\\\"display: none;\\\"></a>\"]</script>"
end

end
Expand Down
Loading

0 comments on commit 1051e6d

Please sign in to comment.