diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-15 20:27:38 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-15 20:27:38 +0000 |
commit | 45804e2c86228efcfb7d635525f0d1635a1b25c1 (patch) | |
tree | f286bddb5813d6d49ee251428058cda2c0f62539 | |
parent | 1708a863a5ed9ea37010de099fbe68193e67f4ba (diff) | |
download | rails-45804e2c86228efcfb7d635525f0d1635a1b25c1.tar.gz rails-45804e2c86228efcfb7d635525f0d1635a1b25c1.tar.bz2 rails-45804e2c86228efcfb7d635525f0d1635a1b25c1.zip |
More pagination speed #1334 [Stefan Kaes]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1839 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/lib/action_controller/pagination.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/pagination_helper.rb | 42 |
2 files changed, 29 insertions, 21 deletions
diff --git a/actionpack/lib/action_controller/pagination.rb b/actionpack/lib/action_controller/pagination.rb index 4b936c0ee7..6b9294f963 100644 --- a/actionpack/lib/action_controller/pagination.rb +++ b/actionpack/lib/action_controller/pagination.rb @@ -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 @@ module ActionController # 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 @@ module ActionController alias to_a :pages end end + end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_view/helpers/pagination_helper.rb b/actionpack/lib/action_view/helpers/pagination_helper.rb index 6f6380ec70..51510599fb 100644 --- a/actionpack/lib/action_view/helpers/pagination_helper.rb +++ b/actionpack/lib/action_view/helpers/pagination_helper.rb @@ -18,7 +18,8 @@ module ActionView } 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 ActionView # +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 @@ module ActionView 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 |