jwhiting
jwhiting

Reputation: 357

Rails notification system

I need to create a facebook-like notification system for an app I am working on. I was wondering what suggestions any of you have for how to go about doing this? If possible, I would like to avoid using the database for the notifications, if this is feasible I'd love to know how.

Thanks in advance

Upvotes: 2

Views: 1204

Answers (2)

Nick
Nick

Reputation: 2413

It wasn't clear in the question if notices needed to persist across sessions or user-to-user (who may not be online at the same time); however, I had a similar need on a Rails app and implemented it through a Notice ActiveRecord model. It's used for broadcasting downtime and other pending events coming up across every page on the site. Notices will show ahead and during their scheduled times.

class Notice < ActiveRecord::Base
  validates_presence_of  :header
  validates_presence_of  :body
  validates_presence_of  :severity
  validates_presence_of  :start_time
  validates_presence_of  :end_time
  validates_inclusion_of :severity, :in => %( low normal high )
end

Since it needed to be displayed everywhere, a helper method was added to ApplicationHelper for displaying it consistently:

module ApplicationHelper
  def show_notifications
    now = DateTime.now.utc
    noticeids = Notice.find(:all, :select=>"id", :conditions=>['start_time >= ? or (start_time <= ? and end_time >= ?)', now, now, now])
    return "" if noticeids.count == 0 # quick exit if there's nothing to display
    # ...skipping code..
    # basically find and return html to display each notice
  end
end

Lastly, in the application layout there's a tiny bit of code to use the ApplicationHelper#show_notifications method.

Upvotes: 2

Alok Swain
Alok Swain

Reputation: 6519

Rails has a default notification system - the flash. You could pass on your own messages around in the flash and then display them in any way you want on the front-end with some CSS and JS. Do google around for some plugins to see if they suit your requirements, though it should not be very hard to implement on on your own.

Upvotes: 0

Related Questions