0

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.

sn3ek
  • 1,929
  • 3
  • 22
  • 32

1 Answers1

2

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

HugoRune
  • 13,157
  • 7
  • 69
  • 144
marknach
  • 232
  • 2
  • 8
  • That is a good and nice way to solve my problem. Thank you! But I will use merit and try if it fits, because I missed the part that it also supports levels. – sn3ek Dec 17 '13 at 21:27
  • After checking the gem out I consider your approach because the gem is not flexible enough for my model structure. – sn3ek Dec 18 '13 at 09:27