<?xml version='1.0' encoding='utf-8' ?>
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'>
  <channel>
    <title>Wolfmans Howlings: Developing a social networking site part 3 - tag cloud</title>
    <link>http://blog.wolfman.com/articles/2007/6/23/developing-a-social-networking-site-part-3-tag-cloud</link>
    <description>A programmers Blog about Ruby, Rails and a few other issue</description>
    <language>en-us</language>
    <ttl>40</ttl>
    <item>
      <title>Developing a social networking site part 3 - tag cloud</title>
      <description>
        &lt;p&gt;This is a simple one.&lt;/p&gt;
        
        &lt;p&gt;I use the excellent &lt;code&gt;acts_as_taggable&lt;/code&gt; plugin, and I wanted to have a
        tag cloud like everyone does.&lt;/p&gt;
        
        &lt;p&gt;Of course I have different Models that can be tagged, and I want to
        keep everything DRY, so I created a helper and put the following in
        the application_helper.rb file&lt;/p&gt;
        
        &lt;pre&gt;  &lt;span class=&quot;comment&quot;&gt;# display a tag cloud for the given model&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;tag_cloud&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;camelcase&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;constantize&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;plural&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;capitalize&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;pluralize&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;plural&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;tag_counts&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;tags.name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;')&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;empty?&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;urlmeth&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;tagged_&lt;span class=&quot;expr&quot;&gt;#{model.to_s.pluralize}&lt;/span&gt;_path&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_sym&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;div class=&lt;span class=&quot;escape&quot;&gt;\&amp;quot;&lt;/span&gt;tagcloud&lt;span class=&quot;escape&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;h3&amp;gt;&lt;span class=&quot;expr&quot;&gt;#{title}&lt;/span&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;span style=&lt;span class=&quot;escape&quot;&gt;\&amp;quot;&lt;/span&gt;font-size:&lt;span class=&quot;expr&quot;&gt;#{calc_size(t.count)}&lt;/span&gt;%&lt;span class=&quot;escape&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;link_to&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;urlmeth&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;))&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;/span&amp;gt; &lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;lt;hr/&amp;gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;gen&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;This allows me to have multiple tag clouds for the different models,
        see &lt;a href=&quot;http://snowdogsr.us&quot;&gt;snowdogsr.us&lt;/a&gt; on the left bar at the bottom.&lt;/p&gt;
        
        &lt;p&gt;The font is bigger for the more popular tags, and you can click on any
        tag to get a listing of all matching articles.&lt;/p&gt;
        
        &lt;p&gt;I use RESTful routing so I added this to routes.rb&lt;/p&gt;
        
        &lt;pre&gt;&lt;code&gt;  map.resources :places, :collection =&amp;gt; { :tagged =&amp;gt; :get }
        &lt;/code&gt;&lt;/pre&gt;
        
        &lt;p&gt;which creates a named route &lt;code&gt;tagged_places_path&lt;/code&gt; which returns an
        index with the places tagged with the tag that was clicked.&lt;/p&gt;
        
        &lt;p&gt;The places_controller method looks like this...&lt;/p&gt;
        
        &lt;pre&gt;  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;tagged&lt;/span&gt;
            &lt;span class=&quot;attribute&quot;&gt;@places&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Place&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find_tagged_with&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;updated_at DESC&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
            &lt;span class=&quot;attribute&quot;&gt;@filter&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Tagged with &lt;span class=&quot;expr&quot;&gt;#{params[:tag]}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;respond_to&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:action&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;xml&lt;/span&gt;  &lt;span class=&quot;punct&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:xml&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@places&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_xml&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;&lt;a href=&quot;http://technorati.com/tag/tagcloud+rails&quot; rel=&quot;tag&quot;&gt;&lt;/a&gt;&lt;/p&gt;
      </description>
      <author>Jim Morris</author>
      <pubDate>Sat, 23 Jun 2007 14:02:49 -0700</pubDate>
      <link>http://blog.wolfman.com/articles/2007/6/23/developing-a-social-networking-site-part-3-tag-cloud</link>
      <guid isPermaLink='false'>urn:uuid:6c5b21f1-032f-40ca-a54e-ff7631f4a5d7</guid>
    </item>
  </channel>
</rss>
