Bugfix for notification emails

The URLs in notification emails didn't get server URL prepended,
so users could not click on them.
This commit is contained in:
Luka Murn 2016-10-13 12:12:28 +02:00
parent 46aa25cb6c
commit 02d3e03f75
5 changed files with 37 additions and 3 deletions

View file

@ -50,6 +50,7 @@ gem 'redcarpet' # Markdown parser
gem 'faker' # Generate fake data
gem 'auto_strip_attributes', '~> 2.1' # Removes unnecessary whitespaces from ActiveRecord or ActiveModel attributes
gem 'deface', '~> 1.0'
gem 'nokogiri' # HTML/XML parser
gem 'paperclip', '~> 4.3' # File attachment, image attachment library
gem 'aws-sdk', '~> 2.2.8'

View file

@ -255,6 +255,9 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
scss_lint (0.50.2)
rake (>= 0.9, < 12)
sass (~> 3.4.20)
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
@ -346,6 +349,7 @@ DEPENDENCIES
minitest-reporters (~> 1.1)
momentjs-rails (>= 2.9.0)
nested_form_fields
nokogiri
paperclip (~> 4.3)
pg
puma
@ -358,6 +362,7 @@ DEPENDENCIES
rubocop
ruby-graphviz (~> 1.2)
sass-rails (~> 5.0)
scss_lint
sdoc (~> 0.4.0)
shoulda-context
shoulda-matchers (>= 3.0.1)

View file

@ -0,0 +1,28 @@
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

View file

@ -1,5 +1,5 @@
class AppMailer < Devise::Mailer
helper :application
helper :application, :mailer
include Devise::Controllers::UrlHelpers
default template_path: 'users/mailer'
default from: ENV["MAIL_FROM"]

View file

@ -4,6 +4,6 @@
<p>Type: <%= I18n.t("notifications.types.#{@notification.type_of}") %></p>
<p><%= @notification.title.html_safe %></p>
<p><%= prepend_server_url_to_links(@notification.title.html_safe).html_safe %></p>
<p><%= @notification.message.html_safe %></p>
<p><%= prepend_server_url_to_links(@notification.message).html_safe %></p>