Skip to content

Commit

Permalink
Fixed a problem in paginator with pages of size one.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/squirrel/trunk@114 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed May 14, 2007
1 parent 8591a2a commit 0d214f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/paginator.rb
Expand Up @@ -12,15 +12,15 @@ class Paginator < Array
def initialize opts={}
@total_results = opts[:count].to_i
@limit = opts[:limit].to_i
@offset = opts[:offset].to_i + 1
@offset = opts[:offset].to_i

@per_page = @limit
@current = (@offset / @limit) + 1
@first = 1
@last = ((@total_results-1) / @limit) + 1
@next = @current + 1 if @current < @last
@previous = @current - 1 if @current > 1
@current_range = (@offset..([@offset+@limit-1, @total_results].min))
@current_range = ((@offset+1)..([@offset+@limit, @total_results].min))

(@first..@last).each do |page|
self[page-1] = Page.new((page-1) * @per_page, @per_page, page, @per_page)
Expand Down
16 changes: 16 additions & 0 deletions test/squirrel_test.rb
Expand Up @@ -146,6 +146,22 @@ def test_does_include_paginator_on_paged_queries
assert_not_nil posts.total_results
end

def test_paginator_low_edge_cases
pages = Thoughtbot::Squirrel::Paginator.new(:count => 100, :offset => 0, :limit => 1)
assert_equal 100, pages.last
assert_equal 1, pages.current
assert_equal 1, pages.first
assert_equal (1..1), pages.current_range
end

def test_paginator_high_edge_cases
pages = Thoughtbot::Squirrel::Paginator.new(:count => 100, :offset => 99, :limit => 1)
assert_equal 100, pages.last
assert_equal 100, pages.current
assert_equal 1, pages.first
assert_equal (100..100), pages.current_range
end

def test_pages_are_sane
assert posts = Post.find(:all) { id <=> (1..6); paginate :page => 2, :per_page => 4 }
assert_not_nil posts.pages
Expand Down

0 comments on commit 0d214f5

Please sign in to comment.