trabajo-afectivo/lib/notification_factory.rb

97 lines
1.8 KiB
Ruby
Raw Normal View History

module NotificationFactory
2014-12-28 13:02:23 +00:00
=begin
2016-04-13 23:40:37 +00:00
result = NotificationFactory.template_read(
2016-02-29 23:52:51 +00:00
template: 'password_reset',
locale: 'en-us',
2016-04-15 21:56:10 +00:00
format: 'html',
type: 'mailer',
)
or
result = NotificationFactory.template_read(
template: 'ticket_update',
locale: 'en-us',
format: 'md',
type: 'slack',
2016-02-29 23:52:51 +00:00
)
returns
{
2016-04-13 23:40:37 +00:00
subject: 'some subject',
2016-02-29 23:52:51 +00:00
body: 'some body',
}
=end
2016-04-13 23:40:37 +00:00
def self.template_read(data)
template_subject = nil
template_body = ''
locale = data[:locale] || Setting.get('locale_default') || 'en-us'
template = data[:template]
2016-04-13 23:40:37 +00:00
format = data[:format]
type = data[:type]
2016-03-02 10:43:57 +00:00
root = Rails.root
2016-04-13 23:40:37 +00:00
location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
# as fallback, use 2 char locale
if !File.exist?(location)
locale = locale[0, 2]
2016-04-13 23:40:37 +00:00
location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
end
# as fallback, use en
if !File.exist?(location)
2016-04-13 23:40:37 +00:00
location = "#{root}/app/views/#{type}/#{template}/en.#{format}.erb"
end
File.open(location, 'r:UTF-8').each do |line|
if !template_subject
template_subject = line
next
end
template_body += line
end
{
2016-04-13 23:40:37 +00:00
subject: template_subject,
body: template_body,
}
end
2016-04-13 23:40:37 +00:00
=begin
2016-04-13 23:40:37 +00:00
string = NotificationFactory.application_template_read(
2016-04-15 21:56:10 +00:00
format: 'html',
type: 'mailer',
)
or
string = NotificationFactory.application_template_read(
format: 'md',
type: 'slack',
2016-04-13 23:40:37 +00:00
)
2016-04-13 23:40:37 +00:00
returns
2016-04-13 23:40:37 +00:00
'some template'
2016-04-13 23:40:37 +00:00
=end
2016-04-13 23:40:37 +00:00
def self.application_template_read(data)
format = data[:format]
type = data[:type]
root = Rails.root
application_template = nil
File.open("#{root}/app/views/#{type}/application.#{format}.erb", 'r:UTF-8') do |file|
application_template = file.read
end
2016-04-13 23:40:37 +00:00
application_template
end
2016-04-13 23:40:37 +00:00
end