fnando / has_tags
- Source
- Commits
- Network (0)
- Issues (1)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
has_tags /
README
has_tags
========
Installing
----------
1) Install the plugin with `script/plugin install git://github.com/fnando/has_tags.git`
2) Generate a migration with `script/generate migration add_tagging` and add the following code:
class AddTagging < ActiveRecord::Migration
def self.up
create_table :taggings do |t|
t.references :tag, :user
t.references :scopable, :taggable, :polymorphic => true
end
create_table :tags do |t|
t.string :name
end
add_index :tags, :name
add_index :taggings, [:taggable_type, :taggable_id]
add_index :taggings, [:scopable_type, :scopable_id]
end
def self.down
drop_table :tags
drop_table :taggings
end
end
3) Run the migrations with `rake db:migrate`
Usage
-----
1) Add the method call `has_tags` to your model.
class Link < ActiveRecord::Base
has_tags :scope => :category
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :links
end
@post = Post.first
@category = Category.first
# set tags using the chosen tag separator
@post.tagged_with = "rails, activerecord, models"
# get tags joined with the chosen tag separator
@post.tagged_with
# get all tags as objects
@post.tags
# check if an user tagged this object
@post.tag_owner?(current_user)
# generate a tag cloud
@tags = Tag.cloud(:link, :limit => 100, :scope => @category)
# at the view, call the helper tag_cloud
tag_cloud @tags do |tag_name, css|
<%= link_to tag_name, tag_path(tag_name), :class => css
end
Copyright (c) 2008 Nando Vieira, released under the MIT license
