Announcing 'acts_as_taggable_on'

by Michael Bleigh on December 3, 2007 • filed under rails Rails plugins announcements tagging acts_as_taggable_oncomment

For a number of applications, especially our Social Networking Platform I’ve found a need for advanced tagging functionality not offered by the acts_as_taggable_on_steroids plugin. Namely, there have been a number of times when I’ve wished a model could have multiple “sets” of tags that would function both independently and together. For example, a user might have tagged themselves, but they might also have skills, interests or sports that would also function like tags. That’s where acts_as_taggable_on comes in.

Installation

To install the plugin on Edge Rails or Rails 2.1 and greater:

script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git

On previous versions of Rails:

git clone git://github.com/mbleigh/acts-as-taggable-on.git vendor/plugins/acts-as-taggable-on

Usage

Acts As Taggable On provides the same functionality of acts_as_taggable_on_steroids with the addition of the notion of “contexts,” or scoped areas in which taggings can live. For the user example listed above, I can now simply make a call like so:

class User < ActiveRecord::Base
  acts_as_taggable_on :tags, :skills, :interests, :sports
end

By taking the same one-liner tack as previous implementations, we are now able to set, find, retrieve, and calculate information based on tags within a context as well as as a whole. With the user model we just created:

@user = User.new(:name => "Bobby")
@user.tag_list = "awesome, slick, hefty"      # this should be familiar
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
@user.skill_list # => ["joking","clowning","boxing"] as TagList
@user.save

@user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]

User.find_tagged_with("awesome") # => [@user]
User.find_tagged_with("joking") # => [@user]
User.find_tagged_with("awesome", :on => :tags) # => [@user]
User.find_tagged_with("awesome", :on => :skills) # => []

@frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]

@bobby.skill_counts
@frankie.skill_counts

And that’s all there is to it! Now you can scope your tags to whatever arbitrary name you would like. The inflector is used to create the singular/plural methods, so be careful about making sure that your nomenclature uses plurals in the acts_as_taggable_on call.

As a side note, this is meant to be used in place of acts_as_taggable_on_steroids, and the acts_as_taggable method works in acts_as_taggable_on simply by calling acts_as_taggable_on :tags. Caching has been written into the implementation but is not yet tested or verified to be working. Stay tuned for additional improvements to this plugin, and feel free to submit any bugs or patches to the Lighthouse

UPDATE 5/3/07: Acts As Taggable on is now hosted on GitHub and has a public Lighthouse available for bug tracking. Information updated above.

Comment | 
blog comments powered by Disqus

Words we've written view all blog posts »

Featured Article

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Ping came on board with Intridea during our infancy. He was employee #4, and has been part of the vision and culture of our company from the beginning. He has helped to grow Intridea from a group of four guys working at their dining room tables, to a company of 40+ Ruby and Rails developers and designers that span continents and multiple time zones. His work as Director of Asian Operations aims to bridge our Asian team of Rubyists with our centralized team in the states. "I believe in helping to extend Intridea into China. There is so much talent in China, it feels like a natural thing to do. Intridea has a culture of quality, and I want to bring that to these developers. They love Ruby, and they have so much energy."

Continue reading »

Recent Blog Posts

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Talkin' About Upgrading to Rails 3

by Michael Bleigh on July 26, 2010

A To-Go Plate of Ruby Midwest

by Michael Bleigh on July 19, 2010