Archive for October 8th, 2008

Acts As Taggable on Steroids Ruby On Rails Plugin With Paginate

Posted on the October 8th, 2008 under My Projects,programming,Ruby on Rails by

Acts As Taggable On Steroids is a great plugin for Rails that makes adding tags to your application quick and easy. Check it out on github here => http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master

I threw this post together with some notes that I took along the way when setting up the plugin for use.

Installation:

script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids

Setup

Setup your migration by running the below:

ruby script/generate acts_as_taggable_migration
rake db:migrate

Add the acts_as_taggable to your model, in my case its the Events model…

class Event < ActiveRecord::Base
# acts as taggable on roids
acts_as_taggable

Now I needed to add a way to allow tags to be added to Events, in my form for my Event I added the following:

<p>
  <%= f.label 'Tag List (Delimiter = ,)' %><br />
  <%= f.text_field :tag_list %>
</p>

To view the tags associated this a Event on the Show page, along with linking to the actual tag to search for other events tagged with the same item, I added the following:

<p><strong>This event was tagged with</strong>: <% for tag in @event.tags %> 
<%= link_to tag.name, events_path(:view =>'tag', :tag => tag.name) %><% end %>
</p>

Tag Clouds

In order to use the plugin’s build in tag cloud functionality you need to add the helper to your application helper by doing the following:

  module ApplicationHelper
    include TagsHelper
  end

In your controller where you are planning on using your tag cloud add the below, this will grab the counts of all your tags so the helper can generate your cloud. Again in my case I am putting this in my Events controller.

@tags = Event.tag_counts

Now you use the following where you wish to show your views:

<% tag_cloud @tags, %w(tag1 tag2 tag3 tag4) do |tag, css_class| %>
    <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
  <% end %>

In your main CSS file you should add something to match the tags we specified above, this sets your font sizes for the cloud display generated by the helper…

  .tag1 { font-size: 1.0em; }
  .tag2 { font-size: 1.6em; }
  .tag3 { font-size: 2.7em; }
  .tag4 { font-size: 3.8em; }

Tag Clouds with Paginate Example

Here I am passing the page and tag to my events model to find all events tagged with that tag, and then use the results of that query and passing it onto paginate.

def self.tag_event_list(page, tag)
 
 options = Event.find_options_for_find_tagged_with(tag).merge :page => page, :per_page => 10, :order => 'date DESC' 
 paginate(options)
 
end

Other Notes:

I added the following to my environment.rb file so that the unused tags get destroyed if they are no longer in use by any events.

# automatically remove dead tags
Tag.destroy_unused = true

For more help check out the read me here: http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master