Class: MailEngine::MailTemplateResolver

Inherits:
ActionView::Resolver
  • Object
show all
Includes:
Singleton
Defined in:
lib/mail_engine/mail_template_resolver.rb

Overview

path resolver, used for find template by provided path.

Instance Method Summary collapse

Instance Method Details

#find_templates(name, prefix, partial, details) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

Normalize arrays by converting all symbols to strings.



32
33
34
# File 'lib/mail_engine/mail_template_resolver.rb', line 32

def normalize_array(array)
  array.map(&:to_s)
end

#normalize_path(name, prefix) ⇒ Object

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



27
28
29
# 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) ⇒ Object

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



53
54
55
56
57
58
59
60
# 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