Skip to content

Commit

Permalink
items are adjusted only for non-empty pages (avoid sequel error for l…
Browse files Browse the repository at this point in the history
…imit 0)
  • Loading branch information
ddnexus committed Aug 3, 2018
1 parent 3abcc11 commit 4e400c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
4 changes: 1 addition & 3 deletions docs/api/pagy.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Pagy exposes all the instance variables needed for the pagination through a few
| -------- | ------------------------------------------------------------------------------------------------------------------ |
| `count` | the collection `:count` |
| `page` | the current page number |
| `items` | the _actual_ number of items in the current page (can be less than the requested `:items` variable) |
| `items` | the _actual_ number of items in the current non-empty page (can be less than the requested `:items` variable) |
| `pages` | the number of total pages in the collection (same as `last` but with cardinal meaning) |
| `last` | the number of the last page in the collection (same as `pages` but with ordinal meaning) |
| `offset` | the number of items skipped from the collection in order to get the start of the current page (`:outset` included) |
Expand Down Expand Up @@ -132,7 +132,6 @@ The lowest possible limit of the pagination is reached when the collection has `
| --------- | ------- |
| `count` | `0` |
| `page` | `1` |
| `items` | `0` |
| `pages` | `1` |
| `last` | `1` |
| `from` | `0` |
Expand All @@ -146,6 +145,5 @@ Which means:
- there is always a `page` #`1` in the pagination, even if it's empty
- `pages` and `last` are always at least both `1`
- the `series` array contains always at least the page #`1`, which for a single page is also the current page, thus a string
- the actual number of `items` in an empty page are `0`
- `from` and `to` of an empty page are both `0`
- `prev` and `next` of a single page (not necessary an empty one) are both `nil` (i.e. there is no other page)
18 changes: 9 additions & 9 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ def self.root; Pathname.new(__FILE__).dirname end

# Merge and validate the options, do some simple aritmetic and set the instance variables
def initialize(vars)
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars
{ count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
@vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars
{ count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
(@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
or raise(ArgumentError, "expected :#{k} >= #{min}; got #{instance_variable_get(:"@#{k}").inspect}")
end
@pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings
@pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings
@page <= @last or raise(OutOfRangeError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}")
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
@items = @count - ((@pages-1) * @items) if @page == @last # adjust items for last page
@from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
@to = @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = (@page+1 unless @page == @last) # nil if no next page
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
@items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page
@from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
@to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = (@page+1 unless @page == @last) # nil if no next page
end

# Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
Expand Down
32 changes: 16 additions & 16 deletions test/pagy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
e.pagy.must_be_instance_of Pagy
end

it 'initiazes count 0' do
it 'initializes count 0' do
pagy = Pagy.new @vars.merge(count: 0)
pagy.pages.must_equal 1
pagy.last.must_equal 1
Expand All @@ -44,7 +44,7 @@
pagy.next.must_be_nil
end

it 'initiazes single page' do
it 'initializes single page' do
pagy = Pagy.new @vars.merge(count: 8)
pagy.pages.must_equal 1
pagy.last.must_equal 1
Expand All @@ -55,7 +55,7 @@
pagy.next.must_be_nil
end

it 'initiazes page 1 of 2' do
it 'initializes page 1 of 2' do
pagy = Pagy.new @vars.merge(count: 15)
pagy.pages.must_equal 2
pagy.last.must_equal 2
Expand All @@ -66,7 +66,7 @@
pagy.next.must_equal 2
end

it 'initiazes page 2 of 2' do
it 'initializes page 2 of 2' do
pagy = Pagy.new @vars.merge(count: 15, page: 2)
pagy.pages.must_equal 2
pagy.last.must_equal 2
Expand All @@ -78,7 +78,7 @@
pagy.next.must_be_nil
end

it 'initiazes page 1' do
it 'initializes page 1' do
pagy = Pagy.new @vars.merge(page: 1)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -92,7 +92,7 @@
pagy.next.must_equal 2
end

it 'initiazes page 2' do
it 'initializes page 2' do
pagy = Pagy.new @vars.merge(page: 2)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -106,7 +106,7 @@
pagy.next.must_equal 3
end

it 'initiazes page 3' do
it 'initializes page 3' do
pagy = Pagy.new @vars.merge(page: 3)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -120,7 +120,7 @@
pagy.next.must_equal 4
end

it 'initiazes page 4' do
it 'initializes page 4' do
pagy = Pagy.new @vars.merge(page: 4)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -134,7 +134,7 @@
pagy.next.must_equal 5
end

it 'initiazes page 5' do
it 'initializes page 5' do
pagy = Pagy.new @vars.merge(page: 5)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -148,7 +148,7 @@
pagy.next.must_equal 6
end

it 'initiazes page 6' do
it 'initializes page 6' do
pagy = Pagy.new @vars.merge(page: 6)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -162,7 +162,7 @@
pagy.next.must_equal 7
end

it 'initiazes page 7' do
it 'initializes page 7' do
pagy = Pagy.new @vars.merge(page: 7)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -176,7 +176,7 @@
pagy.next.must_equal 8
end

it 'initiazes page 8' do
it 'initializes page 8' do
pagy = Pagy.new @vars.merge(page: 8)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -190,7 +190,7 @@
pagy.next.must_equal 9
end

it 'initiazes page 9' do
it 'initializes page 9' do
pagy = Pagy.new @vars.merge(page: 9)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -204,7 +204,7 @@
pagy.next.must_equal 10
end

it 'initiazes page 10' do
it 'initializes page 10' do
pagy = Pagy.new @vars.merge(page: 10)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand All @@ -218,7 +218,7 @@
pagy.next.must_equal 11
end

it 'initiazes page 11' do
it 'initializes page 11' do
pagy = Pagy.new @vars.merge(page: 11)
pagy.count.must_equal 103
pagy.pages.must_equal 11
Expand Down Expand Up @@ -251,7 +251,7 @@
end

it 'initializes items of last page of one' do
Pagy.new(items: 10, count: 0).items.must_equal 0
Pagy.new(items: 10, count: 0).items.must_equal 10
Pagy.new(items: 10, count: 4).items.must_equal 4
Pagy.new(items: 10, count: 10).items.must_equal 10
end
Expand Down

0 comments on commit 4e400c6

Please sign in to comment.