一个跟踪Rails用户活动的通知系统 [英] A notification system to track user activity on Rails

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

问题描述

我正在研究一个基于事件触发器生成通知的gem。例如,在CMS中,如果创建了文章,则会生成通知由用户y创建的文章x。 (如Github / Facebook通知)。



在我的创业板中,我有一个has_notifications方法,在模型中指定时,注册回调



问题当然是,当触发回调时,会调用gem中的相应操作,但此操作将要求执行操作的用户的用户标识生成通知。



我对 acts_as_audited paper_trail 的灵感,但都使用Thread.current变量来存储用户信息,当然我相信这是 hacky and not safe

在基本设计方面,am我做对了吗?我还有什么其他选择,从我的宝石中获取用户信息?我是否应该在各自的控制器中设置过滤器来创建通知,以防万一使用vanilla current_user方法?



任何想法将不胜感激。感谢。

更新



使用通知回调并在过滤器之前使用相应的控制器设置它们。

解决方案

宝石是应该使用我猜与通知回调模型最有可能有一些关系到用户,对吧?

然后,您可以提取用户的数据关系,即 record.user record.user_id 。也许你可以默认为 #user ,并提供和选项如 .has_notification,通过:account当用户关系



如果模型没有关系,那么开发人员将传递给用户对象并提供自定义方法。


I'm working on a gem that would generate notifications based on event triggers. For Example, in a CMS, if an article is created, a notification "Article x created by user y" would be generated. (like Github/Facebook notifications).

In my gem, I have a has_notifications method which when specified in a model, registers callbacks.

The problem, of course is, when the callback is triggered, a corresponding action within the gem is invoked, but this action would require the user id of the user who performed the action to generate the notification.

I've looked towards acts_as_audited and paper_trail for inspiration but both use the Thread.current variable to store the user information, which of course I believe is hacky and not safe.

In terms of basic design, am I doing it right ? What other options do I have, to capture user information from my gem? Should I instead, setup around filters in the respective controllers to create notifications, which in case would use the vanilla current_user method?

Any ideas would be appreciated. Thanks.

Update

I'm looking at using virtual attributes for models with notification call-backs and setting them in the appropriate controllers using before filters.

解决方案

If I have the right hunch about how your gem is supposed to be used I guess the model with the notification callback most probably has some relation to the to the user, right?

Then you could extract the user's data from the relation, i.e. record.user or record.user_id. Maybe you could default to #user and provide and option like .has_notification, through: "account" when the user relation is named differently.

If the model has no relation, then it's up that developer to hand down the user object and provide it accessible in a custom method.

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

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