Project

foliate

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Rails pagination
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.9

Runtime

~> 5.1
 Project Readme

foliate

Pagination for Ruby on Rails.

Why another pagination library? Because a different approach leads to a cleaner and much simpler implementation.

How is it different? Instead of extending ActiveRecord, or defining singleton methods on result sets at runtime, foliate adds a single method to ActionController:

## app/controllers/posts_controller.rb
class PostsController < ApplicationController

  def index
    @posts = paginate(Post) # <-- single method
  end

end

Calling paginate with something like an ActiveRecord::Relation (or an Array) returns an ActiveRecord::Relation (or an Array) scoped to the current page of records, as dictated by params[:page].

Calling paginate also sets a @pagination instance variable for use in the view. This object can be passed directly to render, which will then render the "app/views/application/_pagination.html.erb" view partial:

<!-- app/views/posts/index.html.erb -->

Do something with @posts here...

<%= render @pagination %>

What does it look like? By default, something like this:

page input pagination

Appearance

The foliate installation generator creates "app/views/application/_pagination.html.erb" and "app/assets/stylesheets/pagination.scss" in your project directory. These files can be freely edited to suit your needs.

The installation generator also supports a --bootstrap option which causes the generated stylesheet to apply Bootstrap 4 classes via Sass @extend directives. This allows the pagination element to directly blend in with a Bootstrap-styled site. For this generated stylesheet to work, Bootstrap must be loaded into the same Sass scope. For example:

// in application.scss
@import "bootstrap";
@import "pagination";

Records per page

By default, foliate allots Foliate.config.default_per_page records per page, which can be configured in "config/initializers/foliate.rb". However, this can also be overridden on a per-Controller basis using the per_page: argument:

@posts = paginate(Post, per_page: 50)

Page param name

foliate uses the :page query param (i.e. params[:page]) to dictate the current page. This can be configured by setting Foliate.config.page_param in "config/initializers/foliate.rb". Doing so will properly affect both the paginate method and the Pagination object.

Avoiding SQL count on very large tables

To determine the total number of pages, foliate performs a SQL count query. On very large tables, count queries can have a noticeable performance cost. If you have a more performant method of estimating total record count, you can prevent the count query by specifying the total_records: argument:

@posts = paginate(Post, total_records: Post.cached_count)

Full documentation

For more information, see the full documentation.

Installation

Add this line to your application's Gemfile:

gem "foliate"

Then execute:

$ bundle install

And finally, run the installation generator:

$ rails generate foliate:install

Contributing

Run rake test to run the tests.

License

MIT License