public
Description: Twitter like pagination for Rails
Homepage:
Clone URL: git://github.com/jodosha/more_paginate.git
name age message
file .gitignore Sat Oct 31 08:32:13 -0700 2009 Added log/* to .gitignore [jodosha]
file MIT-LICENSE Fri Oct 23 06:59:42 -0700 2009 first commit [jodosha]
file README.md Mon Nov 02 14:09:00 -0800 2009 Added more instructions to README [jodosha]
file Rakefile Sat Oct 31 11:31:15 -0700 2009 Minimum setup for javascript specs [jodosha]
directory assets/ Sat Oct 31 13:46:48 -0700 2009 Finished with javascript specs [jodosha]
file init.rb Sat Oct 31 07:17:35 -0700 2009 Specs for Helpers [jodosha]
file install.rb Sat Oct 31 09:31:48 -0700 2009 Code for install/uninstall hooks [jodosha]
directory lib/ Fri Dec 04 06:19:58 -0800 2009 Allow to specify the sort order via the #more_p... [jodosha]
directory spec/ Fri Dec 04 06:19:58 -0800 2009 Allow to specify the sort order via the #more_p... [jodosha]
directory tasks/ Sat Oct 31 11:31:15 -0700 2009 Minimum setup for javascript specs [jodosha]
file uninstall.rb Sat Oct 31 09:31:48 -0700 2009 Code for install/uninstall hooks [jodosha]
README.md

more_paginate

Twitter like pagination for Rails.

Basic usage

more_paginate provides a class and associations level method for paginate your records:

Tweet.paginate :all
person.tweets.paginate :all

It's just a tiny enforcement for ActiveRecord::Base#find and it accepts the following additional params:

  • sort_key
  • sort_value
  • sort_id
  • sort_order (optional)

Example

For a full working example, please visit more_paginate_example repository.

# app/models/tweet.rb
class Tweet < ActiveRecord::Base
  belongs_to :person

  def self.paginate_by_creation_date(params)
    paginate :all,
      :sort_key   => params[:sort_key] || "created_at",
      :sort_value => params[:sort_value],
      :sort_id    => params[:sort_id],
      :sort_order => "desc",
      :include    => :person
  end
end

# app/controllers/tweets_controller.rb
class TweetsController < ApplicationController
  def index
    @tweets = Tweet.paginate_by_creation_date params.dup

    respond_to do |format|
      format.html
      format.js { render :partial => "tweet_list" }
    end
  end
end

# app/views/tweets/index.html.erb
<h1>Tweets</h1>
<div id="tweets">
  <%= render "tweet_list" %>
</div>

# app/views/tweets/_tweet_list.html.erb
<ol class="tweetList">
<% @tweets.each do |tweet| -%>
  <li class="tweet">
    <%= avatar tweet.person %>
    <%= link_to h(tweet.person.nickname), person_path(tweet.person), :class => "bold" %>
    <%= truncate h(tweet.text), :length => 140 %><br />
    <span class="time"><%= link_to tweet.created_at.to_s(:db), tweet_path(tweet) %></span>
  </li>
<% end -%>
<ol>
<%= more_paginate @tweets %>

# public/javascripts/application.js
$(document).ready(function() {
  $("#more_link").morePaginate({ container: "#tweets" });
});

Acknowledgements

Copyright

Copyright (c) 2009 Luca Guidi, released under the MIT license.