views:

68

answers:

1

I want to create an event-based notification and badging system that would award users when they accomplish certain goals. Goals might include:

  • Posting 20 entries on a forum -> alert on homepage highlighting user, award of badge
  • Logging in to the site 10 days straight -> congrats to message to user on homepage
  • Commenting on 10 forum posts -> forum badge award

Would this be a job for a state machine such as AASM? I haven't played much with such systems. Would that be the way to go to define events or are there better plugins/solutions that would accommodate this type of behavior? Any tutorials or recommendations for approach would be greatly appreciated.

+1  A: 

It seems that the tricky part here is figuring out how you want to maintain the state needed to determine when a badge should be awarded. The state machine plugins for rails aren't going to help you much with that.

For example, how would a piece of code determine if a user has logged in 10 days straight? Once you figure this out then you can worry about how to organize the event handling.

Do you need to notify them in real time when they get a badge? If so they then you can easily do this with ActiveRecord::Observer but it may have some latency issues in thelong run (http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)

If you don't need to do it in real time then you can do it with background jobs of some form. There are a lot of libraries for background jobs out there. I use workling when I don't care about scaling and just want to get something up fast.

jshen
Hi Jshen, I was just thinking about that problem - how to maintain state? One solution might be to group some of these events together in a table. Forum related events for instance such as a) reach 20 posts, or b) post 10 comments would simply be counter columns perhaps with observers so when the count reaches a certain threshold, a message is sent. I have to imagine that there's an event gem or plugin though...
aressidi
did you look at the observer class I linked? It is essentially an event handler for typical model actions. For example, you'd create an observer that "listens" for post.save() and in that listener you'd check if they've reached 20 and react accordingly. Rails isn't event driven in the sense that there isn't an event loop that detects events and dispatches handlers. The background processors can be even driven so you can look those up which I suggested in my answer.
jshen
Yes Jshen, I checked out the link. Thanks for the lead, I think the observers will be perfect. Thanks again for your response.
aressidi