Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add :without_count option for paginate #127

Open
wants to merge 1 commit into from

2 participants

@ccutrer

Avoids doing a count against the database, by fetching one more item,
and adjusting total_entries to be however many were fetched.

@ccutrer ccutrer Add :without_count option for paginate
Avoids doing a count against the database, by fetching one more item,
and adjusting total_entries to be however many were fetched.
bf91c59
@joevandyk

Doesn't apply to the master branch anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 29, 2011
  1. @ccutrer

    Add :without_count option for paginate

    ccutrer authored
    Avoids doing a count against the database, by fetching one more item,
    and adjusting total_entries to be however many were fetched.
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 3 deletions.
  1. +10 −3 lib/will_paginate/finder.rb
View
13 lib/will_paginate/finder.rb
@@ -65,6 +65,7 @@ def paginate(*args)
options = args.pop
page, per_page, total_entries = wp_parse_options(options)
finder = (options[:finder] || 'find').to_s
+ without_count = options[:without_count]
if finder == 'find'
# an array of IDs may have been given:
@@ -74,13 +75,19 @@ def paginate(*args)
end
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
- count_options = options.except :page, :per_page, :total_entries, :finder
+ count_options = options.except :page, :per_page, :total_entries, :finder, :without_count
find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page)
+ find_options.update(:limit => pager.per_page + 1) if without_count
args << find_options
# @options_from_last_find = nil
- pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? })
-
+ entries = send(finder, *args) { |*a| yield(*a) if block_given? }
+ if (without_count)
+ pager.total_entries = pager.offset + entries.size
+ entries.slice!(pager.per_page)
+ end
+ pager.replace(entries)
+
# magic counting for user convenience:
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
end
Something went wrong with that request. Please try again.