Giter Site home page Giter Site logo

more_paginate's Introduction

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)
  • sort_value_method (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) 2010 Luca Guidi, released under the MIT license.

more_paginate's People

Stargazers

jnbt avatar J. Garcia avatar Shripad Krishna avatar Johanna Brewer avatar Haifeng Cao avatar Vladyslav Shvedov avatar  avatar Martin Trejo avatar Andrey Prihodko avatar Steve Agalloco avatar Tommaso Passi avatar Marco Borromeo avatar Jorge Yau avatar Daniel Quirino Oliveira avatar mech avatar Jérémy Lecour avatar Luca Guidi avatar guillaume belleguic avatar Andrew Vit avatar Yaroslav Markin avatar Michael Erb avatar Michael Kovacs avatar Orlin M Bozhinov avatar

Watchers

James Cloos avatar  avatar

more_paginate'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.