github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

mbleigh / acts-as-taggable-on

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 795
    • 106
  • Source
  • Commits
  • Network (106)
  • Issues (11)
  • Downloads (10)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (10)
    • v1.0.19
    • v1.0.18
    • v1.0.17
    • v1.0.16
    • v1.0.15
    • v1.0.12
    • v1.0.11
    • v1.0.9
    • v1.0.7
    • v1.0.6
Sending Request…
Click here to lend your support to: acts-as-taggable-on and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts. — Read more

  cancel

http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Bumped version 
Tom-Eric (author)
Sun Feb 07 03:42:34 -0800 2010
commit  beee771a0758ea11ae9e67cf3c6199b3f503c653
tree    3ebe0a334e1cd07f18184ec19a66a492fc7185d8
parent  e683717477d0deae1d814d0ce258c33327c86e8a
acts-as-taggable-on /
name age
history
message
file .gitignore Fri Oct 30 11:47:17 -0700 2009 Updating README to reflect move to Gemcutter. [mbleigh]
file CHANGELOG Wed Dec 02 05:17:53 -0800 2009 Update changelog for the first time in a long t... [mbleigh]
file MIT-LICENSE Mon Nov 26 20:05:45 -0800 2007 Initial import of acts_as_taggable_on. git-s... [michael]
file README.rdoc Mon Dec 14 02:48:41 -0800 2009 My typo issue from last commit [sobrinho]
file Rakefile Thu Dec 17 05:40:49 -0800 2009 Updated gemspec to include generators [Tom-Eric]
file VERSION Sun Feb 07 03:42:15 -0800 2010 Version bump to 1.1.3 [Tom-Eric]
file acts-as-taggable-on.gemspec Sun Feb 07 03:42:34 -0800 2010 Bumped version [Tom-Eric]
directory generators/ Mon Jun 23 18:07:16 -0700 2008 Add post-installation instructions; move add_co... [jetrecord]
file init.rb Mon Jun 09 15:04:06 -0700 2008 Gemifying acts-as-taggable-on, including update... [mbleigh]
directory lib/ Sun Feb 07 03:41:50 -0800 2010 Small refactoring [Tom-Eric]
directory rails/ Fri Dec 18 02:42:00 -0800 2009 Removed ** acts_as_taggable_on: initialized pro... [Tom-Eric]
directory spec/ Sat Feb 06 04:24:33 -0800 2010 Fixed issue with Tag.find_or_create_all_with_li... [Tom-Eric]
file uninstall.rb Mon Nov 26 20:05:45 -0800 2007 Initial import of acts_as_taggable_on. git-s... [michael]
README.rdoc

ActsAsTaggableOn

This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney. It has evolved substantially since that point, but all credit goes to him for the initial tagging functionality that so many people have used.

For instance, in a social network, a user might have tags that are called skills, interests, sports, and more. There is no real way to differentiate between tags and so an implementation of this type is not possible with acts as taggable on steroids.

Enter Acts as Taggable On. Rather than tying functionality to a specific keyword (namely "tags"), acts as taggable on allows you to specify an arbitrary number of tag "contexts" that can be used locally or in combination in the same way steroids was used.

Installation

Plugin

Acts As Taggable On is available both as a gem and as a traditional plugin. For the traditional plugin you can install like so (Rails 2.1 or later):

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

GemPlugin

Acts As Taggable On is also available as a gem plugin using Rails 2.1’s gem dependencies. To install the gem, add this to your config/environment.rb:

  config.gem "acts-as-taggable-on", :source => "http://gemcutter.org"

After that, you can run "rake gems:install" to install the gem if you don’t already have it.

Post Installation (Rails)

  1. script/generate acts_as_taggable_on_migration
  2. rake db:migrate

Testing

Acts As Taggable On uses RSpec for its test coverage. Inside the plugin directory, you can run the specs with:

rake spec

If you already have RSpec on your application, the specs will run while using:

rake spec:plugins

Usage

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

    @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">]

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

    # The better way (utilizes named_scope)
    User.tagged_with("awesome", :on => :tags) # => [@user]
    User.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>...]
    @frankie.skill_counts

Finding Tagged Objects

Acts As Taggable On utilizes Rails 2.1’s named_scope to create an association for tags. This way you can mix and match to filter down your results, and it also improves compatibility with the will_paginate gem:

    class User < ActiveRecord::Base
      acts_as_taggable_on :tags
      named_scope :by_join_date, :order => "created_at DESC"
    end

    User.tagged_with("awesome").by_date
    User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)

Relationships

You can find objects of the same type based on similar tags on certain contexts. Also, objects will be returned in descending order based on the total number of matched tags.

    @bobby = User.find_by_name("Bobby")
    @bobby.skill_list # => ["jogging", "diving"]

    @frankie = User.find_by_name("Frankie")
    @frankie.skill_list # => ["hacking"]

    @tom = User.find_by_name("Tom")
    @tom.skill_list # => ["hacking", "jogging", "diving"]

    @tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
    @bobby.find_related_skills # => [<User name="Tom">]
    @frankie.find_related_skills # => [<User name="Tom">]

Dynamic Tag Contexts

In addition to the generated tag contexts in the definition, it is also possible to allow for dynamic tag contexts (this could be user generated tag contexts!)

    @user = User.new(:name => "Bobby")
    @user.set_tag_list_on(:customs, "same, as, tag, list")
    @user.tag_list_on(:customs) # => ["same","as","tag","list"]
    @user.save
    @user.tags_on(:customs) # => [<Tag name='same'>,...]
    @user.tag_counts_on(:customs)
    User.find_tagged_with("same", :on => :customs) # => [@user]

Tag Ownership

Tags can have owners:

    class User < ActiveRecord::Base
      acts_as_tagger
    end

    class Photo < ActiveRecord::Base
      acts_as_taggable_on :locations
    end

    @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
    @some_user.owned_taggings
    @some_user.owned_tags
    @some_photo.locations_from(@some_user)

Tag cloud calculations

To construct tag clouds, the frequency of each tag needs to be calculated. Because we specified acts_as_taggable_on on the User class, we can get a calculation of all the tag counts by using User.tag_counts_on(:customs). But what if we wanted a tag count for an single user’s posts? To achieve this we call tag_counts on the association:

  User.find(:first).posts.tag_counts_on(:tags)

A helper is included to assist with generating tag clouds.

Here is an example that generates a tag cloud.

Helper:

  module PostsHelper
    include TagsHelper
  end

Controller:

  class PostController < ApplicationController
    def tag_cloud
      @tags = Post.tag_counts_on(:tags)
    end
  end

View:

  <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
    <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
  <% end %>

CSS:

  .css1 { font-size: 1.0em; }
  .css2 { font-size: 1.2em; }
  .css3 { font-size: 1.4em; }
  .css4 { font-size: 1.6em; }

Contributors

  • TomEric (i76) - Maintainer
  • Michael Bleigh - Original Author
  • Brendan Lim - Related Objects
  • Pradeep Elankumaran - Taggers
  • Sinclair Bain - Patch King

Patch Contributors

  • tristanzdunn - Related objects of other classes
  • azabaj - Fixed migrate down
  • Peter Cooper - named_scope fix
  • slainer68 - STI fix
  • harrylove - migration instructions and fix-ups
  • lawrencepit - cached tag work
  • sobrinho - fixed tag_cloud helper

Copyright © 2007-2009 Michael Bleigh (mbleigh.com/) and Intridea Inc. (intridea.com/), released under the MIT license

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server