Skip to content

Commit

Permalink
improve readability of pagy_link_proc (#40):
Browse files Browse the repository at this point in the history
- remove conditions to avoid extra spaces inside the html tags
- add Pagy::VARS[:link_extra] empty default

Co-authored-by: Michael Grosser <[email protected]>
  • Loading branch information
ddnexus and grosser committed Jun 2, 2018
1 parent f95bc45 commit 8789edd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/api/pagy.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ These are the non-core variables: as for the core-variables they can be set glob
| `:page_param` | the name of the page param name used in the url. (see [Customizing the page param](../how-to.md#customizing-the-page-param) | `:page` |
| `:params` | the arbitrary param hash to add to the url. (see [Customizing the params](../how-to.md#customizing-the-params) | `{}` |
| `:anchor` | the arbitrary anchor string (including the "#") to add to the url. (see [Customizing the page param](../how-to.md#customizing-the-params) | `nil` |
| `:link_extra` | the extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links | `nil` |
| `:link_extra` | the extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links | `""` |
| `:item_path` | the dictionary path used by the `pagy_info` method to lookup the item/model name | `nil` |

There is no specific default nor validation for non-core variables, which are just optional strings.
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OutOfRangeError < StandardError; end
def self.root; Pathname.new(__FILE__).dirname end

# default core vars
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {} }
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {}, link_extra: ''.freeze }

attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next

Expand Down
15 changes: 7 additions & 8 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ def pagy_get_params(params) params end

MARKER = "-pagy-#{'pagy'.hash}-".freeze

# returns a specialized proc to generate the HTML links
def pagy_link_proc(pagy, lx=''.freeze) # "lx" means "link extra"
p_prev, p_next, p_lx = pagy.prev, pagy.next, pagy.vars[:link_extra]
a, b = %(<a href="#{pagy_url_for(MARKER, pagy)}"#{p_lx ? %( #{p_lx}) : ''.freeze}#{lx.empty? ? lx : %( #{lx})}).split(MARKER)
-> (n, text=n, x=''.freeze) { "#{a}#{n}#{b}#{ if n == p_prev ; ' rel="prev"'.freeze
elsif n == p_next ; ' rel="next"'.freeze
else ''.freeze end }#{x.empty? ? x : %( #{x})}>#{text}</a>" }
# returns a performance optimized proc to generate the HTML links
def pagy_link_proc(pagy, link_extra=''.freeze)
p_prev, p_next = pagy.prev, pagy.next
a, b = %(<a href="#{pagy_url_for(MARKER, pagy)}" #{pagy.vars[:link_extra]} #{link_extra}).split(MARKER, 2)
-> (n, text=n, extra=''.freeze) { "#{a}#{n}#{b}#{ if n == p_prev ; ' rel="prev"'.freeze
elsif n == p_next ; ' rel="next"'.freeze
else ''.freeze end } #{extra}>#{text}</a>" }
end


# Pagy::Frontend::I18N constant
zero_one = [:zero, :one]; I18N = { plurals: -> (c) {(zero_one[c] || :other).to_s.freeze}, data: {}}
def I18N.load_file(file) I18N[:data].replace(YAML.load_file(file).first[1]) end
Expand Down
67 changes: 34 additions & 33 deletions test/pagy/frontend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def test_pagy_nav_page_1
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev disabled">&lsaquo;&nbsp;Prev</span> ' \
'<span class="page active">1</span> ' \
'<span class="page"><a href="/foo?page=2" rel="next">2</a></span> ' \
'<span class="page"><a href="/foo?page=3">3</a></span> ' \
'<span class="page"><a href="/foo?page=4">4</a></span> ' \
'<span class="page"><a href="/foo?page=5">5</a></span> ' \
'<span class="page"><a href="/foo?page=6">6</a></span> ' \
'<span class="page next"><a href="/foo?page=2" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'<span class="page"><a href="/foo?page=2" rel="next" >2</a></span> ' \
'<span class="page"><a href="/foo?page=3" >3</a></span> ' \
'<span class="page"><a href="/foo?page=4" >4</a></span> ' \
'<span class="page"><a href="/foo?page=5" >5</a></span> ' \
'<span class="page"><a href="/foo?page=6" >6</a></span> ' \
'<span class="page next"><a href="/foo?page=2" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'</nav>',
frontend.pagy_nav(pagy)
)
Expand All @@ -49,30 +49,31 @@ def test_pagy_nav_page_3

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev"><a href="/foo?page=2" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1">1</a></span> ' \
'<span class="page"><a href="/foo?page=2" rel="prev">2</a></span> ' \
'<span class="page prev"><a href="/foo?page=2" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1" >1</a></span> ' \
'<span class="page"><a href="/foo?page=2" rel="prev" >2</a></span> ' \
'<span class="page active">3</span> ' \
'<span class="page"><a href="/foo?page=4" rel="next">4</a></span> ' \
'<span class="page"><a href="/foo?page=5">5</a></span> ' \
'<span class="page"><a href="/foo?page=6">6</a></span> ' \
'<span class="page next"><a href="/foo?page=4" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'<span class="page"><a href="/foo?page=4" rel="next" >4</a></span> ' \
'<span class="page"><a href="/foo?page=5" >5</a></span> ' \
'<span class="page"><a href="/foo?page=6" >6</a></span> ' \
'<span class="page next"><a href="/foo?page=4" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'</nav>',
frontend.pagy_nav(pagy)
)
end


def test_pagy_nav_page_6
pagy, _ = @array.pagy(6)

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev"><a href="/foo?page=5" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1">1</a></span> ' \
'<span class="page"><a href="/foo?page=2">2</a></span> ' \
'<span class="page"><a href="/foo?page=3">3</a></span> ' \
'<span class="page"><a href="/foo?page=4">4</a></span> ' \
'<span class="page"><a href="/foo?page=5" rel="prev">5</a></span> ' \
'<span class="page prev"><a href="/foo?page=5" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1" >1</a></span> ' \
'<span class="page"><a href="/foo?page=2" >2</a></span> ' \
'<span class="page"><a href="/foo?page=3" >3</a></span> ' \
'<span class="page"><a href="/foo?page=4" >4</a></span> ' \
'<span class="page"><a href="/foo?page=5" rel="prev" >5</a></span> ' \
'<span class="page active">6</span> ' \
'<span class="page next disabled">Next&nbsp;&rsaquo;</span>' \
'</nav>',
Expand All @@ -86,36 +87,36 @@ def test_pagy_nav_page_10

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev"><a href="/foo?page=9" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1">1</a></span> ' \
'<span class="page prev"><a href="/foo?page=9" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1" >1</a></span> ' \
'<span class="page gap">&hellip;</span> ' \
'<span class="page"><a href="/foo?page=6">6</a></span> ' \
'<span class="page"><a href="/foo?page=7">7</a></span> ' \
'<span class="page"><a href="/foo?page=8">8</a></span> ' \
'<span class="page"><a href="/foo?page=9" rel="prev">9</a></span> ' \
'<span class="page"><a href="/foo?page=6" >6</a></span> ' \
'<span class="page"><a href="/foo?page=7" >7</a></span> ' \
'<span class="page"><a href="/foo?page=8" >8</a></span> ' \
'<span class="page"><a href="/foo?page=9" rel="prev" >9</a></span> ' \
'<span class="page active">10</span> ' \
'<span class="page"><a href="/foo?page=11" rel="next">11</a></span> ' \
'<span class="page"><a href="/foo?page=12">12</a></span> ' \
'<span class="page"><a href="/foo?page=13">13</a></span> ' \
'<span class="page"><a href="/foo?page=14">14</a></span> ' \
'<span class="page"><a href="/foo?page=11" rel="next" >11</a></span> ' \
'<span class="page"><a href="/foo?page=12" >12</a></span> ' \
'<span class="page"><a href="/foo?page=13" >13</a></span> ' \
'<span class="page"><a href="/foo?page=14" >14</a></span> ' \
'<span class="page gap">&hellip;</span> ' \
'<span class="page"><a href="/foo?page=50">50</a></span> ' \
'<span class="page next"><a href="/foo?page=11" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span></nav>',
'<span class="page"><a href="/foo?page=50" >50</a></span> ' \
'<span class="page next"><a href="/foo?page=11" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span></nav>',
frontend.pagy_nav(pagy)
)
end

def test_link_extras
pagy, _ = @array.pagy(1, link_extra: "X")
frontend.pagy_nav(pagy).must_include '?page=2" X rel'
frontend.pagy_nav(pagy).must_include '?page=2" X rel'
end
end

describe "#pagy_link_proc" do
it "renders with extras" do
@array = (1..103).to_a.extend(Pagy::Array::PageMethod)
pagy, _ = @array.pagy(1)
frontend.pagy_link_proc(pagy, "X").call(1).must_equal '<a href="/foo?page=1" X>1</a>'
frontend.pagy_link_proc(pagy, "X").call(1).must_equal '<a href="/foo?page=1" X >1</a>'
end
end

Expand Down

0 comments on commit 8789edd

Please sign in to comment.