views:

18

answers:

2

When a user earns 10 points in my application, he gets an email saying "You just earned 10 points!"

If someone earns 10 points per day over a week, I don't mind sending him an email a day – but if someone earns 50 points in a single day, I don't want to send him 5 emails.

So I'd like a way to intercept each outgoing email before it gets sent, examine it, and determine whether I should send it or not. What's the best way to do this?

+1  A: 

I have a similar application, where I have a sent_previous_email_at timestamp field for the customer. I just check:

send_email if send_previous_email_at < DateTime.today - 1.days
Geoff Lanotte
+1  A: 

I would attach some sort of method to either the User model or the Point model (if you have something like that).

class User
  def add_points(count)
    # Add points
    send_email if criteria_is_met
  end

  def send_email
    # Send email
  end
end

Not exactly "global", but instead of calling the mailer directly, just use something like this so you can easily add conditional behavior.

Karl