public
Description: This plugin was created to act as a proxy between different notification systems (Mail, Jabber, etc) based on the user's preferences.
Homepage: http://github.com/fnando/has_notifications
Clone URL: git://github.com/fnando/has_notifications.git
fnando (author)
Wed Nov 19 02:15:11 -0800 2008
commit  1b0a606b37616fd942535a1cd749c16f7f15edf7
tree    7357716a8916408d1a8a45f3302bf3328a7a0d0b
parent  678d637a13949ea26260b0eaaeeee7825e790af8
name age message
file MIT-LICENSE Wed Oct 22 18:21:31 -0700 2008 first commit [fnando]
file README.markdown Thu Oct 23 09:50:37 -0700 2008 FAITH! Everything was working as expected! Adde... [fnando]
file init.rb Wed Oct 22 18:21:31 -0700 2008 first commit [fnando]
directory lib/ Wed Nov 19 02:15:11 -0800 2008 Added options method, so you can skip the ugly @ [fnando]
directory spec/ Thu Oct 23 09:50:37 -0700 2008 FAITH! Everything was working as expected! Adde... [fnando]
README.markdown

has_notifications

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.

Instalation

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

2) 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

3) Run the migrations with rake db:migrate

Usage

1) Add association below to the User model:

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

2) Create user preferences for a specific notification

# create a new notification setting, specifying which
# senders will be associated
notification = @user.notifications.create({
    :name => 'friendship_request',
    :senders => %w(mail jabber)
})

# deliver some message
Notify.deliver({
    :name => 'friendship_request, 
    :user => @user, 
    :friendship => @friendship
})

3) 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_queue

# lib/senders/mail.rb
class Notify::Senders::Mail < Notify::Base
  def deliver
    Mailer.queue(:some_email, @options)
  end
end

# lib/senders/jabber.rb
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