How to create a tag cloud in Rails that uses colour to indicate the popularity of a tag. Uses compass and acts-as-taggable-on gems.
Tag Clouds
The purpose of the tag cloud is to highlight the most popular tags by using font size to represent tag usage. Whereas this is a useful tool for the user, if you care about good typography and keeping to the grid you probably won’t want to include one of these janky things in your sidebar.
What if there were another way to represent the popularity of each tag? Made By Many have taken an interesting approach in using colour to indicate tag popularity.
Here’s one way to create tinted tags..
Acts As Taggable On
acts-as-taggable-on is the obvious choice for adding tagging functionality to Rails.
First make your model taggable by following the instructions in the gem readme.
The gem creates two ActiveRecord models; Tag and Tagging, the latter is an association model.
We need to add a new attribute, :tint to the Tag model. This is where we will save a generated hex code that represents the popularity of the tag.
Depending on how many taggings you have, this is a potentially expensive operation. One option would be to use a task scheduler such as delayed_job or whenever to recalculate tag tints. I chose to use an after_save callback..
Comments