123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- module NotificationFactory
- def self.build(data)
- data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /xm ) { |placeholder|
-
-
- original_string = $&
- callback = $1
- object_name = nil
- object_method = nil
- if callback =~ /\A ( [\w]+ )\.( [\w\.]+ ) \z/x
- object_name = $1
- object_method = $2
- end
-
- if callback =~ /(`|\.(|\s*)(save|destroy|delete|remove|drop|update\(|update_att|create\(|new|all|where|find))/i
- placeholder = "#{original_string} (not allowed)"
-
- elsif object_name && object_method
-
- if object_name == 'config'
- placeholder = Setting.get(object_method)
-
- elsif !data[:objects][object_name.to_sym]
- placeholder = "\#{#{object_name} / no such object}"
- else
- value = nil
- object_refs = data[:objects][object_name.to_sym]
- object_methods = object_method.split('.')
- object_methods_s = ''
- object_methods.each {|method|
- if object_methods_s != ''
- object_methods_s += '.'
- end
- object_methods_s += method
-
- if !object_refs.respond_to?( method.to_sym )
- value = "\#{#{object_name}.#{object_methods_s} / no such method}"
- break
- end
- object_refs = object_refs.send( method.to_sym )
-
- next if object_name != 'article'
- next if method != 'body'
- next if data[:objects][:article].content_type != 'text/html'
- object_refs = object_refs.html2text.chomp
- }
- if !value
- placeholder = object_refs
- else
- placeholder = value
- end
- end
- end
- placeholder
- }
-
- data[:string].gsub!( /i18n\((|.+?)\)/ ) {
- string = $1
- locale = data[:locale] || 'en'
- Translation.translate( locale, string )
- }
- data[:string]
- end
- def self.send(data)
- sender = Setting.get('notification_sender')
- Rails.logger.info "Send notification to: #{data[:recipient][:email]} (from #{sender})"
- content_type = 'text/plain'
- if data[:content_type]
- content_type = data[:content_type]
- end
-
- channel = Channel.find_by(area: 'Email::Notification', active: true)
- channel.deliver(
- {
-
- from: sender,
- to: data[:recipient][:email],
- subject: data[:subject],
- body: data[:body],
- content_type: content_type,
- },
- true
- )
- end
- end
|