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:
[html]
# app/views/clouds/skills.html.erb
<?php if @tags.empty? -?>
No tags :/
<?php else -?>
<?php= level ?>"><?php=h tag ?>
<?php end -?>
<?php tag_cloud(@tags, @levels) do |tag,level| -?>
<?php end -?>
[/html]
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/ :-)

Thanks for the great writeup on my plugin! I just wanted to drop a note to say that I have moved the project over to GitHub, which is where future updates will be coming in.
GitHub Project URL: http://github.com/mbleigh/acts-as-taggable-on
New Installation Instructions: script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git
Thanks for your reply. I will follow your updates on github.
Doesn't work with single table inheritance :(
You must include TagsHelper on ApplicationHelper if you want to use tag_cloud.
module ApplicationHelper include TagsHelper end
and I prefer this...
No tags :/
.level-1 { font-size: 1.0em; } .level-2 { font-size: 1.2em; } .level-3 { font-size: 1.4em; } .level-4 { font-size: 1.6em; } .level-5 { font-size: 1.8em; }
Hello,
Thanks for the tips for the tag_cloud. I noticed that when using your example the tag cloud is sorted by name, thus it takes away from the appearance of a tag cloud (with the most popular tags in the middle). The solution i found is to remove the ".sort_by(&:name)" and also ":order => "count DESC"," from your example and then it works as advertised. Thus it would result in:
@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", :limit => 30 )
Laisser une réponse