diff --git a/docs/api/pagy.md b/docs/api/pagy.md index 0817ea28f..f18a1272a 100644 --- a/docs/api/pagy.md +++ b/docs/api/pagy.md @@ -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) | @@ -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` | @@ -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) diff --git a/lib/pagy.rb b/lib/pagy.rb index cdd68f196..654b7e70a 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -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] diff --git a/test/pagy_test.rb b/test/pagy_test.rb index 6734976ee..5aaf4955a 100644 --- a/test/pagy_test.rb +++ b/test/pagy_test.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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