Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created July 2, 2010 14:16
Show Gist options
  • Save tcocca/461414 to your computer and use it in GitHub Desktop.
Save tcocca/461414 to your computer and use it in GitHub Desktop.
module ActionMailer
class Base
# call deliver! and then clear the theme out
# need to clear the theme on both success and failure do to errors
def deliver_with_clear_theme_path!(mail = @mail)
begin
deliver_without_clear_theme_path!
ensure
clear_theme_path
end
end
alias_method_chain :deliver!, :clear_theme_path
protected
# Allows us to use a custom mailer template for a theme
def set_theme(theme)
# make sure no left over theme paths are in the view paths
self.class.view_paths.delete_if{|dir_path| dir_path.path.match("themes")}
@theme_path = File.join(RAILS_ROOT, 'app', 'themes', theme, 'views')
self.class.view_paths.unshift(@theme_path) if File.exists?(@theme_path)
end
# Delete the theme path off of the view_paths after the emails is sent
# The view paths are cached so if 1 company has a custom theme template and the next company does not,
# that company would get the previous company's themed template instead of the default, unless that path is cleared out
def clear_theme_path
if @theme_path && self.class.view_paths.first == @theme_path
self.class.view_paths.shift
@theme_path = nil
end
end
end
end
class SomeMailer < ActionMailer::Base
def contact(company)
set_theme(company.theme)
# normal mailer stuff here ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment