Module: Paginate::Helper

Defined in:
lib/paginate/helper.rb

Instance Method Summary collapse

Instance Method Details

#paginate(collection, *args) ⇒ Object

Display pagination based on the collection and current page.

<%= paginate @posts %>
<%= paginate @posts, options %>
<%= paginate @posts, posts_path, options %>

The available options are:

  • :url: the URL which page numbers will be appended to. Can be proc or string.

  • :id: the HTML id that will identify the pagination block.

  • :size: the page size. When not specified will default to Paginate.configuration.size.

  • :param_name: the page param name. When not specified will default to Paginate.configuration.param_name.

  • :renderer: A class that will be used to render the pagination. When not specified will default to Paginate::Renderer::List.

    <%= paginate @posts, proc {|page| posts_path(page) } <%= paginate @posts, :url => proc {|page| posts_path(page) }

You don’t have to specify the URL; the current requested URI will be used if you don’t provide it.



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
# File 'lib/paginate/helper.rb', line 22

def paginate(collection, *args)
  options = args.extract_options!

  param_name = [
    options[:param_name],
    Paginate.configuration.param_name,
    :page
  ].compact.first

  renderer = [
    options[:renderer],
    Paginate.configuration.renderer,
    Paginate::Renderer::List
  ].compact.first

  options.merge!({
    collection: collection,
    page: params[param_name],
    param_name: param_name,
    fullpath: request.fullpath
  })

  options.merge!(url: args.first) if args.any?
  renderer.new(self, options).render
end

#render(*args, &block) ⇒ Object

Override the original render method, so we can strip the additional item from the collection, without changing your workflow with the iterate “always felt dirty” method.

To render a paginated set, just pass the :paginate => true option. You can also provide a custom size with :size => number

<%= render @posts, :paginate => true %>
<%= render @pots, :paginate => true, :size => 20 %>
<%= render "post", :collection => @posts, :paginate => true, :size => 20 %>


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/paginate/helper.rb', line 59

def render(*args, &block)
  options    = args.extract_options!
  paginated  = options.delete(:paginate)
  size       = options.delete(:size) { Paginate.configuration.size }

  return super(*[*args, options], &block) unless paginated
  collection = options.delete(:collection) { args.shift }
  collection = collection[0, size]

  super(collection, *[*args, options], &block)
end