scinote-web/app/helpers/mailer_helper.rb
Luka Murn 02d3e03f75 Bugfix for notification emails
The URLs in notification emails didn't get server URL prepended,
so users could not click on them.
2016-10-13 12:12:28 +02:00

28 lines
762 B
Ruby

module MailerHelper
# This method receives a HTML (as String), and
# prepends the server URL in front of URLs/paths
def prepend_server_url_to_links(html)
html_doc = Nokogiri::HTML.fragment(html)
links = html_doc.search('a')
links.each do |link|
href = link.attribute('href')
href.value = prepend_server_url(href.value) if href.present?
end
html_doc.to_html
end
private
def prepend_server_url(href)
return href if href.start_with? ENV['MAIL_SERVER_URL']
new_href = ''
unless ENV['MAIL_SERVER_URL'].start_with?('http://', 'https://')
new_href += 'http://'
end
new_href += ENV['MAIL_SERVER_URL']
new_href += ((href.start_with? '/') ? '' : '/')
new_href += href
new_href
end
end