<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pieces of Web &#187; Plugins</title>
	<atom:link href="http://danengle.us/tag/plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://danengle.us</link>
	<description>Dan Engle's Rails and Web Development Blog</description>
	<lastBuildDate>Fri, 29 May 2009 21:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extending the will_paginate Rails Plugin</title>
		<link>http://danengle.us/2009/03/extending-the-will_paginate-rails-plugins/</link>
		<comments>http://danengle.us/2009/03/extending-the-will_paginate-rails-plugins/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 19:09:48 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[will_paginate]]></category>

		<guid isPermaLink="false">http://danengle.us/?p=44</guid>
		<description><![CDATA[Sometimes plugins only give you 99% of what you need.  What do you do when you need a little more functionality?  You add it yourself.  See how I added a couple extra methods to Mislav's will_paginate plugin here.]]></description>
			<content:encoded><![CDATA[<p>Recently I needed a little extra functionality for paginating.  Specifically, I wanted to know if I was on the last page, and if so, add some content below my list of paginated stuff.  Unfortunately, <a href="http://github.com/mislav/will_paginate/tree/master">Mislav&#8217;s, will_paginate plugin</a> does not provide those methods.  All I wanted to be able to (with a collection of @products) do is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@products</span>.<span style="color:#5A0A0A; font-weight:bold;">last_page</span>? <span style="color:#006600; font-weight:bold;">%&gt;</span>
  here is the content
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Adding that method is actually pretty easy.  While I&#8217;m at it, it makes sense to add a first_page? method also.  Will_paginate has the WillPaginate::Collection class that already adds some additional properties to make paginating in views easy.  I just needed to add a little more to it.  To start, I created a new file named will_paginate_ext.rb in my lib directory.  Inside I added&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> WillPaginateExt
  <span style="color:#9966CC; font-weight:bold;">def</span> first_page?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">current_page</span> == <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> last_page?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">current_page</span> == <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">total_pages</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">WillPaginate::Collection</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> WillPaginateExt
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>&#8230;and inside my environment.rb file, below and outside of the Rails::Initializer block, add the line&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'will_paginate_ext'</span></pre></div></div>

<p>With those simple additions, I add two new methods to the will_paginate plugin and am now able to figure out if I am on the first or last page of pagination and add content as necessary.</p>
<p>Is there a better way to add functionality to plugins or other objects?  Let me know in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://danengle.us/2009/03/extending-the-will_paginate-rails-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
