Acts As Bookmarkable Plugin

This is my latest installment on Ruby on Rails plugins. Here is a rails plugin which can help your user manage bookmarks of model records. A bookmarkable model can be any ActiveRecord class so you can use the acts_as_bookmarkable to bookmark other users in a friends list, or bookmark posts in a favorites list, etc. And following the Web 2.0 trends, bookmarks themselves can act as taggable so that you can label bookmarks with tags. You need to install the acts_as_taggable plugin separately, or you can remove one line in the bookmark.rb file to disable tags for bookmarks.

To get started download the plugin from its repository:

script/plugin install http://juixe.com/svn/acts_as_bookmarkable

The installation process will add several ruby files in the vendor/plugins directory. Create a new rails migration and cut and past the following self.up and self.down methods:

You have two options to retrieve the bookmarks for a given user. You can look up all the bookmarks for the given user that are of a certain bookmarkable type. Lets say I want to find all posts that have been bookmarked by the logged in user. To accomplish this I can do the following:

bmarks = Post.find_bookmarks_by_user(session[:user])

If you want all bookmarks for a given user, regardless of the type, then you can use the find_bookmarks_by_user method available from the Bookmark class.

Love it? Share it!

This entry was posted on Wednesday, July 5th, 2006 at 8:20 amand is filed under . You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

@Frederico – Thanks for you comment. I had removed the add_bookmark method sometime ago and must had forgotten to remove it from here.

In regards to your second comment, I think I could add such a method as a convenience, but I don’t want to further confuse users with the existing find_bookmarks_by_user(user). You want an array of the bookmarked model such as Product or Post, and find_bookmarks_by_user returns an array of bookmarks. Did you know that a bookmark has a reference to your model? Yeah, you could say something like the following…