diff --git a/docs/api/pagy.md b/docs/api/pagy.md index 07ea5854b..c6badfd8a 100644 --- a/docs/api/pagy.md +++ b/docs/api/pagy.md @@ -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. diff --git a/lib/pagy.rb b/lib/pagy.rb index a11e659ce..b18c6a543 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -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 diff --git a/lib/pagy/frontend.rb b/lib/pagy/frontend.rb index 40f5d16bf..21e674c5d 100644 --- a/lib/pagy/frontend.rb +++ b/lib/pagy/frontend.rb @@ -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 = %( (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}" } + # 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 = %( (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}" } 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 diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb index 34f719e79..08db660fe 100644 --- a/test/pagy/frontend_test.rb +++ b/test/pagy/frontend_test.rb @@ -33,12 +33,12 @@ def test_pagy_nav_page_1 '', frontend.pagy_nav(pagy) ) @@ -49,30 +49,31 @@ def test_pagy_nav_page_3 assert_equal( '', frontend.pagy_nav(pagy) ) end + def test_pagy_nav_page_6 pagy, _ = @array.pagy(6) assert_equal( '', @@ -86,28 +87,28 @@ def test_pagy_nav_page_10 assert_equal( '', + '50 ' \ + '', 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 @@ -115,7 +116,7 @@ def test_link_extras 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 '1' + frontend.pagy_link_proc(pagy, "X").call(1).must_equal '1' end end