More-style pagination for Rails. As an example, consider the following story:
With moar, this story can be realized using the following code:
## app/controllers/posts_controller.rb
class PostsController < ApplicationController
# Step 1 (optional): set controller-specific pagination increments
moar_increments [10, 20, 30]
def index
# Step 2: apply pagination increments
@posts = moar(Post).order(:created_at)
end
end
<!-- app/views/posts/index.html.erb -->
<h1>Posts</h1>
<ul id="list-of-posts">
<% @posts.each do |post| %>
<li><%= link_to post.title, post %></li>
<% end %>
</ul>
<!-- Step 3: render pagination link -->
<%= link_to_more @posts, "#list-of-posts" %>
A few things to note:
link_to_more
helper automatically infers the link text ("Morelink_to_more
helper is a CSS selector<tbody>
element rather than the<table>
element.link_to_more
helper will render nothing when there are no moreif
required.For complete usage details, see the documentation for
moar_increments
,
moar
, and
link_to_more
.
The moar install generator will create two configuration files in your project directory: "config/initializers/moar.rb" and "config/locales/moar.en.yml".
"config/initializers/moar.rb" can be edited to change the default
pagination increments used when moar_increments
is not called, and to
change the query param used to indicate page number.
"config/locales/moar.en.yml" can be edited to change the link text
rendered by link_to_more
. Translations listed in this file are
provided a results_name
interpolation argument which contains the
humanized translated pluralized downcased name of the relevant model.
For example, if the translation string is "Need more %{results_name}!"
and the paginated results consist of CowBell
models, the rendered text
will be "Need more cow bells!".
Add the gem to your Gemfile:
$ bundle add moar
And run the installation generator:
$ rails generate moar:install
Run bin/test
to run the tests.