annotate vendor/plugins/classic_pagination/lib/pagination_helper.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 module ActionView
Chris@0 2 module Helpers
Chris@0 3 # Provides methods for linking to ActionController::Pagination objects using a simple generator API. You can optionally
Chris@0 4 # also build your links manually using ActionView::Helpers::AssetHelper#link_to like so:
Chris@0 5 #
Chris@0 6 # <%= link_to "Previous page", { :page => paginator.current.previous } if paginator.current.previous %>
Chris@0 7 # <%= link_to "Next page", { :page => paginator.current.next } if paginator.current.next %>
Chris@0 8 module PaginationHelper
Chris@0 9 unless const_defined?(:DEFAULT_OPTIONS)
Chris@0 10 DEFAULT_OPTIONS = {
Chris@0 11 :name => :page,
Chris@0 12 :window_size => 2,
Chris@0 13 :always_show_anchors => true,
Chris@0 14 :link_to_current_page => false,
Chris@0 15 :params => {}
Chris@0 16 }
Chris@0 17 end
Chris@0 18
Chris@0 19 # Creates a basic HTML link bar for the given +paginator+. Links will be created
Chris@0 20 # for the next and/or previous page and for a number of other pages around the current
Chris@0 21 # pages position. The +html_options+ hash is passed to +link_to+ when the links are created.
Chris@0 22 #
Chris@0 23 # ==== Options
Chris@0 24 # <tt>:name</tt>:: the routing name for this paginator
Chris@0 25 # (defaults to +page+)
Chris@0 26 # <tt>:prefix</tt>:: prefix for pagination links
Chris@0 27 # (i.e. Older Pages: 1 2 3 4)
Chris@0 28 # <tt>:suffix</tt>:: suffix for pagination links
Chris@0 29 # (i.e. 1 2 3 4 <- Older Pages)
Chris@0 30 # <tt>:window_size</tt>:: the number of pages to show around
Chris@0 31 # the current page (defaults to <tt>2</tt>)
Chris@0 32 # <tt>:always_show_anchors</tt>:: whether or not the first and last
Chris@0 33 # pages should always be shown
Chris@0 34 # (defaults to +true+)
Chris@0 35 # <tt>:link_to_current_page</tt>:: whether or not the current page
Chris@0 36 # should be linked to (defaults to
Chris@0 37 # +false+)
Chris@0 38 # <tt>:params</tt>:: any additional routing parameters
Chris@0 39 # for page URLs
Chris@0 40 #
Chris@0 41 # ==== Examples
Chris@0 42 # # We'll assume we have a paginator setup in @person_pages...
Chris@0 43 #
Chris@0 44 # pagination_links(@person_pages)
Chris@0 45 # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
Chris@0 46 #
Chris@0 47 # pagination_links(@person_pages, :link_to_current_page => true)
Chris@0 48 # # => <a href="/?page=1/">1</a> <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
Chris@0 49 #
Chris@0 50 # pagination_links(@person_pages, :always_show_anchors => false)
Chris@0 51 # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a>
Chris@0 52 #
Chris@0 53 # pagination_links(@person_pages, :window_size => 1)
Chris@0 54 # # => 1 <a href="/?page=2/">2</a> ... <a href="/?page=10/">10</a>
Chris@0 55 #
Chris@0 56 # pagination_links(@person_pages, :params => { :viewer => "flash" })
Chris@0 57 # # => 1 <a href="/?page=2&amp;viewer=flash/">2</a> <a href="/?page=3&amp;viewer=flash/">3</a> ...
Chris@0 58 # # <a href="/?page=10&amp;viewer=flash/">10</a>
Chris@0 59 def pagination_links(paginator, options={}, html_options={})
Chris@0 60 name = options[:name] || DEFAULT_OPTIONS[:name]
Chris@0 61 params = (options[:params] || DEFAULT_OPTIONS[:params]).clone
Chris@0 62
Chris@0 63 prefix = options[:prefix] || ''
Chris@0 64 suffix = options[:suffix] || ''
Chris@0 65
Chris@0 66 pagination_links_each(paginator, options, prefix, suffix) do |n|
Chris@0 67 params[name] = n
Chris@0 68 link_to(n.to_s, params, html_options)
Chris@0 69 end
Chris@0 70 end
Chris@0 71
Chris@0 72 # Iterate through the pages of a given +paginator+, invoking a
Chris@0 73 # block for each page number that needs to be rendered as a link.
Chris@0 74 #
Chris@0 75 # ==== Options
Chris@0 76 # <tt>:window_size</tt>:: the number of pages to show around
Chris@0 77 # the current page (defaults to +2+)
Chris@0 78 # <tt>:always_show_anchors</tt>:: whether or not the first and last
Chris@0 79 # pages should always be shown
Chris@0 80 # (defaults to +true+)
Chris@0 81 # <tt>:link_to_current_page</tt>:: whether or not the current page
Chris@0 82 # should be linked to (defaults to
Chris@0 83 # +false+)
Chris@0 84 #
Chris@0 85 # ==== Example
Chris@0 86 # # Turn paginated links into an Ajax call
Chris@0 87 # pagination_links_each(paginator, page_options) do |link|
Chris@0 88 # options = { :url => {:action => 'list'}, :update => 'results' }
Chris@0 89 # html_options = { :href => url_for(:action => 'list') }
Chris@0 90 #
Chris@0 91 # link_to_remote(link.to_s, options, html_options)
Chris@0 92 # end
Chris@0 93 def pagination_links_each(paginator, options, prefix = nil, suffix = nil)
Chris@0 94 options = DEFAULT_OPTIONS.merge(options)
Chris@0 95 link_to_current_page = options[:link_to_current_page]
Chris@0 96 always_show_anchors = options[:always_show_anchors]
Chris@0 97
Chris@0 98 current_page = paginator.current_page
Chris@0 99 window_pages = current_page.window(options[:window_size]).pages
Chris@0 100 return if window_pages.length <= 1 unless link_to_current_page
Chris@0 101
Chris@0 102 first, last = paginator.first, paginator.last
Chris@0 103
Chris@0 104 html = ''
Chris@0 105
Chris@0 106 html << prefix if prefix
Chris@0 107
Chris@0 108 if always_show_anchors and not (wp_first = window_pages[0]).first?
Chris@0 109 html << yield(first.number)
Chris@0 110 html << ' ... ' if wp_first.number - first.number > 1
Chris@0 111 html << ' '
Chris@0 112 end
Chris@0 113
Chris@0 114 window_pages.each do |page|
Chris@0 115 if current_page == page && !link_to_current_page
Chris@0 116 html << page.number.to_s
Chris@0 117 else
Chris@0 118 html << yield(page.number)
Chris@0 119 end
Chris@0 120 html << ' '
Chris@0 121 end
Chris@0 122
Chris@0 123 if always_show_anchors and not (wp_last = window_pages[-1]).last?
Chris@0 124 html << ' ... ' if last.number - wp_last.number > 1
Chris@0 125 html << yield(last.number)
Chris@0 126 end
Chris@0 127
Chris@0 128 html << suffix if suffix
Chris@0 129
Chris@0 130 html
Chris@0 131 end
Chris@0 132
Chris@0 133 end # PaginationHelper
Chris@0 134 end # Helpers
Chris@0 135 end # ActionView