Class: Paginate::Renderer::Base

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

Direct Known Subclasses

List, More

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, options) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/paginate/renderer.rb', line 15

def initialize(view_context, options)
  @view_context = view_context
  @options = options.reverse_merge(Paginate.configuration.to_hash)
  @processor = Paginate::Base.new(nil, options)
end

Instance Attribute Details

#optionsObject (readonly)

Set the pagination options.



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

def options
  @options
end

#processorObject (readonly)

Set the object with defines all pagination methods like ‘Paginate::Base#next_page?`.



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

def processor
  @processor
end

#view_contextObject (readonly)

Set the view context. You can use this object to call view and url helpers.



9
10
11
# File 'lib/paginate/renderer.rb', line 9

def view_context
  @view_context
end

Instance Method Details

#next_urlObject

Return the URL for next page.



27
28
29
# File 'lib/paginate/renderer.rb', line 27

def next_url
  url_for(processor.page + 1)
end

#previous_urlObject

Return the URL for previous page.



22
23
24
# File 'lib/paginate/renderer.rb', line 22

def previous_url
  url_for(processor.page - 1)
end

#url_for(page) ⇒ Object

Compute the URL for a given page. It will keep track of all query string and replace the page parameter with the specified ‘page`.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paginate/renderer.rb', line 34

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