Question

I'd like to implement a badge system; the user gains badges when doing stuff like "asking questions", "voting"...

However, I'm searching for a solution to define conditions to grant badges. One solution would be that I use an observer model to trigger the badges. But I would use a more elegant way to define the conditions the badge will be granted other than defining this within the observer model.

Example:

The user asked five questions each with a rating of at least five. In code I would define a condition like:

user.questions.keep_if{|q| q.rating >= 5 }.size >= 5

But I'd like to define this condition within the badge model object. Do you know what a good approach would be?

I'm aware of the merit badge gem; it is not suitable for my application, otherwise I would have used it by now.

I hope you can help me.

Was it helpful?

Solution

I would look into ActiveRecord callbacks.

Using a before_save callback would allow you to define a method containing your one-liner above, and test for it every time you save a user to the database

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top