Class: Paginate::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/paginate/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Renderer

Returns a new instance of Renderer.



6
7
8
9
10
# File 'lib/paginate/renderer.rb', line 6

def initialize(options)
  @current_page = [options[:page].to_i, 1].max
  options.reverse_merge!(Paginate::Config.to_hash)
  @options = options.merge(:page => current_page)
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



4
5
6
# File 'lib/paginate/renderer.rb', line 4

def current_page
  @current_page
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/paginate/renderer.rb', line 3

def options
  @options
end

Instance Method Details

#processorObject



12
13
14
# File 'lib/paginate/renderer.rb', line 12

def processor
  @processor ||= Paginate::Base.new(options)
end

#renderObject



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
70
71
# File 'lib/paginate/renderer.rb', line 38

def render
  html = String.new
  previous_label = I18n.t("paginate.previous")
  previous_url = url_for(options[:page] - 1)
  next_label = I18n.t("paginate.next")
  next_url = url_for(options[:page] + 1)
  page_label = I18n.t("paginate.page", options)

  css = %w[ paginate ]
  css << "disabled" unless processor.previous_page? || processor.next_page?

  html << %[<ul class="#{css.join(" ")}">]

  # Previous page
  if processor.previous_page?
    html << %[<li class="previous-page"><a href="#{previous_url}" title="#{previous_label}">#{previous_label}</a></li>]
  else
    html << %[<li class="previous-page disabled"><span title="#{previous_label}">#{previous_label}</span></li>]
  end

  # Current page
  html << %[<li class="page"><span>#{page_label}</span></li>]

  # Next page
  if processor.next_page?
    html << %[<li class="next-page"><a href="#{next_url}" title="#{next_label}">#{next_label}</a></li>]
  else
    html << %[<li class="next-page disabled"><span title="#{next_label}">#{next_label}</span></li>]
  end

  html << %[</ul>]

  html.html_safe
end

#url_for(page) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/paginate/renderer.rb', line 16

def url_for(page)
  url = options[:url] || options[:fullpath]

  if url.respond_to?(:call)
    url = url.call(page).to_s.dup
  else
    url = url.dup

    re = Regexp.new("([&?])#{Regexp.escape(options[:param_name].to_s)}=[^&]*")
    url.gsub!(re, "\\1")
    url.gsub!(/[\?&]$/, "")
    url.gsub!(/&+/, "&")
    url.gsub!(/\?&/, "?")

    url << (url =~ /\?/ ? "&" : "?")
    url << page.to_query(options[:param_name])
  end

  url.gsub!(/&/, "&amp;")
  url
end