class MailEngine::MailTemplateResolver

path resolver, used for find template by provided path.

Public Instance Methods

find_templates(name, prefix, partial, details) click to toggle source
# File lib/mail_engine/mail_template_resolver.rb, line 9
def find_templates(name, prefix, partial, details)
  template_path_and_name = prefix.include?("mail_engine/mail_dispatcher") ? normalize_path(name, nil) : normalize_path(name, prefix)

  conditions = {
    :path    => template_path_and_name,
    :locale  => normalize_array(details[:locale]),
    :format  => normalize_array(details[:formats]),
    :handler => normalize_array(details[:handlers]),
    :partial => partial || false
  }

  MailTemplate.where(conditions).map do |record|
    initialize_template(record)
  end
end
initialize_template(record) click to toggle source

Initialize an ActionView::Template object based on the record found.

# File lib/mail_engine/mail_template_resolver.rb, line 37
def initialize_template(record)
  source = record.body

  identifier = "mail template - #{record.id} - #{record.path.inspect}"
  handler = ActionView::Template.registered_template_handler(record.handler)

  details = {
    :format => Mime[record.format],
    :updated_at => record.updated_at,
    :virtual_path => virtual_path(record.path, record.partial)
  }

  ActionView::Template.new(source, identifier, handler, details)
end
normalize_array(array) click to toggle source

Normalize arrays by converting all symbols to strings.

# File lib/mail_engine/mail_template_resolver.rb, line 32
def normalize_array(array)
  array.map(&:to_s)
end
normalize_path(name, prefix) click to toggle source

Normalize name and prefix, so the tuple [“index”, “users”] becomes “users/index” and the tuple [“template”, nil] becomes “template”.

# File lib/mail_engine/mail_template_resolver.rb, line 27
def normalize_path(name, prefix)
  prefix.present? ? "#{prefix}/#{name}" : name
end
virtual_path(path, partial) click to toggle source

Make paths as “users/user” become “users/_user” for partials.

# File lib/mail_engine/mail_template_resolver.rb, line 53
def virtual_path(path, partial)
  return path unless partial
  if index = path.rindex("/")
    path.insert(index + 1, "_")
  else
    "_#{path}"
  end
end