It was late last night. And the night before, I admit it. I also admit that I don’t know much about what Web 2.0 is all about, but there are two things that I do know: rounded corners are involved and, of course tags and tag clouds.
Tagging is work, therefore ipso facto to be avoided, better still: automated.
Let’s say you want to show people which part of your site are popular. How about a tag cloud of the most requested locations? Well, the first thing to do would be to collect which requests are happening at all. Also, you’d need to attach a sensible, displayable title to the raw request. For the sake of this little demonstration, I’m going to assume that in your template — list.rhtml
, edit.rhtml
, and brethren — you have an instance variable @title
. It could be either defined in the template itself or transplanted there from its controller, it doesn’t matter.
Given all this, you could hypothetically write code like this.
class ApplicationController < ActionController::Base collect_popular_requests :max_age => 7.days, :max_count => 10000, :title => proc { |controller| controller.response.template.instance_variable_get(:@title) }
Therein further assuming that at most the 10000 most recent requests are kept if they are no older than 7 days. Now that we have hypothetically collected the data to base our tag cloud on, we have to display it somehow. Again, let's assume it could be done with a route like this
map.popular '/', :controller => 'most_popular', :action => 'index'
a controller like that
class MostPopularController < ApplicationController helper BoilerPlate::PopularRequestsHelper collect_popular_requests :\off # Ignore this place def index @popular_requests = popular_requests_list.most_popular(30) end end
and for good measure a view (index.rhtml
)
<ul class="tagcloud"> <% alpha_reqs = @popular_requests.sort_by(&:title) -%> <% alpha_reqs.each do |req| -%> <%= popular_request_tag(req, :wrap => 'li', :min_size => 30, :max_size => 400) %> <% end -%> </ul>
and some CSS for good style
.tagcloud { text-align: center; width: 70%; } .tagcloud li { display: inline-block; display: -moz-inline-box; white-space: nowrap; vertical-align: middle; line-height: 1.2em; padding: 0 0.2em; }
Well, if you've made it this far, you might even consider doing all of this for real. For a good start, I suggest you download this Rails plugin, install it, create a database table with
$ script/generate popular_requests AddPopularRequests $ rake db:migrate
and start playing.