Made pagination faster #1334 [Stefan Kaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1832 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-07-14 08:02:01 +00:00
parent 807df4fcf0
commit 16595c93b2
3 changed files with 50 additions and 37 deletions

View File

@ -31,7 +31,7 @@ module ActionController
# instance variable, which is an ordered collection of model objects for the # instance variable, which is an ordered collection of model objects for the
# current page (at most 20, sorted by last name and first name), and a # current page (at most 20, sorted by last name and first name), and a
# <tt>@person_pages</tt> Paginator instance. The current page is determined # <tt>@person_pages</tt> Paginator instance. The current page is determined
# by the <tt>@params['page']</tt> variable. # by the <tt>params[:page]</tt> variable.
# #
# ==== Pagination for a single action # ==== Pagination for a single action
# #
@ -67,6 +67,7 @@ module ActionController
:conditions => nil, :conditions => nil,
:order_by => nil, :order_by => nil,
:join => nil, :join => nil,
:select => nil,
:parameter => 'page' :parameter => 'page'
} }
end end
@ -154,11 +155,13 @@ module ActionController
model.count(conditions,joins) model.count(conditions,joins)
end end
# Returns a collection of items for the given +model+ and +conditions+, # Returns a collection of items for the given +model+ and +options[conditions]+,
# ordered by +order_by+, for the current page in the given +paginator+. # ordered by +options[order_by]+, for the current page in the given +paginator+.
# Override this method to implement a custom finder. # Override this method to implement a custom finder.
def find_collection_for_pagination(model, conditions, order_by, join, paginator) def find_collection_for_pagination(model, options, paginator)
model.find_all(conditions, order_by, paginator.current.to_sql, join) model.find(:all, :conditions => options[:conditions], :order => options[:order_by],
:joins => options[:join], :select => options[:select],
:limit => options[:per_page], :offset => paginator.current.offset)
end end
protected :create_paginators_and_retrieve_collections, protected :create_paginators_and_retrieve_collections,
@ -171,9 +174,7 @@ module ActionController
count = count_collection_for_pagination(klass, options[:conditions], options[:join]) count = count_collection_for_pagination(klass, options[:conditions], options[:join])
paginator = Paginator.new(self, count, options[:per_page], page) paginator = Paginator.new(self, count, options[:per_page], page)
collection = find_collection_for_pagination(klass, options, paginator)
collection = find_collection_for_pagination(klass,
options[:conditions], options[:order_by], options[:join], paginator)
return paginator, collection return paginator, collection
end end
@ -196,6 +197,7 @@ module ActionController
@controller = controller @controller = controller
@item_count = item_count || 0 @item_count = item_count || 0
@items_per_page = items_per_page @items_per_page = items_per_page
@pages = {}
self.current_page = current_page self.current_page = current_page
end end
@ -210,44 +212,43 @@ module ActionController
page.paginator == self page.paginator == self
end end
page = page.to_i page = page.to_i
@current_page = has_page_number?(page) ? page : 1 @current_page_number = has_page_number?(page) ? page : 1
end end
# Returns a Page object representing this paginator's current page. # Returns a Page object representing this paginator's current page.
def current_page def current_page
self[@current_page] @current_page ||= self[@current_page_number]
end end
alias current :current_page alias current :current_page
# Returns a new Page representing the first page in this paginator. # Returns a new Page representing the first page in this paginator.
def first_page def first_page
self[1] @first_page ||= self[1]
end end
alias first :first_page alias first :first_page
# Returns a new Page representing the last page in this paginator. # Returns a new Page representing the last page in this paginator.
def last_page def last_page
self[page_count] @last_page ||= self[page_count]
end end
alias last :last_page alias last :last_page
# Returns the number of pages in this paginator. # Returns the number of pages in this paginator.
def page_count def page_count
return 1 if @item_count.zero? @page_count ||= @item_count.zero? ? 1 : @item_count.div(@items_per_page)
(@item_count / @items_per_page.to_f).ceil
end end
alias length :page_count alias length :page_count
# Returns true if this paginator contains the page of index +number+. # Returns true if this paginator contains the page of index +number+.
def has_page_number?(number) def has_page_number?(number)
return false unless number.is_a? Fixnum
number >= 1 and number <= page_count number >= 1 and number <= page_count
end end
# Returns a new Page representing the page with the given index # Returns a new Page representing the page with the given index
# +number+. # +number+.
def [](number) def [](number)
Page.new(self, number) @pages[number] ||= Page.new(self, number)
end end
# Successively yields all the paginator's pages to the given block. # Successively yields all the paginator's pages to the given block.
@ -318,13 +319,13 @@ module ActionController
# Returns a new Page object representing the page just before this # Returns a new Page object representing the page just before this
# page, or nil if this is the first page. # page, or nil if this is the first page.
def previous def previous
if first? then nil else Page.new(@paginator, @number - 1) end if first? then nil else @paginator[@number - 1] end
end end
# Returns a new Page object representing the page just after this # Returns a new Page object representing the page just after this
# page, or nil if this is the last page. # page, or nil if this is the last page.
def next def next
if last? then nil else Page.new(@paginator, @number + 1) end if last? then nil else @paginator[@number + 1] end
end end
# Returns a new Window object for this page with the specified # Returns a new Window object for this page with the specified
@ -368,11 +369,10 @@ module ActionController
# Returns an array of Page objects in the current window. # Returns an array of Page objects in the current window.
def pages def pages
(@first.number..@last.number).to_a.map {|n| @paginator[n]} (@first.number..@last.number).to_a.collect! {|n| @paginator[n]}
end end
alias to_a :pages alias to_a :pages
end end
end end
end end
end end

View File

@ -36,36 +36,45 @@ module ActionView
def pagination_links(paginator, options={}) def pagination_links(paginator, options={})
options.merge!(DEFAULT_OPTIONS) {|key, old, new| old} options.merge!(DEFAULT_OPTIONS) {|key, old, new| old}
window_pages = paginator.current.window(options[:window_size]).pages pagination_links_each(paginator, options[:window_size], options[:always_show_anchors], options[:link_to_current_page]) do |n|
link_to(n.to_s, { options[:name] => n }.update(options[:params]))
end
end
return if window_pages.length <= 1 unless # Iterate through the pages of a given +paginator+, invoking a
options[:link_to_current_page] # block for each page number that needs to be rendered as a link.
def pagination_links_each(paginator, window_size=2, always_show_anchors=true, link_to_current_page=false)
current_page = paginator.current_page
window_pages = current_page.window(window_size).pages
return if window_pages.length <= 1 unless link_to_current_page
first, last = paginator.first, paginator.last first, last = paginator.first, paginator.last
returning html = '' do html = ''
if options[:always_show_anchors] and not window_pages[0].first? if always_show_anchors and not (wp_first = window_pages[0]).first?
html << link_to(first.number, { options[:name] => first }.update( options[:params] )) html << yield(first.number)
html << ' ... ' if window_pages[0].number - first.number > 1 html << ' ... ' if wp_first.number - first.number > 1
html << ' ' html << ' '
end end
window_pages.each do |page| window_pages.each do |page|
if paginator.current == page && !options[:link_to_current_page] if current_page == page && !link_to_current_page
html << page.number.to_s html << page.number.to_s
else else
html << link_to(page.number, { options[:name] => page }.update( options[:params] )) html << yield(page.number)
end end
html << ' ' html << ' '
end end
if options[:always_show_anchors] && !window_pages.last.last? if always_show_anchors and not (wp_last = window_pages[-1]).last?
html << ' ... ' if last.number - window_pages[-1].number > 1 html << ' ... ' if last.number - wp_last.number > 1
html << link_to(paginator.last.number, { options[:name] => last }.update( options[:params])) html << yield(last.number)
end
end
end end
html
end end
end
end end # PaginationHelper
end # Helpers
end # ActionView

View File

@ -9,6 +9,10 @@ class Address
def Address.find_all(arg1, arg2, arg3, arg4) def Address.find_all(arg1, arg2, arg3, arg4)
[] []
end end
def self.find(*args)
[]
end
end end
class AddressesTestController < ActionController::Base class AddressesTestController < ActionController::Base