notification_factory.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. module NotificationFactory
  2. def self.build(data)
  3. data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /x ) { |placeholder|
  4. # store possible callback to work with
  5. # and check if it's valid for execution
  6. original_string = $&
  7. callback = $1
  8. callback_valid = nil
  9. # use quoted text
  10. callback.gsub!( /\A ( \w+ ) \.body \z/x ) { |item|
  11. callback_valid = true
  12. item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp'
  13. }
  14. # use config params
  15. callback.gsub!( /\A config\.( [\w\.]+ ) \z/x ) { |item|
  16. callback_valid = true
  17. name = $1
  18. item = "Setting.get('#{name}')"
  19. }
  20. # use object params
  21. callback.gsub!( /\A ( [\w]+ )( \.[\w\.]+ ) \z/x ) { |item|
  22. object_name = $1
  23. object_method = $2
  24. next if !data[:objects][object_name.to_sym]
  25. callback_valid = true
  26. item = "data[:objects]['#{object_name}'.to_sym]#{object_method}"
  27. }
  28. if callback_valid
  29. # replace value
  30. begin
  31. placeholder = eval callback
  32. rescue Exception => e
  33. Rails.logger.error "Evaluation error caused by callback '#{callback}'"
  34. Rails.logger.error e.inspect
  35. end
  36. else
  37. placeholder = original_string
  38. end
  39. }
  40. # translate
  41. data[:string].gsub!( /i18n\((.+?)\)/ ) { |placeholder|
  42. string = $1
  43. locale = data[:locale] || 'en'
  44. placeholder = Translation.translate( locale, string )
  45. }
  46. return data[:string]
  47. end
  48. def self.send(data)
  49. sender = Setting.get('notification_sender')
  50. Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]} (from #{sender})"
  51. Channel::EmailSend.new.send(
  52. {
  53. # :in_reply_to => self.in_reply_to,
  54. :from => sender,
  55. :to => data[:recipient][:email],
  56. :subject => data[:subject],
  57. :body => data[:body],
  58. },
  59. true
  60. )
  61. end
  62. end