Permalink
Browse files

More pagination speed #1334 [Stefan Kaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1839 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 1708a86 commit 45804e2c86228efcfb7d635525f0d1635a1b25c1 @dhh dhh committed Jul 15, 2005
@@ -31,7 +31,7 @@ module ActionController
# 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
# <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
#
@@ -235,7 +235,8 @@ def last_page
# Returns the number of pages in this paginator.
def page_count
- @page_count ||= @item_count.zero? ? 1 : @item_count.div(@items_per_page)
+ @page_count ||= @item_count.zero? ? 1 :
+ (q,r=@item_count.divmod(@items_per_page); r==0? q : q+1)
end
alias length :page_count
@@ -374,5 +375,6 @@ def pages
alias to_a :pages
end
end
+
end
-end
+end
@@ -18,7 +18,8 @@ module PaginationHelper
}
end
- # Creates a basic HTML link bar for the given +paginator+.
+ # Creates a basic HTML link bar for the given +paginator+.
+ # +html_options+ are passed to +link_to+.
#
# +options+ are:
# <tt>:name</tt>:: the routing name for this paginator
@@ -33,20 +34,25 @@ module PaginationHelper
# +false+)
# <tt>:params</tt>:: any additional routing parameters
# for page URLs
- def pagination_links(paginator, options={})
- options.merge!(DEFAULT_OPTIONS) {|key, old, new| old}
+ def pagination_links(paginator, options={}, html_options={})
+ name = options[:name] || DEFAULT_OPTIONS[:name]
+ params = (options[:params] || DEFAULT_OPTIONS[:params]).clone
- 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]))
+ pagination_links_each(paginator, options) do |n|
+ params[name] = n
+ link_to(n.to_s, params, html_options)
end
end
# Iterate through the pages of a given +paginator+, invoking a
# block for each page number that needs to be rendered as a link.
+ def pagination_links_each(paginator, options)
+ options = DEFAULT_OPTIONS.merge(options)
+ link_to_current_page = options[:link_to_current_page]
+ always_show_anchors = options[:always_show_anchors]
- 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
+ window_pages = current_page.window(options[:window_size]).pages
return if window_pages.length <= 1 unless link_to_current_page
first, last = paginator.first, paginator.last
@@ -55,26 +61,26 @@ def pagination_links_each(paginator, window_size=2, always_show_anchors=true, li
if always_show_anchors and not (wp_first = window_pages[0]).first?
html << yield(first.number)
html << ' ... ' if wp_first.number - first.number > 1
- html << ' '
- end
+ html << ' '
+ end
- window_pages.each do |page|
+ window_pages.each do |page|
if current_page == page && !link_to_current_page
- html << page.number.to_s
- else
+ html << page.number.to_s
+ else
html << yield(page.number)
- end
- html << ' '
end
-
+ html << ' '
+ end
+
if always_show_anchors and not (wp_last = window_pages[-1]).last?
html << ' ... ' if last.number - wp_last.number > 1
html << yield(last.number)
+ end
+
+ html
end
- html
- end
-
end # PaginationHelper
end # Helpers
end # ActionView

0 comments on commit 45804e2

Please sign in to comment.