PageAdams
ActiveRecord model pagination made easy.
Installation
Add this line to your application's Gemfile:
gem 'page_adams'And then execute:
$ bundle installOr install it yourself as:
$ gem install page_adamsUsage
Include PageAdams in every model you want to paginate
class Article < ApplicationRecord
include PageAdams
endThat's it. Now you can call Article.page 5 to get the fifth page of paginated results.
page returns a hash with the following structure:
> Article.page 1
=> {:records=>#<ActiveRecord::Relation [#<Article id: 1>]>, :current_page=>1, :total_pages=>4}in wich
- the
:recordskey holds an array of instances (or empty) (ActiveRecord::Relation) - the
:current_pagekey holds the current retrieved page (Integer) - the
:total_pageskey holds the total number of instances pages (Integer)
You can also specify the page size per method call
> pagination = Article.page(1, 7)
> pagination[:records].size
=> 7Configuration
At the moment, only page_size can be configured. Create 'config/initializers/page_adams.rb' and write
PageAdams.setup do |config|
config.page_size = 7
endTests
A basic suite of tests has been included into the gem, using RSpec.
Clone the project, enter the folder and run rspec.
License
The gem is available as open source under the terms of the MIT License.