On my site I’m adding a feature to let users to turn off email
notifications for certain things. I want to add the code which checks
these preferences in my ActionMailer model and stop the delivery
process if the user has that notification preference turned off.
Here’s an example of what I’d like to do:
class Postoffice < ActionMailer::Base
# The user received an event invitation
def invite(event, invite, subject, message)
user = invite.user
return if !user.can_email_for(:invite)
@recipients = invite.user.email
@from = "\"#{event.user.name}\" <[email protected]>"
@subject = subject
body[:event] = event
end
end
The user function “can_email_for” will check the user’s preferences
for that type of notification and return true or false. Currently
this code works, but only because the rhtml template can’t compile
since the variables aren’t set (the return statement causes this).
That doesn’t seem like a solid way to stop the email delivery. Is
there a better way to do this?
I don’t want to put this logic in the code which calls
Postoffice.deliver_invite(…) because that’ll cause a lot of
redundant code and it seems to make more sense to have this sort of
logic in the ActionMailer model.
Thanks,
Jeremy