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}\"
<site-CHhhIV5UF21JHO4bupJ/Og@public.gmane.org>"
@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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Any ideas? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mozmonkey wrote:> 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}\" <site-CHhhIV5UF21JHO4bupJ/Og@public.gmane.org>" > @subject = subject > > body[:event] = event > end > endThere''s no specified way to do that in the API docs (although i''d not be surprised if a "return false" did the trick) otherwise, as you say, it works at the moment because an exception is raised. So, don''t rely on this bug. raise(EmailPermissionsError, "user doesn''t want invite mail") unless user.can_email_for(:invite) however, I reckon you''re wrong about putting it in the emailer. This is a user method. class User def invite(other_user-) return unless other_user.can_email_for(:invite) InviteMailer.deliver_invite... end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---