safely, in production

a couple handy shortcuts that makes sending email in rails apps more robust:

$ cat config/initializers/object.rb 
class Object
  def safely *envs, &block
    return unless block_given?
    # TODO why not just use RAILS_ENV?
    yield  if envs.empty? || envs.any? { |e| e == ENV['RAILS_ENV'].to_sym }
  rescue Exception => e
    log_error e  if respond_to? :log_error
  end
end

once you have those, you can write your actionmailer code thusly:

class Mailer < ActionMailer::Base
  def notify
    safely :production do
      safely do
        # stuff
      end
    end
  end
end

or maybe you want to run stuff in production and development modes, but not test mode?

  safely :development, :production do
    # stuff
  end

test mode only?

  safely :test do
    # stuff
  end

or just run safely in all modes?

  safely do
    # stuff
  end

less retarded. have a better way?


About this entry