Raimei

Rai-mei (雷鳴) is a very tiny library for pagination (and sorting columns).

It DOES ...

It DOES NOT ...

  • Render pager
  • Retrieve records from any storage
  • Sort records

This library is independent of ActiveRecord and other ORMs, and also does not rely on Rails and other frameworks.

Usage

Working examples (with Sinatra and Sequel) available here.

Terms

In following typical navigation:

<<< << < 5 6 [7] 8 9 > >> >>>

<<<
The #leading page. (typically 1)
<<
The #backward page.
<
The #previous page. (in this example, 6)
5
The #first visible page on the navigation.
[7]
The #current page.
9
The #last visible page on the navigation.
>
The #next page. (in this example, 8)
>>
The #forward page.
>>>
The #trailing page. (typically the total number of pages)

Raimei::Navigation

The Raimei::Navigation is a page navigation class, but it only manage page numbers. If you want to manage offsets of records, you can use Raimei::Pager instead.

nav = Raimei::Navigation.new(:total_pages         => 10,
                             :pages_on_navigation => 5,
                             :current_page        => 7)

puts "<<< " if nav.leading?
puts "<< "  if nav.backward?

nav.each do |page|
  puts "#{page}"
  puts "*" if page.current?
  puts " "
end

puts ">> "  if nav.forward?
puts ">>> " if nav.trailing?

# <<< << 5 6 7* 8 9 >>>

Raimei::Pager

The Raimei::Pager is a descendant of Raimei::Navigation, and provides record offset management in addition.

pager = Raimei::Pager.new(:total_entries       => 100,
                          :page_size           => 20,
                          :pages_on_navigation => 5,
                          :current_page        => 3)

# Mongoid flavour ORM example :)
records = Record.all.limit(pager.page_size).skip(pager.offset_for_current)
records.each do |record|
  puts record
end

puts "#{pager.entries_for_current} records."
puts "(#{pager.top_entry_index_for_current} - #{pager.bottom_entry_index_for_current})"

# see example in Raimei::Navigation for paging navigation.

Raimei::Sorter

Raimei::Sorter is a manager for sorting criterion of sortable table. Raimei::Sorter itself DOES NOT sort records :)

To use Raimei::Sorter, you have to require 'raimei/sorter' explicitly.

sorter = Raimei::Sorter.new([ [:foo, :asc], [:bar, :desc], [:baz, :asc] ])

params[:order] = sorter.link_for("bar")   # => "bar-"
sorter.sort_by! params[:order]
sorter.order                              # => [ [:bar, :desc], [:foo, :asc], [:baz, :asc] ]

params[:order] = sorter.link_for("baz")   # => "baz,bar-"
sorter.sort_by! params[:order]
sorter.order                              # => [ [:baz, :asc], [:bar, :desc], [:foo, :asc] ]

# You can supply sorter.order to ORM's sorting method
records = Record.all.order_by(sorter.order)

Installation

Add this line to your application's Gemfile:

gem 'raimei'

And then execute:

$ bundle

Or install it yourself as:

$ gem install raimei

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request