This plugin was created to act as a proxy between different notification systems based on the user's preferences. IT DOESN'T IMPLEMENT ANY NOTIFICATION SYSTEM.
-
Install the plugin with
script/plugin install git://github.com/fnando/has_notifications.git
-
Generate a migration with
script/generate migration create_notifications
and add the following code:class CreateNotifications < ActiveRecord::Migration def self.up create_table :notifications do |t| t.references :user t.string :name t.text :senders end
add_index :notifications, :user_id add_index :notifications, :name
end
def self.down drop_table :notifications end end
-
Run the migrations with
rake db:migrate
-
Add association below to the User model:
class User < ActiveRecord::Base has_many :notifications, :dependent => :destroy end
-
Create user preferences for a specific notification
notification = @user.notifications.create({ :name => 'friendship_request', :senders => %w(mail jabber) })
Notify.deliver({ :name => 'friendship_request, :user => @user, :friendship => @friendship })
-
To implement a sender, you need to add a class to the module
Notify::Senders
. Check this example, that implements notification by mail using mail_queueclass Notify::Senders::Mail < Notify::Base def deliver Mailer.queue(:some_email, @options) end end
class Notify::Senders::Jabber < Notify::Base def deliver # using xmpp4r-simple set somewhere JABBER.deliver(@options[:user].jid, "You have new friendship request. Visit http://example.com/friends/pending" ) end end
A sender should have a deliver
method. The options hash is set as instance
attribute named @options
.
Copyright (c) 2008 Nando Vieira, released under the MIT license