开发者

A notification system to track user activity on Rails

开发者 https://www.devze.com 2023-04-05 21:02 出处:网络
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. (li

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.


I would recommend to mimic the notifications pattern used in Cocoa. Basically, you could pass an user info dictionary to your notifications so that you can put the data you want.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号