<?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>McKinney Station &#187; tagging</title>
	<atom:link href="http://www.mckinneystation.com/categories/tagging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mckinneystation.com</link>
	<description>Ruby on Rails web application development for Dallas/Fort Worth and all of North Texas.</description>
	<lastBuildDate>Wed, 02 Sep 2009 14:29:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pagination with acts_as_taggable_on_steroids, acts_as_ferret, and will_paginate</title>
		<link>http://www.mckinneystation.com/2007/08/20/pagination-with-acts_as_taggable_on_steroids-acts_as_ferret-and-will_paginate/</link>
		<comments>http://www.mckinneystation.com/2007/08/20/pagination-with-acts_as_taggable_on_steroids-acts_as_ferret-and-will_paginate/#comments</comments>
		<pubDate>Mon, 20 Aug 2007 20:40:18 +0000</pubDate>
		<dc:creator>Geoffrey</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ferret]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[tagging]]></category>

		<guid isPermaLink="false">http://www.mckinneystation.com/2007/08/20/pagination-with-acts_as_taggable_on_steroids-acts_as_ferret-and-will_paginate/</guid>
		<description><![CDATA[UPDATE (6/6/08):  This is out of date for the latest Rails and will_paginate.
So I needed to paginate large collection of data in a new app I am working on.  will_paginate is a good drop in replacement for the the default rails paginator.  But I am also using acts_as_taggable_on_steroids (for tagging) and acts_as_ferret [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATE (6/6/08):  This is out of date for the latest Rails and will_paginate.</p>
<p>So I needed to paginate large collection of data in a new app I am working on.  <a href="http://errtheblog.com/post/4791">will_paginate</a> is a good drop in replacement for the the default rails paginator.  But I am also using <a href="http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids">acts_as_taggable_on_steroids</a> (for tagging) and <a href="http://projects.jkraemer.net/acts_as_ferret/wiki">acts_as_ferret</a> (for searching), so i needed special pagination for those scenarios.  </p>
<h2>acts_as_ferret</h2>
<p>A quick Google search led me to <a href="http://opensoul.org/2007/8/17/acts_as_ferret-will_paginate">this for paginating acts_as_ferret search results</a>.  I modified the offset calculation and ended up with this:</p>
<pre>
<code>module ActsAsFerret
  module ClassMethods
    def paginate_search(query, options = {})
      options, page, per_page = wp_parse_options!(options)
      offset = (page.to_i - 1) * per_page
      options.merge!(:offset =&gt; offset, :limit =&gt; per_page)
      result = result = find_by_contents(query, options)
      returning WillPaginate::Collection.new(page, per_page, result.total_hits) do |pager|
        pager.replace result
      end
    end
  end
end</code>
</pre>
<p>Drop that in a file lib/ferret_pagination.rb, require it in you environment.rb, and you can now do this in your controller:</p>
<pre>
<code>@entries = Entry.paginate_search params[:query],
                                       :page =&gt; params[:page],
                                       :per_page =&gt; 20</code>
</pre>
<h2>acts_as_taggable (on steroids)</h2>
<p>So with that out of the way, I was now ready to tackle paginating entries tagged with a certain tag.  Another quick google search turned up <a href="http://errtheblog.com/post/4791">some ideas in the will_paginate comments</a>.  I used <a href="http://blog.wolfman.com/articles/2007/07/30/paginating-acts_as_taggable-with-will_paginate">this one</a> as a starting point and this is what I ended up with:</p>
<pre>
<code>module ActiveRecord
  module Acts #:nodoc:
    module Taggable #:nodoc:
      module SingletonMethods
        # Return the number of time this class has been tagged with this tag
        def tagging_counts(tag)
          count_by_sql("select count(*) FROM tags, taggings WHERE " + sanitize_sql(['tags.name = ? AND tags.id = taggings.tag_id AND taggings.taggable_type = ?', tag, name]))
        end

        # paginate a call to find_tagged_with
        # tag is the tag to find
        # options is the option to use for pagination (:page, :per_page) and for find_tagged_with
        def paginate_by_tag(tag, options = {})
          options, page, per_page = wp_parse_options!(options)
          offset = (page.to_i - 1) * per_page
          options.merge!(:offset =&gt; offset, :limit =&gt; per_page.to_i)
          items = find_tagged_with(tag, options)
          count = tagging_counts(tag)
          returning WillPaginate::Collection.new(page, per_page, count) do |p|
            p.replace items
          end
        end
      end
    end
  end
end</code>
</pre>
<p>Again, drop that in a file lib/taggable_pagination.rb, require it in you environment.rb, and you can now do this in your controller:</p>
<pre>
<code>@entries = Entry.paginate_by_tag @tag.name,
                                     :order =&gt; 'entries.created_at DESC',
                                     :page =&gt; params[:page],
                                     :per_page =&gt; 20</code>
</pre>
<h2>Thanks</h2>
<p>Thanks to <a href="http://opensoul.org/">Brandon</a> for posting the ferret pagination code, <a href="http://blog.wolfman.com/">Jim</a> for the acts as taggable pagination code, and <a href="http://errtheblog.com/">PJ</a> for the will_paginate code.</p>
<p><strong>UPDATED:</strong> Corrected problem noted in comments</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mckinneystation.com/2007/08/20/pagination-with-acts_as_taggable_on_steroids-acts_as_ferret-and-will_paginate/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>
