Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Last active September 30, 2015 07:47
Show Gist options
  • Select an option

  • Save jeffrafter/1746973 to your computer and use it in GitHub Desktop.

Select an option

Save jeffrafter/1746973 to your computer and use it in GitHub Desktop.
Abortable Action Mailer (no longer needed)
module AbortableMailer
class UndeliverableMailMessage < Mail::Message
def self.deliver
false
end
def self.deliver!
false
end
end
class AbortDeliveryError < StandardError
end
class Base < ActionMailer::Base
def abort_delivery
raise AbortDeliveryError
end
def process(*args)
begin
super *args
rescue AbortDeliveryError
self.message = AbortableMailer::UndeliverableMailMessage.new
end
end
end
end
class Notification < AbortableMailer::Base
def user_email(user_id)
abort_delivery if unsubscribed?(user_id)
end
end
@jeffrafter

Copy link
Copy Markdown
Author

NOTE: as of Rails 3.2.9 this is no longer necessary. Instead you can use the built in NullMail class:

class Notification < AbortableMailer::Base
  def user_email(user_id)
    return NullMail.new if unsubscribed?(user_id)

    # Send the mail normally
    mail(....) 
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment