diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/pagination_helper.rb | 42 |
1 files changed, 24 insertions, 18 deletions
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 |