class Ditty::Emails::Base

Attributes

locals[RW]
mail[RW]
options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ditty/emails/base.rb, line 11
def initialize(options = {})
  @mail = options[:mail] || Mail.new
  @locals = options[:locals] || {}
  @options = base_options.merge options
end

Private Class Methods

deliver!(to = nil, options = {}) click to toggle source
# File lib/ditty/emails/base.rb, line 81
def deliver!(to = nil, options = {})
  locals = options[:locals] || {}
  new(options).deliver!(to, locals)
end

Public Instance Methods

deliver!(to = nil, locals = {}) click to toggle source
# File lib/ditty/emails/base.rb, line 17
def deliver!(to = nil, locals = {})
  options[:to] = to unless to.nil?
  @locals.merge!(locals)
  %i[to from subject content_type].each do |param|
    next unless options[param]

    @locals[param] ||= options[param]
    mail.send(param, options[param])
  end
  html = content
  mail.html_part do
    body html
  end
  mail.deliver!
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/ditty/emails/base.rb, line 33
def method_missing(method, *args, &block)
  return super unless respond_to_missing?(method)

  mail.send(method, *args, &block)
end
respond_to_missing?(method, _include_private = false) click to toggle source
Calls superclass method
# File lib/ditty/emails/base.rb, line 39
def respond_to_missing?(method, _include_private = false)
  return true if mail.respond_to?(method)

  super
end

Private Instance Methods

base_options() click to toggle source
# File lib/ditty/emails/base.rb, line 66
def base_options
  { subject: '(No Subject)', from: 'no-reply@ditty.io', view: :base, content_type: 'text/html; charset=UTF-8' }
end
content() click to toggle source
# File lib/ditty/emails/base.rb, line 47
def content
  result = Haml::Engine.new(content_haml).render(Object.new, locals)
  return result unless options[:layout]

  Haml::Engine.new(layout_haml).render(Object.new, locals.merge(content: result))
end
content_haml() click to toggle source
# File lib/ditty/emails/base.rb, line 54
def content_haml
  read_template(options[:view])
end
find_template(file) click to toggle source
# File lib/ditty/emails/base.rb, line 70
def find_template(file)
  template = File.expand_path("./views/#{file}.haml")
  return template if File.file? template

  template = File.expand_path("./#{file}.haml", ::Ditty::Ditty.view_folder)
  return template if File.file? template

  file
end
layout_haml() click to toggle source
# File lib/ditty/emails/base.rb, line 58
def layout_haml
  read_template("layouts/#{options[:layout]}") if options[:layout]
end
read_template(template) click to toggle source
# File lib/ditty/emails/base.rb, line 62
def read_template(template)
  File.read(find_template("emails/#{template}"))
end