Module: ActiveScaffold::Helpers::PaginationHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/helpers/pagination_helpers.rb

Instance Method Summary collapse

Instance Method Details


4
5
6
# File 'lib/active_scaffold/helpers/pagination_helpers.rb', line 4

def pagination_ajax_link(page_number, url_options, options)
  link_to page_number, url_options.merge(:page => page_number), options.merge(:class => 'as_paginate')
end

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_scaffold/helpers/pagination_helpers.rb', line 18

def pagination_ajax_links(current_page, url_options, options, inner_window, outer_window)
  start_number = current_page.number - inner_window
  end_number = current_page.number + inner_window
  start_number = 1 if start_number <= 0
  if current_page.pager.infinite?
    offsets = [20, 100]
  elsif end_number > current_page.pager.last.number
    end_number = current_page.pager.last.number
  end

  html = []
  if current_page.number == 1
    last_page = 0
  else
    last_page = 1
    last_page.upto([last_page + outer_window, current_page.number - 1].min) do |num|
      html << pagination_ajax_link(num, url_options, options)
      last_page = num
    end
  end
  if current_page.pager.infinite?
    offsets.reverse.each do |offset|
      page = current_page.number - offset
      next unless page < start_number && page > last_page

      html << '..' if page > last_page + 1
      html << pagination_ajax_link(page, url_options, options)
      last_page = page
    end
  end
  html << '..' if start_number > last_page + 1

  [start_number, last_page + 1].max.upto(end_number) do |num|
    html << if current_page.number == num
              (:span, num.to_s, :class => 'as_paginate current')
            else
              pagination_ajax_link(num, url_options, options)
            end
  end

  if current_page.pager.infinite?
    offsets.each do |offset|
      html << '..' << pagination_ajax_link(current_page.number + offset, url_options, options)
    end
  else
    html << '..' unless end_number >= current_page.pager.last.number - outer_window - 1
    [end_number + 1, current_page.pager.last.number - outer_window].max.upto(current_page.pager.last.number) do |num|
      html << pagination_ajax_link(num, url_options, options)
    end
  end
  safe_join html, ' '
end

#pagination_url_options(url_options = nil) ⇒ Object


8
9
10
11
12
13
14
15
16
# File 'lib/active_scaffold/helpers/pagination_helpers.rb', line 8

def pagination_url_options(url_options = nil)
  if url_options.nil?
    url_options = {action: @pagination_action || :index}
    # :id needed because rails reuse it even if it was deleted from params (like do_refresh_list does)
    url_options[:id] = nil if @remove_id_from_list_links
    url_options = params_for(url_options)
  end
  url_options
end