{"id":129,"date":"2006-07-05T08:20:18","date_gmt":"2006-07-05T13:20:18","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/05\/acts-as-bookmarkable-plugin\/"},"modified":"2006-07-05T08:20:18","modified_gmt":"2006-07-05T13:20:18","slug":"acts-as-bookmarkable-plugin","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/05\/acts-as-bookmarkable-plugin\/","title":{"rendered":"Acts As Bookmarkable Plugin"},"content":{"rendered":"<p>This is my latest installment on Ruby on Rails plugins.  Here is a <b>rails plugin<\/b> 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 <b>taggable<\/b> 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.<\/p>\n<p>To get started download the plugin from its repository:<\/p>\n<pre>\nscript\/plugin install http:\/\/juixe.com\/svn\/acts_as_bookmarkable\n<\/pre>\n<p>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:<\/p>\n<pre>\ndef self.up\n  create_table :bookmarks, :force =&gt; true do |t|\n    t.column :title, :string, :limit =&gt; 50, :default =&gt; \"\"\n    t.column :created_at, :datetime, :null =&gt; false\n    t.column :bookmarkable_type, :string,\n      :limit =&gt; 15, :default =&gt; \"\", :null =&gt; false\n    t.column :bookmarkable_id, :integer, :default =&gt; 0, :null =&gt; false\n    t.column :user_id, :integer, :default =&gt; 0, :null =&gt; false\n  end\n\n  add_index :bookmarks, [\"user_id\"], :name =&gt; \"fk_bookmarks_user\"\nend\n\ndef self.down\n  drop_table :bookmarks\nend\n<\/pre>\n<p>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.<\/p>\n<pre>\nclass Post &lt; ActiveRecord::Base\n  acts_as_bookmarkable\nend\n<\/pre>\n<p>In your controller you can bookmark a post with just a few lines of code:<\/p>\n<pre>\npost = Post.find(params[:id])\nbmark = Bookmark.new(:title =&gt; someTitle)\npost.bookmarks &lt;&lt; bmark\n<\/pre>\n<p>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.<\/p>\n<pre>\nbmark = Bookmark.new(:title =&gt; someTitle, :user_id =&gt; session[:user].id)\n<\/pre>\n<p>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:<\/p>\n<pre>\nbmarks = Post.find_bookmarks_by_user(session[:user])\n<\/pre>\n<p>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.<\/p>\n<pre>\nbmarks = Bookmark.find_bookmarks_by_user(session[:user])\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails\" rel=\"tag\">  rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\">  ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/plugin\" rel=\"tag\">  plugin<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails+plugin\" rel=\"tag\">  rails plugin<\/a>, <a href=\"http:\/\/technorati.com\/tag\/activerecord.+acts_as_bookmarkable\" rel=\"tag\"> activerecord. acts_as_bookmarkable<\/a>, <a href=\"http:\/\/technorati.com\/tag\/bookmarks\" rel=\"tag\"> bookmarks<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-25","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/129"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}