Many times we need to paginate some model ordering it from newest to oldest. An example of this is when we paginate the latests posts, the latests comments, latests tickets and so on.
will_paginate_latests is a plugin for Rails that uses the famous gem will_paginate underneat and lets you write in the controller:
paginate_latests(:twitts)
instead of:
Twitt.will_paginate(:page => params[:page], :order => 'id DESC')
less code and more readable. If you’re asking about the page parameter, it is taken automatically by the plugin, you don’t have to care!
In the root folder your Rails project:
script/plugin install git://github.com/dcrec1/will_paginate_latests.git
In the controller:
paginate_latests(:tickets)
If you want more than 10 records per page, 20 for example, you can do:
paginate_latests(:blog_posts, :per_page => 20)
In the first example the model is Ticket and in the second the model is BlogPost.
Copyright © 2008 [name of plugin creator]
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.