Class: ActionMailer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/maildown/ext/action_mailer.rb

Overview

Monkeypatch to allow mailer to auto generate text/html

If you generate a mailer action, by default it will only render an html email:

def welcome
  mail(
    to:       "[email protected]",
    reply_to: "[email protected]",
    subject:  "hello world"
  )
end

You can add a format block to have it produce html and text emails:

def welcome
  mail(
    to:       "[email protected]",
    reply_to: "[email protected]",
    subject:  "hello world"
  ) do |format|
   format.text
   format.html
 end
end

For the handler to work correctly and produce both HTML and text emails this would need to be required similar to how github.com/plataformatec/markerb works.

This monkeypatch detects when a markdown email is being used and generates both a markdown and text template

Instance Method Summary collapse

Instance Method Details

#each_template(paths, name, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/maildown/ext/action_mailer.rb', line 42

def each_template(paths, name, &block)
  templates = original_each_template(paths, name, &block)

  return templates if templates.first.handler != Maildown::Handlers::Markdown

  html_template = templates.first

  # Cached template is already defined
  if html_template.instance_variable_defined?(:"@maildown_text_template")
    text_template = html_template.instance_variable_get(:"@maildown_text_template")
    return [html_template, text_template]
  end

  if Maildown.rails_6?
    text_template = html_template
      .class
      .new(
        html_template.source,
        html_template.identifier,
        html_template.handler,
        format: :text,
        locals: html_template.locals
      )
  else
    text_template = html_template.dup
    formats = html_template.formats.dup.tap { |f| f.delete(:html) }

    text_template.formats = formats
  end

  html_template.instance_variable_set(:"@maildown_text_template", text_template)

  return [html_template, text_template]
end

#original_each_templateObject



40
# File 'lib/maildown/ext/action_mailer.rb', line 40

alias :original_each_template :each_template