From 37cf51aed2f1b29bf1bcc2ce768a902860c948b8 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Sun, 18 Nov 2018 09:54:19 +0700 Subject: [PATCH] series can return an empty array if size is empty --- docs/api/pagy.md | 2 ++ docs/how-to.md | 12 ++++++++++++ lib/pagy.rb | 2 +- test/pagy_test.rb | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/api/pagy.md b/docs/api/pagy.md index 69c22a743..aab656abc 100644 --- a/docs/api/pagy.md +++ b/docs/api/pagy.md @@ -124,6 +124,8 @@ The nav helpers and the templates basically loop through this array and render t That is self-contained, simple and efficient. +**Notice**: This method returns an empty array if the passed `size` (i.e. the `:size` variable by default) is set to an empty array. Useful to totally skip the generation of page links in the frontend. + ### Lowest limit analysys The lowest possible limit of the pagination is reached when the collection has `0` count. In that case the Pagy object created has the following peculiar attributes: diff --git a/docs/how-to.md b/docs/how-to.md index aa1ab50f3..02936a4be 100644 --- a/docs/how-to.md +++ b/docs/how-to.md @@ -127,6 +127,18 @@ As you can see by the result of the `series` method, you get 3 initial pages, 1 You can easily try different options (also asymmetrical) in a console by changing the `:size`. Just check the `series` array to see what it contains when used in combination with different core variables. +### Skipping the page links + +If you want to skip the generation of the page links, just set the `:size` variable to an empty array: + +For example: + +```ruby +pagy = Pagy.new count:1000, size: [] # etc +pagy.series +#=> [] +``` + ## Passing the page number You don't need to explicitly pass the page number to the `pagy` method, because it is pulled in by the `pagy_get_vars` (which is called internally by the `pagy` method). However you can force a `page` number by just passing it to the `pagy` method. For example: diff --git a/lib/pagy.rb b/lib/pagy.rb index 55cab8b2b..a1809cded 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -34,8 +34,8 @@ def initialize(vars) # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36] def series(size=@vars[:size]) + (series = []) and size.empty? and return series 4.times{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")} - series = [] [*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b| if a<0 || a==b || a>@last # skip out of range and duplicates elsif a+1 == b; series.push(a) # no gap -> no additions diff --git a/test/pagy_test.rb b/test/pagy_test.rb index 73a209df2..0845910ac 100644 --- a/test/pagy_test.rb +++ b/test/pagy_test.rb @@ -417,6 +417,10 @@ def series_for(page, *expected) Pagy.new(@vars3.merge(count: 15, page: 2)).series.must_equal [1, "2"] end + it 'computes an empty series' do + Pagy.new(@vars3.merge(count: 100, size: [])).series.must_equal [] + end + end end