mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-14 13:16:28 +08:00
29 lines
762 B
Ruby
29 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
|