Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Ruby
branch: master

This branch is 11 commits ahead, 15 commits behind jamesgolick:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
generators/is_taggable
lib
test add TagList subclass of array for displaying the tag list
README.rdoc
init.rb

README.rdoc

Is Taggable

Rails plugin for working easily with tagging stuff in your application.

Stores tags in separate table and implements a polymorphic interface taggable for attaching tags to ActiveRecord objects.

Comes with a generator for database migrations and autocompleting tags in forms (with the --with-autocompleter option)

See examples below.

Usage

Install the plugin by cloning the repository and running

script/generate is_taggable .

To generate migration tables. The dot is necessary.

Example

In your Article model:

class Article < ActiveRecord::Base
  is_taggable :tags
end

Then:

>> Tag.all
  Tag Load (0.3ms)   SELECT * FROM `tags` 
=> [#<Tag id: 1, name: "one", kind: "tag", ...>, #<Tag id: 2, name: "two", kind: "tag", ...>]

>> a = Article.first
  Article Load (0.4ms)   SELECT * FROM `articles` LIMIT 1
=> #<Article id: 1, title: ....>

>> a.tag_list
  Article Load (0.4ms)   SELECT * FROM `articles` LIMIT 1
  Tag Load (0.5ms)   SELECT `tags`.* FROM `tags` INNER JOIN taggings ON tags.id = taggings.tag_id WHERE ((`taggings`.taggable_type = 'Article') AND (`taggings`.taggable_id = 1)) AND (`tags`.`kind` = 'tag') 
=> ["one"]

>> a.tag_list = ['one', 'two']
=> ["one", "two"]

>> a.save
  ...
  Tagging Create (0.3ms)   INSERT INTO `taggings` (`updated_at`, `tag_id`, `taggable_type`, `taggable_id`, `created_at`) VALUES('2008-12-18 16:51:50', 2, 'Article', 1, '2008-12-18 16:51:50')
  ...
=> true

>> Article.find_all_tagged_with 'one'
  Article Load (1.9ms)   SELECT * FROM `articles` 
  Tagging Load (66.6ms)   SELECT `taggings`.* FROM `taggings` WHERE (`taggings`.`taggable_id` IN (1,2,3) and `taggings`.`taggable_type` = 'Article') 
  Tag Load (0.4ms)   SELECT * FROM `tags` WHERE (`tags`.`id` IN (1,2)) 
=> [#<Article id: 1, title: "Lorem...", ...>]

>> Article.find_all_tagged_with ['one', 'two']
  Article Load (2.1ms)   SELECT * FROM `articles` 
  Tagging Load (2.9ms)   SELECT `taggings`.* FROM `taggings` WHERE (`taggings`.`taggable_id` IN (1,2,3) and `taggings`.`taggable_type` = 'Article') 
  Tag Load (0.3ms)   SELECT * FROM `tags` WHERE (`tags`.`id` IN (1,2)) 
=> [#<Article id: 1, title: "Lorem...", ...>]

>> Article.first.find_tagged_alike
  SQL (0.5ms)   SELECT count(*) AS count_all FROM `tags` INNER JOIN taggings ON tags.id = taggings.tag_id WHERE ((`taggings`.taggable_type = 'Article') AND (`taggings`.taggable_id = 1)) 
  Article Load (1.5ms)   SELECT * FROM `articles` WHERE (id != '1') 
  Tagging Load (2.9ms)   SELECT `taggings`.* FROM `taggings` WHERE (`taggings`.`taggable_id` IN (1,2,3) and `taggings`.`taggable_type` = 'Article') 
  Tag Load (0.3ms)   SELECT * FROM `tags` WHERE (`tags`.`id` IN (1,2,3)) 
  Tag Load (0.4ms)   SELECT `tags`.* FROM `tags` INNER JOIN taggings ON tags.id = taggings.tag_id WHERE ((`taggings`.taggable_type = 'Article') AND (`taggings`.taggable_id = 1)) AND (`tags`.`kind` = 'tag') 
=> [#<Article id: 201, title: "Another...", ...>]

When you run generator with the --with-autocompleter option, also a controller and view are generated. You can use it like this then:

<% form_for(@my_object) do |f| %>
  <%= f.text_area :tag_list %>
  <%= taggable_autocompleter_for('my_object_tag_list') %>
<% end %>

Tags matching input will be then added to your input/textarea. Assumes you separate tags with commas (,).

Copyright © 2008 James Golick & Karel Minarik, released under the MIT license

Something went wrong with that request. Please try again.