Sha256: b4d5d3ff2b7cfa021067754cc81c5855500e71db9dc5ba7069788804fb652787

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

class MailEngine::MailDispatcher < ActionMailer::Base
  default :from => MailEngine::Base.current_config["default_from"]
  sendgrid_header do
    category MailEngine::Base.current_config["sendgrid"]["category_name"]
    filters {
      opentrack "enable" => 1
      clicktrack "enable" => 1
      subscriptiontrack "enable" => 0
      template "enable" => 0
      footer "enable" => 0
    }
  end

  protected

  ###
  # MailEngine::MailDispatcher.{any template name}(
  #   :subject => 'hello',
  #   :to => 'm@theplant.jp',
  #   :values => {:username => @user.name}
  # ).deliver
  #
  def self.method_missing(method, *args)
    return super if respond_to?(method)

    options = args.dup.extract_options!
    options[:locale] ||= I18n.locale

    templates = MailEngine::MailTemplate.where(:path => method, :locale => options[:locale], :for_marketing => true, :partial => false).order('format desc').all
    raise "No Template added." if templates.blank?

    subject = templates.first.subject
    formats = templates.map {|t| "format.#{t.format}" }.sort.reverse

    # generate the method for db template.
    class_eval <<-METHOD
      def #{method} options
        ### extract mail options, used for mail method.
        mail_options = {}
        mail_options[:subject] = "#{%Q{#{subject}}}"

        options.delete_if {|key, value|
          if [:subject, :to, :from, :cc, :bcc, :reply_to, :date].include?(key.to_sym)
            mail_options[key] = value
            true
          else
            false
          end
        }

        ### store :values to instance value
        (options[:values]||[]).each do |key, value|
          instance_variable_set "@" + key.to_s, value
        end

        mail mail_options do |format|
          #{formats.join(';')}
        end
      end
    METHOD

    new(method, *args).message
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mail_engine-0.1.10 app/mailers/mail_engine/mail_dispatcher.rb
mail_engine-0.1.9 app/mailers/mail_engine/mail_dispatcher.rb