ShowMeTheMoney-17 - I will paginate.. with some help

Iteration#2 Day 4/10

You know when I run rake on the command line to run my tests, i get a lot of deprecation warnings ... which stink! I have been secretly plotting the death of those horrid lil pagination Deprecation warnings. When I run rake from the command prompt, they get in my way of seeing the test results

DEPRECATION WARNING: paginate is deprecated and will be removed from Rails 2.0 (Pagination is moving to a plugin in Rails 2.0: script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination) See http://www.rubyonrails.org/deprecation for details. (called from list at L:/Gishu/Ruby/Rails/ShowMeTheMoney/app/controllers/inflow_controller.rb:12)

After some initial investigation to determine the effort it would take, I find it’s really simple – Its going to be tough going back to the non-rails world out there.
Hey! We're moving.. to will_paginate

The nice folks at errtheblog.com have come up with a better way for pagination (the mechanism to display those next previous page links at the bottom). The current way has some performance issues and needs more code to be written. It has been moved out of the rails 2.0 into a plugin called classic_pagination and hence the warnings.
Now we’ll see how to move to the new way in less than half the amount of typing I did for this post till now.
  • First install the plugin like this..
CmdPrompt> ruby script\plugin install svn://errtheblog.com/svn/plugins/will_paginate
  • Next off to the OutflowController’s list method. There we see..
@expense_pages, @expenses = paginate :expenses, :order=>'created_at', :per_page => 10

Off with the old 2 paginator and array objects and.. in with the new WillPaginate::Collection object
@expenses = Expense.paginate :page=>params[:page], :per_page=>10, :order=>’created_at’


  • Finally we go to the list action’s view. Near the end of the page, we have

<%= link_to 'Previous page', { :page => @expense_pages.current.previous } if @expense_pages.current.previous %>
<%= link_to 'Next page', { :page => @expense_pages.current.next } if @expense_pages.current.next %>

Now check this out.. I love it when you shrink code while keeping the functionality intact. You will agree that this is much neater.

<%= will_paginate @expenses %>


We try it out in our browser and it works as advertised. Awesome. Let’s update the InflowController similarly.. All clear. Hit F5 in firefox… looks good. Rake from the command line is much better now without all those pesky warnings. Of course now we have integrated test running with green bars and the works within Aptana Studio. Groovy! (In case you hit some icebergs.. here’s the Railscasts tutorial video and words straight from the creators at ErrTheBlog )
Update: Will_paginate has moved... http://github.com/mislav/will_paginate/wikis/installation




No comments:

Post a Comment