Module: Kaminari::Helpers::HelperMethods
- Defined in:
- lib/kaminari/helpers/helper_methods.rb
Instance Method Summary collapse
-
#link_to_next_page(scope, name, **options) ⇒ Object
A simple “Twitter like” pagination link that creates a link to the next page.
-
#link_to_previous_page(scope, name, **options) ⇒ Object
(also: #link_to_prev_page)
A simple “Twitter like” pagination link that creates a link to the previous page.
-
#page_entries_info(collection, entry_name: nil) ⇒ Object
Renders a helpful message with numbers of displayed vs.
-
#paginate(scope, paginator_class: Kaminari::Helpers::Paginator, template: nil, **options) ⇒ Object
A helper that renders the pagination links.
-
#path_to_next_page(scope, options = {}) ⇒ Object
A helper that calculates the path to the next page.
-
#path_to_prev_page(scope, options = {}) ⇒ Object
A helper that calculates the path to the previous page.
-
#rel_next_prev_link_tags(scope, options = {}) ⇒ Object
Renders rel=“next” and rel=“prev” links to be used in the head.
Instance Method Details
#link_to_next_page(scope, name, **options) ⇒ Object
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 %>
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 75 def link_to_next_page(scope, name, **) next_page = path_to_next_page(scope, ) .except! :params, :param_name [:rel] ||= 'next' link_to_if next_page, name, next_page, do yield if block_given? end end |
#link_to_previous_page(scope, name, **options) ⇒ Object Also known as: link_to_prev_page
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 %>
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 46 def link_to_previous_page(scope, name, **) prev_page = path_to_prev_page(scope, ) .except! :params, :param_name [:rel] ||= 'prev' link_to_if prev_page, name, prev_page, do yield if block_given? 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
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 102 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 t('helpers.page_entries_info.more_pages.display_entries', entry_name: entry_name, first: collection.offset_value + 1, last: [collection.offset_value + collection.limit_value, collection.total_count].min, 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.
21 22 23 24 25 26 27 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 21 def paginate(scope, paginator_class: Kaminari::Helpers::Paginator, template: nil, **) [:total_pages] ||= scope.total_pages .reverse_merge! current_page: scope.current_page, per_page: scope.limit_value, remote: false paginator = paginator_class.new (template || self), paginator.to_s end |
#path_to_next_page(scope, options = {}) ⇒ Object
A helper that calculates the path to the next page.
Examples
Basic usage:
<%= path_to_next_page @items %>
#-> /items?page=2
It will return ‘nil` if there is no next page.
153 154 155 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 153 def path_to_next_page(scope, = {}) Kaminari::Helpers::NextPage.new(self, .reverse_merge(current_page: scope.current_page)).url if scope.next_page end |
#path_to_prev_page(scope, options = {}) ⇒ Object
A helper that calculates the path to the previous page.
Examples
Basic usage:
<%= path_to_prev_page @items %>
#-> /items
It will return ‘nil` if there is no previous page.
166 167 168 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 166 def path_to_prev_page(scope, = {}) Kaminari::Helpers::PrevPage.new(self, .reverse_merge(current_page: scope.current_page)).url if scope.prev_page end |
#rel_next_prev_link_tags(scope, options = {}) ⇒ Object
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" />
134 135 136 137 138 139 140 141 142 |
# File 'lib/kaminari/helpers/helper_methods.rb', line 134 def (scope, = {}) next_page = path_to_next_page(scope, ) prev_page = path_to_prev_page(scope, ) output = String.new output << tag(:link, rel: "next", href: next_page) if next_page output << tag(:link, rel: "prev", href: prev_page) if prev_page output.html_safe end |