aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-20 23:12:05 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-20 23:12:05 +0000
commitd578196f9a5b583c167d60d0f10d7684f41aa318 (patch)
tree57601f77093382d6fe43718d13c320add23e46f5 /actionpack/lib/action_view
parent409d56abda45ee380d6a903cbf835e7a770061d8 (diff)
downloadrails-d578196f9a5b583c167d60d0f10d7684f41aa318.tar.gz
rails-d578196f9a5b583c167d60d0f10d7684f41aa318.tar.bz2
rails-d578196f9a5b583c167d60d0f10d7684f41aa318.zip
Added pagination support through both a controller and helper add-on #817 [Sam Stephenson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@949 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/pagination_helper.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/pagination_helper.rb b/actionpack/lib/action_view/helpers/pagination_helper.rb
new file mode 100644
index 0000000000..cb3c3807eb
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/pagination_helper.rb
@@ -0,0 +1,65 @@
+module ActionView
+ module Helpers
+ # Provides methods for linking to ActionController::Pagination objects.
+ module PaginationHelper
+ unless const_defined?(:DEFAULT_OPTIONS)
+ DEFAULT_OPTIONS = {
+ :name => :page,
+ :window_size => 2,
+ :always_show_anchors => true,
+ :link_to_current_page => false,
+ :params => {}
+ }
+ end
+
+ # Creates a basic HTML link bar for the given +paginator+.
+ #
+ # +options+ are:
+ # <tt>:name</tt>:: the routing name for this paginator
+ # (defaults to +page+)
+ # <tt>:window_size</tt>:: the number of pages to show around
+ # the current page (defaults to +2+)
+ # <tt>:always_show_anchors</tt>:: whether or not the first and last
+ # pages should always be shown
+ # (defaults to +true+)
+ # <tt>:link_to_current_page</tt>:: whether or not the current page
+ # should be linked to (defaults to
+ # +false+)
+ # <tt>:pararms</tt>:: any additional routing parameters
+ # for page URLs
+ def pagination_links(paginator, options={})
+ options.merge!(DEFAULT_OPTIONS) {|key, old, new| old}
+
+ window_pages = paginator.current.window(options[:window_size]).pages
+
+ return if window_pages.length <= 1 unless
+ options[:link_to_current_page]
+
+ first, last = paginator.first, paginator.last
+
+ returning html = '' do
+ if options[:always_show_anchors] and not window_pages[0].first?
+ html << link_to(first.number, options[:name] => first)
+ html << ' ... ' if window_pages[0].number - first.number > 1
+ html << ' '
+ end
+
+ window_pages.each do |page|
+ if paginator.current == page && !options[:link_to_current_page]
+ html << page.number.to_s
+ else
+ html << link_to(page.number, options[:name] => page)
+ end
+ html << ' '
+ end
+
+ if options[:always_show_anchors] && !window_pages.last.last?
+ html << ' ... ' if last.number - window_pages[-1].number > 1
+ html << link_to(paginator.last.number, options[:name] => last)
+ end
+ end
+ end
+
+ end
+ end
+end