Skip to content

Commit

Permalink
Merge pull request planetscale#5 from planetscale/kaminari-without-count
Browse files Browse the repository at this point in the history
Make compatible with Kaminari without_count
  • Loading branch information
mscoutermarsh authored Sep 13, 2022
2 parents 925bf88 + abb202c commit ec1a64c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/fast_page/active_record_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ def deferred_join_load
# Must have a limit or offset defined
raise ArgumentError, "You must specify a limit or offset to use fast_page" if !limit_value && !offset_value

# We load 1 additional record to determine if there is a next page.
# This helps us avoid doing a count over all records
@values[:limit] = limit_value + 1 if limit_value
id_scope = dup
id_scope = id_scope.except(:includes) unless references_eager_loaded_tables?
ids = id_scope.pluck(:id)

if limit_value
@values[:limit] = limit_value - 1
# Record if there is a next page
@_has_next = ids.length > limit_value
ids = ids.first(limit_value)
end

if ids.empty?
@records = []
@loaded = true
Expand Down
6 changes: 4 additions & 2 deletions test/fast_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def test_returns_same_results
og = User.all.limit(5).offset(5).order(created_at: :desc)
fast = User.all.limit(5).offset(5).order(created_at: :desc).fast_page

assert_equal og, fast
assert_equal og.length, fast.length
assert_equal og.select(&:id), fast.select(&:id)
end

def test_errors_without_limit_or_offset
Expand All @@ -84,7 +85,8 @@ def test_works_limit_only
og = User.all.limit(5).order(created_at: :desc)
fast = User.all.limit(5).order(created_at: :desc).fast_page

assert_equal og, fast
assert_equal og.length, fast.length
assert_equal og.select(&:id), fast.select(&:id)
end

def test_works_offset_only
Expand Down
14 changes: 13 additions & 1 deletion test/kaminari_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ def test_kaminari_works
og_page = User.page(2).per(1).order(created_at: :desc)
fast_page = User.page(2).per(1).order(created_at: :desc).fast_page

assert_equal og_page, fast_page
assert_equal og_page.length, fast_page.length
assert_equal og_page.first.id, fast_page.first.id
assert_equal og_page.current_page, fast_page.current_page
assert_equal og_page.next_page, fast_page.next_page
assert_equal og_page.prev_page, fast_page.prev_page
end

def test_kaminari_works_without_count
og_page = User.page(2).per(1).order(created_at: :desc).without_count
fast_page = User.page(2).per(1).order(created_at: :desc).without_count.fast_page

assert_equal og_page.length, fast_page.length
assert_equal og_page.first.id, fast_page.first.id
assert_equal og_page.current_page, fast_page.current_page
assert_equal og_page.next_page, fast_page.next_page
assert_equal og_page.prev_page, fast_page.prev_page
Expand Down

0 comments on commit ec1a64c

Please sign in to comment.