Module: Kaminari::Helpers::HelperMethods

Includes:
UrlHelper
Defined in:
lib/kaminari/helpers/helper_methods.rb

Instance Method Summary collapse

Methods included from UrlHelper

#next_page_path, #next_page_url, #path_to_next_url, #prev_page_path, #prev_page_url

Instance Method Details

A simple “Twitter like” pagination link that creates a link to the next page.

Examples

Basic usage:

<%= link_to_next_page @items, 'Next Page' %>

Ajax:

<%= link_to_next_page @items, 'Next Page', remote: true %>

By default, it renders nothing if there are no more results on the next page. You can customize this output by passing a block.

<%= link_to_next_page @users, 'Next Page' do %>
  <span>No More Pages</span>
<% end %>


172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kaminari/helpers/helper_methods.rb', line 172

def link_to_next_page(scope, name, **options)
  next_page = path_to_next_page(scope, options)

  options.except! :params, :param_name
  options[:rel] ||= 'next'

  if next_page
    link_to name, next_page, options
  elsif block_given?
    yield
  end
end

A simple “Twitter like” pagination link that creates a link to the previous page.

Examples

Basic usage:

<%= link_to_previous_page @items, 'Previous Page' %>

Ajax:

<%= link_to_previous_page @items, 'Previous Page', remote: true %>

By default, it renders nothing if there are no more results on the previous page. You can customize this output by passing a block.

<%= link_to_previous_page @users, 'Previous Page' do %>
  <span>At the Beginning</span>
<% end %>


141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kaminari/helpers/helper_methods.rb', line 141

def link_to_previous_page(scope, name, **options)
  prev_page = path_to_prev_page(scope, options)

  options.except! :params, :param_name
  options[:rel] ||= 'prev'

  if prev_page
    link_to name, prev_page, options
  elsif block_given?
    yield
  end
end

#page_entries_info(collection, entry_name: nil) ⇒ Object

Renders a helpful message with numbers of displayed vs. total entries. Ported from mislav/will_paginate

Examples

Basic usage:

<%= page_entries_info @posts %>
#-> Displaying posts 6 - 10 of 26 in total

By default, the message will use the humanized class name of objects in collection: for instance, “project types” for ProjectType models. The namespace will be cutted out and only the last name will be used. Override this with the :entry_name parameter:

<%= page_entries_info @posts, entry_name: 'item' %>
#-> Displaying items 6 - 10 of 26 in total


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/kaminari/helpers/helper_methods.rb', line 201

def page_entries_info(collection, entry_name: nil)
  entry_name = if entry_name
                 entry_name.pluralize(collection.size, I18n.locale)
               else
                 collection.entry_name(count: collection.size).downcase
               end

  if collection.total_pages < 2
    t('helpers.page_entries_info.one_page.display_entries', entry_name: entry_name, count: collection.total_count)
  else
    from = collection.offset_value + 1
    to   = collection.offset_value + (collection.respond_to?(:records) ? collection.records : collection.to_a).size

    t('helpers.page_entries_info.more_pages.display_entries', entry_name: entry_name, first: from, last: to, total: collection.total_count)
  end.html_safe
end

#paginate(scope, paginator_class: Kaminari::Helpers::Paginator, template: nil, **options) ⇒ Object

A helper that renders the pagination links.

<%= paginate @articles %>

Options

  • :window - The “inner window” size (4 by default).

  • :outer_window - The “outer window” size (0 by default).

  • :left - The “left outer window” size (0 by default).

  • :right - The “right outer window” size (0 by default).

  • :params - url_for parameters for the links (:controller, :action, etc.)

  • :param_name - parameter name for page number in the links (:page by default)

  • :remote - Ajax? (false by default)

  • :paginator_class - Specify a custom Paginator (Kaminari::Helpers::Paginator by default)

  • :template - Specify a custom template renderer for rendering the Paginator (receiver by default)

  • :ANY_OTHER_VALUES - Any other hash key & values would be directly passed into each tag as :locals value.



116
117
118
119
120
121
122
# File 'lib/kaminari/helpers/helper_methods.rb', line 116

def paginate(scope, paginator_class: Kaminari::Helpers::Paginator, template: nil, **options)
  options[:total_pages] ||= scope.total_pages
  options.reverse_merge! current_page: scope.current_page, per_page: scope.limit_value, remote: false

  paginator = paginator_class.new (template || self), **options
  paginator.to_s
end

Renders rel=“next” and rel=“prev” links to be used in the head.

Examples

Basic usage:

In head:
<head>
  <title>My Website</title>
  <%= yield :head %>
</head>

Somewhere in body:
<% content_for :head do %>
  <%= rel_next_prev_link_tags @items %>
<% end %>

#-> <link rel="next" href="/items/page/3"><link rel="prev" href="/items/page/1">


236
237
238
239
240
241
242
243
244
# File 'lib/kaminari/helpers/helper_methods.rb', line 236

def rel_next_prev_link_tags(scope, options = {})
  next_page = path_to_next_page(scope, options)
  prev_page = path_to_prev_page(scope, options)

  output = String.new
  output << %Q|<link rel="next" href="#{next_page}">| if next_page
  output << %Q|<link rel="prev" href="#{prev_page}">| if prev_page
  output.html_safe
end