<?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>The Wojo Group &#187; pages</title>
	<atom:link href="http://www.thewojogroup.com/tag/pages/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thewojogroup.com</link>
	<description>The musings of a small creative media company.</description>
	<lastBuildDate>Tue, 11 May 2010 00:10:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Pagination with Rails</title>
		<link>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/</link>
		<comments>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/#comments</comments>
		<pubDate>Tue, 05 May 2009 14:07:17 +0000</pubDate>
		<dc:creator>Brett Wejrowski</dc:creator>
				<category><![CDATA[Motionspire]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.thewojogroup.com/?p=510</guid>
		<description><![CDATA[While I was developing Motionspire, I needed to implement a pagination system for the video content.  I looked through the existing plugins, but found that I really desired something extremely simple and custom to work within my search controller.  With only a few lines of code, I created a very basic system for managing pagination [...]]]></description>
			<content:encoded><![CDATA[<p>While I was developing <a href="http://www.motionspire.com">Motionspire</a>, I needed to implement a pagination system for the video content.  I looked through the existing plugins, but found that I really desired something extremely simple and custom to work within my search controller.  With only a few lines of code, I created a very basic system for managing pagination that can be ported to other sites with ease.</p>
<h4>The Search Controller</h4>
<p>The Motionspire search controller uses one function and view for returning any content, whether it&#8217;s filtered or not, so the pagination only required one copy of the code to be implemented.  I keep a session variable for maintaining the user&#8217;s preference for items per page, and if that parameter has been sent to the search function I set the session variable :</p>
<pre name="code" class="ruby">

if params[:itemsPerPage] then session[:itemsPerPage] = params[:itemsPerPage] end
</pre>
<p>When I run though the search function, I see if a page parameter has been passed:</p>
<pre name="code" class="ruby">

if params[:page] then @page = params[:page].to_i else @page = 1 end
</pre>
<p>After I retrieve the articles pertaining to the search parameters (in this case the results are stored into an array called @videos), I check the :itemsPerPage preference and find the last possible page:</p>
<pre name="code" class="ruby">

if session[:itemsPerPage] then @per = session[:itemsPerPage].to_i else @per = 24 end

@lastpage = @videos.length/(@per) + 1

if @videos.length%(@per) == 0

    @lastpage = @lastpage - 1

end
</pre>
<p>I have a default itemsPerPage value of 24 hardcoded, and you can change this for your search.</p>
<p>From here, we simply return the search results for that page, using the current page and the items per page preference:</p>
<pre name="code" class="ruby">

@videos = @videos[((@page-1)*@per),@per]
</pre>
<p><em>For search queries within a large data set, I would recommend using these parameters with the sql query with the &#8216;ORDER BY&#8217; and &#8216;LIMIT&#8217; in order to avoid returning extremely large results unnecessarily.</em></p>
<h4>The Helper</h4>
<p>First, I created a short helper function that finds the min and max pages, ensuring we don&#8217;t show a negative page or a page that doesn&#8217;t exist:</p>
<pre name="code" class="ruby">

def minmaxpage(current,total,option)
    min = current - 2
    if min &lt; 1
        min = 1
        max = 1 + 4
        if max &gt; total then max = total end
    end
    max = min + 4
    if max &gt; total
        max = total
        min = max - 4
        while min &lt; 1
            min = min + 1
        end
    end
    if option == &#039;min&#039; then return min else return max end
end
</pre>
<h4>The View</h4>
<p>For the content from the view, we use the @videos array to present the content for the page.  The only custom coding necessary is in the links for the pages.  For this site, I provide a first and last page link, along with 2 pages on either side of the current page.  You can manipulate this code how you want, but the basic idea will remain the same.</p>
<p>First we grab the min and max page from the helper:</p>
<pre name="code" class="ruby">

&lt;% min = minmaxpage(@page, @lastpage, &#039;min&#039;) %&gt;

&lt;% max = minmaxpage(@page, @lastpage, &#039;max&#039;) %&gt;
</pre>
<p>For the code for the page links, we simply check to see if the min page is within two of the current:</p>
<pre name="code" class="ruby">

&lt;% if min &lt; @page %&gt;

    &lt;%= link_to( &quot;Prev&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; (@page - 1) ) %&gt;

&lt;% end %&gt;

&lt;% if min &gt; 1 %&gt;

    &lt;%= link_to( &quot;1&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; 1 ) %&gt;

&lt;% end %&gt;

&lt;% x = min %&gt;
</pre>
<p>Then we loop through the remaining pages, ensuring we don&#8217;t go over the max, and make the current page simple text instead of a link:</p>
<pre name="code" class="ruby">

&lt;% while x &lt;= max %&gt;

    &lt;% if x.to_i == @page.to_i %&gt;

        &lt;span class=&quot;current&quot;&gt;&lt;%= x %&gt;&lt;/span&gt;

    &lt;% else %&gt;

        &lt;%= link_to( x.to_s,

            :action =&gt; &quot;search&quot;,

            :page =&gt; x ) %&gt;

    &lt;% end %&gt;

    &lt;% x = x+1 %&gt;

&lt;% end %&gt;

&lt;% if max &lt; @lastpage %&gt;

    &lt;%= link_to( @lastpage.to_s,

        :action =&gt; &quot;search&quot;,

        :page =&gt; @lastpage ) %&gt;

&lt;% end %&gt;

&lt;% if max &gt; @page %&gt;

    &lt;%= link_to( &quot;Next&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; (@page + 1) ) %&gt;

&lt;% end %&gt;
</pre>
<p>The code may look a little long, but the implementation took very little time.  I know exactly what the code is doing and it is very easy to expand this for different situations and code.</p>
<p>There are a lot of solutions for pagination of content, but I found that this simple implementation helped me avoid using plugins and save time.  Let me know if you guys have any questions or comparable solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
