af83

Playing with acts as taggable on

There are several Ruby on Rails plugins for tagging. The first one was acts_as_taggable by DHH. It was simple and limited in functionality, so it was forked several times. The most popular version of these forks is acts_as_taggable_on_steroids, which is pretty performant, has tag cloud calculations and offers extras such as tests.

However, it's not possible to have several sets of tags on the same object. For example, it can be useful to separate skills and interests for users. acts_as_taggable_on implements this lacking functionality.

Cool, but how do we use it ?

First, install it like any rails plugin :

script/plugin install http://svn.intridea.com/svn/public/acts_as_taggable_on/

Then, generate the migration for creating the new SQL tables for the tags (and play this migration) :

script/generate acts_as_taggable_on_migration
rake db:migrate

The last step of the installation is to declare the User class as taggable :

class User <  ActiveRecord::Base
  acts_as_taggable_on :skills, :interest
  # ...
end

Done ? OK, let's test it in script/console:

>> joe = User.new(:login => 'Joe')
# => #<User id: nil, login: "joe">

>> joe.skill_list
# => []
>> joe.skill_list = "ruby, rails, optimization"
# => "ruby, rails, optimization"
>> joe.skill_list
# => ["ruby", "rails", "optimization"]

>> joe.interest_list = "procrastinate, humour"
# => "procrastinate, humour"
>> joe.interest_list
# => ["procrastinate", "humour"]

>> joe.save
# => true

>> User.find_tagged_with("rails")
# => [#<User id: 1, login: "joe">]
>> User.find_tagged_with("rails", :on => :interests)
# => []

With these methods, you should be able to have tags on many models on your app, with several sets if you want. The next big step is tag clouds, because of the lacking documentation. I think an example is welcomed.

So, the first thing is finding tags with their counts :

# app/controllers/clouds_controller.rb
class CloudsController <  ApplicationController
  def skills
    @tags = User.skill_counts
    @levels = (1 .. 5).map { |i| "level-#{i}" }
  end
end

Then, we can show them with the tag_cloud helper:

# app/views/clouds/skills.html.erb
<% if @tags.empty? -%>
  <p>No tags :/</p>
<% else -%>
  <ul id="tag-cloud">
    <% tag_cloud(@tags, @levels) do |tag,level| -%>
      <li class="<%= level %>"><%=h tag %></span>
    <% end -%>
  </ul>

<% end -%>

Go on http://my-web-site/clouds/skills and have a glance on the tag clouds.

The last caveat is tag clouds with tags from all the models and sets. For this, we need to dive into acts_as_taggable_on and play with ActiveRecord:

class CloudsController <  ApplicationController
  def index
    @tags = Tag.find(:all,
      :select => "#{Tag.table_name}.id, #{Tag.table_name}.name, COUNT(*) AS count",
      :joins  => "LEFT OUTER JOIN #{Tagging.table_name} ON #{Tag.table_name}.id = #{Tagging.table_name}.tag_id",
      :group  => "#{Tag.table_name}.id, #{Tag.table_name}.name HAVING COUNT(*) > 0",
      :order  => "count DESC",
      :limit  => 30
    ).sort_by(&:name)
    @levels = (1 .. 5).map { |i| "level-#{i}" }
  end
end

The app/views/clouds/all.html.erb view is the same as skills.html.erb. Enjoy it on http://my-web-site/clouds/ :-)

blog comments powered by Disqus