Code Monkey home page Code Monkey logo

paginator's Introduction

Paginator

Paginator is a simple pagination class that provides a generic interface suitable for use in any Ruby program.

Paginator doesn't make any assumptions as to how data is retrieved; you just have to provide it with the total number of objects and a way to pull a specific set of objects based on the offset and number of objects per page.

Examples

In both of these examples I'm using a PER_PAGE constant (the number of items per page), but it's merely for labeling purposes.

You could, of course, just pass in the number of items per page directly to the initializer without assigning it somewhere beforehand.

In Plain Ruby

bunch_o_data = (1..60).to_a
pager = Paginator.create(bunch_o_data.size, PER_PAGE) do |offset, per_page|
  bunch_o_data[offset,per_page]
end
pager.each do |page|
  puts "Page ##{page.number}"
  page.each do |item|
    puts item
  end
end

In Rails

Nothing changes with Paginator; you just use ActiveRecord from within the block you provide.

A controller action:

def index
  @pager = Paginator.create(Foo.count, PER_PAGE) do |offset, per_page|
    Foo.limit(per_page).offset(offset)
  end
  @page = @pager.page(params[:page])
end

In your view:

<% @page.each do |foo| %>
  <%# Show something for each item %>
<% end %>
<%= @page.number %>
<%= link_to("Prev", foos_url(page: @page.prev.number)) if @page.prev? %>
<%= link_to("Next", foos_url(page: @page.next.number)) if @page.next? %>

License

See LICENSE.txt.

paginator's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

paginator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.