notification_factory.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module NotificationFactory
  2. =begin
  3. result = NotificationFactory.template_read(
  4. template: 'password_reset',
  5. locale: 'en-us',
  6. format: 'html',
  7. type: 'mailer',
  8. )
  9. or
  10. result = NotificationFactory.template_read(
  11. template: 'ticket_update',
  12. locale: 'en-us',
  13. format: 'md',
  14. type: 'slack',
  15. )
  16. returns
  17. {
  18. subject: 'some subject',
  19. body: 'some body',
  20. }
  21. =end
  22. def self.template_read(data)
  23. template_subject = nil
  24. template_body = ''
  25. locale = data[:locale] || Setting.get('locale_default') || 'en-us'
  26. template = data[:template]
  27. format = data[:format]
  28. type = data[:type]
  29. root = Rails.root
  30. location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
  31. # as fallback, use 2 char locale
  32. if !File.exist?(location)
  33. locale = locale[0, 2]
  34. location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
  35. end
  36. # as fallback, use en
  37. if !File.exist?(location)
  38. location = "#{root}/app/views/#{type}/#{template}/en.#{format}.erb"
  39. end
  40. File.open(location, 'r:UTF-8').each do |line|
  41. if !template_subject
  42. template_subject = line
  43. next
  44. end
  45. template_body += line
  46. end
  47. {
  48. subject: template_subject,
  49. body: template_body,
  50. }
  51. end
  52. =begin
  53. string = NotificationFactory.application_template_read(
  54. format: 'html',
  55. type: 'mailer',
  56. )
  57. or
  58. string = NotificationFactory.application_template_read(
  59. format: 'md',
  60. type: 'slack',
  61. )
  62. returns
  63. 'some template'
  64. =end
  65. def self.application_template_read(data)
  66. format = data[:format]
  67. type = data[:type]
  68. root = Rails.root
  69. application_template = nil
  70. File.open("#{root}/app/views/#{type}/application.#{format}.erb", 'r:UTF-8') do |file|
  71. application_template = file.read
  72. end
  73. application_template
  74. end
  75. end