forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification_factory.rb
80 lines (59 loc) · 1.69 KB
/
notification_factory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
76
77
78
79
80
module NotificationFactory
TEMPLATE_PATH_STRING = Rails.root.join('app', 'views', '%<type>s', '%<template>s', '%<filename>s').to_s.freeze
APPLICATION_TEMPLATE_PATH_STRING = Rails.root.join('app', 'views', '%<type>s', 'application.%<format>s.erb').to_s.freeze
=begin
result = NotificationFactory.template_read(
template: 'password_reset',
locale: 'en-us',
format: 'html',
type: 'mailer',
)
or
result = NotificationFactory.template_read(
template: 'ticket_update',
locale: 'en-us',
format: 'md',
type: 'slack',
)
returns
{
subject: 'some subject',
body: 'some body',
}
=end
def self.template_read(data)
template = File.readlines(template_path(data))
{ subject: template.shift, body: template.join }
end
def self.template_path(data)
template_filenames(data)
.map { |filename| data.merge(filename: filename) }
.map { |data_hash| TEMPLATE_PATH_STRING % data_hash }
.find(&File.method(:exist?))
end
private_class_method :template_path
def self.template_filenames(data)
locale = data[:locale] || Setting.get('locale_default') || 'en-us'
[locale, locale[0, 2], 'en']
.uniq
.map { |locale_code| "#{locale_code}.#{data[:format]}.erb" }
.map { |basename| ["#{basename}.custom", basename] }.flatten
end
private_class_method :template_filenames
=begin
string = NotificationFactory.application_template_read(
format: 'html',
type: 'mailer',
)
or
string = NotificationFactory.application_template_read(
format: 'md',
type: 'slack',
)
returns
'some template'
=end
def self.application_template_read(data)
File.read(APPLICATION_TEMPLATE_PATH_STRING % data)
end
end