diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index d863fbdea..d542da8a1 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -3,11 +3,13 @@ module PaginationHelper html = '
" diff --git a/lib/danbooru/paginator/active_record_extension.rb b/lib/danbooru/paginator/active_record_extension.rb index 1864cc215..667c8dc73 100644 --- a/lib/danbooru/paginator/active_record_extension.rb +++ b/lib/danbooru/paginator/active_record_extension.rb @@ -31,7 +31,7 @@ module Danbooru end def paginate_sequential_before(before_id = nil) - c = limit(records_per_page) + c = limit(records_per_page + 1) if before_id.to_i > 0 c = c.where("#{table_name}.id < ?", before_id.to_i) @@ -44,7 +44,7 @@ module Danbooru end def paginate_sequential_after(after_id) - limit(records_per_page).where("#{table_name}.id > ?", after_id.to_i).reorder("#{table_name}.id asc").tap do |obj| + limit(records_per_page + 1).where("#{table_name}.id > ?", after_id.to_i).reorder("#{table_name}.id asc").tap do |obj| obj.extend(SequentialCollectionExtension) obj.sequential_paginator_mode = :after end diff --git a/lib/danbooru/paginator/sequential_collection_extension.rb b/lib/danbooru/paginator/sequential_collection_extension.rb index 071b3a853..9cee8fc4a 100644 --- a/lib/danbooru/paginator/sequential_collection_extension.rb +++ b/lib/danbooru/paginator/sequential_collection_extension.rb @@ -4,18 +4,26 @@ module Danbooru attr_accessor :sequential_paginator_mode def is_first_page? - size == 0 + if sequential_paginator_mode == :before + false + else + size <= records_per_page + end end def is_last_page? - size == 0 + if sequential_paginator_mode == :after + false + else + size <= records_per_page + end end def to_a if sequential_paginator_mode == :before - super + super.first(records_per_page) else - super.reverse + super.first(records_per_page).reverse end end end diff --git a/test/unit/post_sets/favorite_test.rb b/test/unit/post_sets/favorite_test.rb index e9257a823..3aac726b1 100644 --- a/test/unit/post_sets/favorite_test.rb +++ b/test/unit/post_sets/favorite_test.rb @@ -34,10 +34,15 @@ module PostSets assert_equal(1, @set.posts.size) assert_equal(@post_1.id, @set.posts.first.id) end + + should "know what page it's on" do + refute(@set.favorites.is_first_page?) + refute(@set.favorites.is_last_page?) + end end end - context "a favorite set for after the second most recent post" do + context "a favorite set for after the third most recent post" do setup do id = ::Favorite.where(:user_id => @user.id, :post_id => @post_2.id).first.id ::Favorite.stubs(:records_per_page).returns(1) @@ -45,10 +50,55 @@ module PostSets end context "a sequential paginator" do - should "return the most recent element" do + should "return the second most recent element" do assert_equal(1, @set.posts.size) assert_equal(@post_1.id, @set.posts.first.id) end + + should "know what page it's on" do + refute(@set.favorites.is_first_page?) + refute(@set.favorites.is_last_page?) + end + end + end + + context "a favorite set for before the second most recent post" do + setup do + id = ::Favorite.where(:user_id => @user.id, :post_id => @post_1.id).first.id + ::Favorite.stubs(:records_per_page).returns(1) + @set = PostSets::Favorite.new(@user.id, "b#{id}") + end + + context "a sequential paginator" do + should "return the third most recent element" do + assert_equal(1, @set.posts.size) + assert_equal(@post_2.id, @set.posts.first.id) + end + + should "know what page it's on" do + refute(@set.favorites.is_first_page?) + assert(@set.favorites.is_last_page?) + end + end + end + + context "a favorite set for after the second most recent post" do + setup do + id = ::Favorite.where(:user_id => @user.id, :post_id => @post_1.id).first.id + ::Favorite.stubs(:records_per_page).returns(1) + @set = PostSets::Favorite.new(@user.id, "a#{id}") + end + + context "a sequential paginator" do + should "return the most recent element" do + assert_equal(1, @set.posts.size) + assert_equal(@post_3.id, @set.posts.first.id) + end + + should "know what page it's on" do + assert(@set.favorites.is_first_page?) + refute(@set.favorites.is_last_page?) + end end end @@ -63,6 +113,11 @@ module PostSets assert_equal(1, @set.posts.size) assert_equal(@post_1.id, @set.posts.first.id) end + + should "know what page it's on" do + refute(@set.favorites.is_first_page?) + refute(@set.favorites.is_last_page?) + end end end @@ -76,6 +131,11 @@ module PostSets assert_equal(1, @set.posts.size) assert_equal(@post_3.id, @set.posts.first.id) end + + should "know what page it's on" do + assert(@set.favorites.is_first_page?) + refute(@set.favorites.is_last_page?) + end end end end