Created
January 25, 2012 15:02
-
-
Save jnstq/1676654 to your computer and use it in GitHub Desktop.
MadApplicationMailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
require 'render_anywhere' | |
class MadApplicationMailer | |
include RenderAnywhere | |
attr_accessor :options, :body_options | |
def self.method_missing(name, *args, &blk) | |
if name.to_s =~ /^deliver_(\w+)$/ | |
perform_delivery($1, *args) | |
else | |
super | |
end | |
end | |
def self.perform_delivery(name, *args) | |
mail = new | |
mail.send("mimi_#{name}", *args) | |
mail.send_email! | |
end | |
def initialize | |
self.options = {} | |
self.body_options = {} | |
rendering_controller.prepend_view_path Rails.root.join('vendor', 'plugins', 'email_notifications', 'app', 'views') | |
rendering_controller.class.helper ProvidedLogo::LinkHelper, :starts, :renders, :meetings | |
rendering_controller.default_url_options[:host] = HOST | |
end | |
def lang(locale) | |
rendering_controller.singleton_class.class_eval do | |
define_method(:lang) { locale } | |
end | |
rendering_controller.class.helper_method :lang | |
end | |
[:content_type, :unconfirmed, :recipients, :from, :subject, :promotion_name, :bcc].each do |name| | |
define_method name do |*args| | |
if args.present? | |
options[name] = args.flatten.join(', ') | |
else | |
options[name] | |
end | |
end | |
end | |
alias promotion promotion_name | |
def body(*args) | |
if args.present? | |
attributes = args.extract_options! | |
body_options.merge!(attributes) | |
else | |
body_options | |
end | |
end | |
def send_email! | |
if Rails.env.test? | |
ActionMailer::Base.deliveries << self | |
else | |
mailer.send_mail(options.stringify_keys, body_options.stringify_keys) | |
end | |
end | |
protected | |
def mailer | |
@mailer ||= MadMimi.new(MADMIMI_CONFIG[:username], MADMIMI_CONFIG[:api_key]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment