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:
def self.up
create_table :bookmarks, :force => true do |t|
t.column :title, :string, :limit => 50, :default => ""
t.column :created_at, :datetime, :null => false
t.column :bookmarkable_type, :string,
:limit => 15, :default => "", :null => false
t.column :bookmarkable_id, :integer, :default => 0, :null => false
t.column :user_id, :integer, :default => 0, :null => false
end
add_index :bookmarks, ["user_id"], :name => "fk_bookmarks_user"
end
def self.down
drop_table :bookmarks
end
Once you have the acts_as_bookmarkable plugin installed you can make your ActiveRecord classes act as models that can be bookmarkable by calling the acts_as_bookmakble method.
class Post < ActiveRecord::Base acts_as_bookmarkable end
In your controller you can bookmark a post with just a few lines of code:
post = Post.find(params[:id]) bmark = Bookmark.new(:title => someTitle) post.bookmarks << bmark
And like my other plugins you can assign a bookmark to a user by using the user_id property. Here is an example of how to assign a bookmark to a user model.
bmark = Bookmark.new(:title => someTitle, :user_id => session[:user].id)
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.
bmarks = Bookmark.find_bookmarks_by_user(session[:user])
Technorati Tags: ruby, rails, ruby on rails, plugin, rails plugin, activerecord. acts_as_bookmarkable, bookmarks
Related posts:
7 Comments
“post.add_bookmark bmark” did not work,
I had to do:
“post.bookmarks << bmark”
I have an add to your plugin. the below example is for finding all the Products bookmarked by a given user_id
def self.find_bookmarked(user) bookmarks = Bookmark.find(:all, :conditions => ["bookmarkable_type = ? AND user_id = ?","Product", user.id]) products = Array.new for bookmark in bookmarks products << Product.find(bookmark.bookmarkable_id) end return products end@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…
TechKnow, cool,
thanks for the help.
I needed to do the following for Edge Rails in acts_as_bookmarkable.rb:
change:
has_many :bookmarks, :as => :bookmarkable, :dependent => true
to:
has_many :bookmarks, :as => :bookmarkable, :dependent => :destroy
I did this to get my console to work.
@Tom – Thanks for the comment. I have update the plugin accordingly.
This is really cool. Does the plug in work with Rails 2.0.x?