2012-04-21 19:23:48 +00:00
|
|
|
module NotificationFactory
|
|
|
|
def self.build(data)
|
2013-01-03 10:47:39 +00:00
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
data[:string].gsub!( /\#\{(.+?)\}/ ) { |s|
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
# use quoted text
|
|
|
|
callback = $1
|
|
|
|
callback.gsub!( /\.body$/ ) { |item|
|
|
|
|
item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp'
|
|
|
|
}
|
|
|
|
|
|
|
|
# use config params
|
|
|
|
callback.gsub!( /^config\.(.+?)$/ ) { |item|
|
|
|
|
name = $1
|
|
|
|
item = "Setting.get('#{$1}')"
|
|
|
|
}
|
|
|
|
|
|
|
|
# use object params
|
|
|
|
callback.gsub!( /^(.+?)(\..+?)$/ ) { |item|
|
|
|
|
object_name = $1
|
|
|
|
object_method = $2
|
|
|
|
replace = nil
|
|
|
|
if data[:objects][object_name.to_sym]
|
|
|
|
replace = "data[:objects]['#{object_name}'.to_sym]#{object_method}"
|
2013-01-03 10:47:39 +00:00
|
|
|
else
|
2012-04-21 19:23:48 +00:00
|
|
|
replace = $1 + $2
|
|
|
|
end
|
|
|
|
item = replace
|
|
|
|
}
|
|
|
|
|
|
|
|
# replace value
|
2013-01-03 10:47:39 +00:00
|
|
|
begin
|
|
|
|
s = eval callback
|
|
|
|
rescue Exception => e
|
|
|
|
Rails.logger.error "can't eval #{callback}"
|
|
|
|
Rails.logger.error e.inspect
|
|
|
|
end
|
2012-04-21 19:23:48 +00:00
|
|
|
}
|
2013-01-04 14:28:55 +00:00
|
|
|
|
|
|
|
# translate
|
|
|
|
data[:string].gsub!( /i18n\((.+?)\)/ ) { |s|
|
|
|
|
string = $1
|
|
|
|
locale = data[:locale] || 'en'
|
|
|
|
s = Translation.translate( locale, string )
|
|
|
|
}
|
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
return data[:string]
|
|
|
|
end
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
def self.send(data)
|
|
|
|
sender = Setting.get('notification_sender')
|
|
|
|
a = Channel::IMAP.new
|
2013-01-03 10:47:39 +00:00
|
|
|
Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]}"
|
2012-04-21 19:23:48 +00:00
|
|
|
message = a.send(
|
|
|
|
{
|
|
|
|
# :in_reply_to => self.in_reply_to,
|
|
|
|
:from => sender,
|
2012-05-03 18:47:16 +00:00
|
|
|
:to => data[:recipient][:email],
|
2012-04-21 19:23:48 +00:00
|
|
|
:subject => data[:subject],
|
2014-06-08 22:01:20 +00:00
|
|
|
:body => data[:body],
|
2012-04-21 19:23:48 +00:00
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
2013-09-20 06:29:09 +00:00
|
|
|
end
|