Skip to content
Add bookmark support to Rails apps with this plugin
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
MIT-LICENSE
README.markdown
init.rb

README.markdown

has_bookmarks

Instalation

1) Install the plugin with script/plugin install git://github.com/fnando/has_bookmarks.git

2) Generate a migration with script/generate migration create_bookmarks and add the following code:

class CreateBookmarks < ActiveRecord::Migration
  def self.up
    create_table :bookmarks do |t|
      t.references :bookmarkable, :polymorphic => true
      t.references :user
      t.string :name
      t.timestamps
    end

    add_index :bookmarks, :bookmarkable_id
    add_index :bookmarks, :bookmarkable_type
    add_index :bookmarks, :user_id
    add_index :bookmarks, :name
  end

  def self.down
    drop_table :bookmarks
  end
end

3) Run the migrations with rake db:migrate

Usage

1) Add the method call has_bookmarks to your model.

class Product < ActiveRecord::Base
  has_bookmarks
end

2) Add this association on your User model:

class User < ActiveRecord::Base
  has_many :bookmarks, :dependent => :destroy
end

product = Product.first
user = User.first

product.bookmark(:user => user) # => <bookmark>
product.bookmarks # => [<bookmark>]
product.bookmarked?(:user => user) # => true
product.find_users_that_bookmarked # => [<user>]
product.find_bookmark_by_user(:user => user) # => [<bookmark>]
product.remove_bookmark_for(:user => user)

If you have different types of bookmarks, you can use the option :name:

product.bookmark(:user => user, :name => 'wishlist')
product.bookmark(:user => user, :name => 'i_have')

product.bookmarks.by_name('wishlist')
product.bookmarked?(:user => user, :name => 'wishlist')
product.find_users_that_bookmarked(:name => 'wishlist')
product.find_bookmark_by_user(:user => user, :name => 'wishlist')
product.remove_bookmark_for(:user => user, :name => 'wishlist')

To retrieve bookmarks from an user:

user.bookmarks
user.bookmarks.by_name('wishlist')

If you have has_paginate installed, you can paginate the users that bookmarked a given item:

product.find_users_that_bookmarked(:page => 2)
user.bookmarks.by_name('wishlist').paginate(params[:page])

Optionally, you can have counter columns for general/named bookmarks. If your model doesn't have counter columns, nothing will happen.

product.bookmark(:user => user) # => update bookmarks_count
product.bookmark(:user => user, :name => 'wishlist') # => update wishlist_bookmarks_count

NOTE: You should have a User model. Otherwise, this won't work!

Copyright (c) 2008 Nando Vieira, released under the MIT license

Something went wrong with that request. Please try again.