Rails 通知系统 [英] Rails notification system

查看:51
本文介绍了Rails 通知系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我正在开发的应用程序创建一个类似 facebook 的通知系统.我想知道你们对如何去做这件事有什么建议?如果可能,我想避免将数据库用于通知,如果可行,我很想知道如何.

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.

提前致谢

推荐答案

问题中不清楚通知是否需要跨会话或用户到用户(可能不同时间在线)持续;但是,我对 Rails 应用程序有类似的需求,并通过 Notice ActiveRecord 模型实现了它.它用于广播站点上每个页面上出现的停机时间和其他未决事件.通知将在预定时间提前显示.

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

由于需要到处显示,所以在ApplicationHelper中添加了一个helper方法,用于一致显示:

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

最后,在应用程序布局中有一小段代码可以使用 ApplicationHelper#show_notifications 方法.

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

这篇关于Rails 通知系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆