Skip to content

Commit

Permalink
series can return an empty array if size is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 18, 2018
1 parent 7bf9d14 commit 37cf51a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/api/pagy.md
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions docs/how-to.md
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/pagy_test.rb
Expand Up @@ -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

0 comments on commit 37cf51a

Please sign in to comment.