Merge pull request #218 from Ducz0r/lm-mail-previews

Add notification mailer preview
This commit is contained in:
Luka Murn 2016-10-13 14:58:04 +02:00 committed by GitHub
commit 845d487fa1
8 changed files with 121 additions and 15 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

@ -557,7 +557,7 @@ class Users::SettingsController < ApplicationController
end
def generate_notification(user, target_user, role, org)
title = I18n.t('activities.assign_user_to_organization',
title = I18n.t('notifications.assign_user_to_organization',
assigned_user: target_user.name,
role: role,
organization: org.name,

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,17 +1,17 @@
class AppMailer < Devise::Mailer
helper :application
helper :application, :mailer
include Devise::Controllers::UrlHelpers
default template_path: 'users/mailer'
default from: ENV["MAIL_FROM"]
default reply: ENV["MAIL_REPLYTO"]
def notification(user, notification)
def notification(user, notification, opts = {})
@user = user
@notification = notification
headers = {
to: @user.email,
subject: I18n.t('notifications.email_title')
}
}.merge(opts)
mail(headers)
end
end

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>

View file

@ -1056,7 +1056,6 @@ en:
delete_step_comment: "<i>%{user}</i> deleted comment on Step %{step} <strong>%{step_name}</strong>."
edit_result_comment: "<i>%{user}</i> edited comment on result <strong>%{result}</strong>."
delete_result_comment: "<i>%{user}</i> deleted comment on result <strong>%{result}</strong>."
assign_user_to_organization: "<i>%{assigned_user}</i> was added as %{role} to organization <strong>%{organization}</strong> by <i>%{assigned_by_user}</i>."
user_my_modules:
new:
@ -1509,6 +1508,8 @@ en:
recent_changes: "Recent changes"
system_message: "sciNote system message"
email_title: "You've received a sciNote notification!"
assign_user_to_organization: "<i>%{assigned_user}</i> was added as %{role} to organization <strong>%{organization}</strong> by <i>%{assigned_by_user}</i>."
# This section contains general words that can be used in any parts of
# application.

View file

@ -1,30 +1,101 @@
class AppMailerPreview < ActionMailer::Preview
def confirmation_instructions
AppMailer.confirmation_instructions(fake_user, "faketoken", {})
AppMailer.confirmation_instructions(fake_user, 'faketoken', {})
end
def reset_password_instructions
AppMailer.reset_password_instructions(fake_user, "faketoken", {})
AppMailer.reset_password_instructions(fake_user, 'faketoken', {})
end
def unlock_instructions
AppMailer.unlock_instructions(fake_user, "faketoken", {})
AppMailer.unlock_instructions(fake_user, 'faketoken', {})
end
def invitation_instructions
AppMailer.invitation_instructions(fake_user, "faketoken", {})
AppMailer.invitation_instructions(fake_user, 'faketoken', {})
end
def assignment_notification
AppMailer.notification(
fake_user,
Notification.new(
type_of: :assignment,
title: I18n.t(
'notifications.assign_user_to_organization',
assigned_user: fake_user_2.full_name,
role: 'Administrator',
organization: fake_org.name,
assigned_by_user: fake_user.full_name
),
message: ActionController::Base.helpers.sanitize(
"<a href='#' target='_blank'>#{fake_org.name}</a>"
),
generator_user: fake_user,
created_at: Time.now
)
)
end
def recent_changes_notification
user = User.first
user = fake_user if user.blank?
AppMailer.notification(
user,
Notification.new(
type_of: :recent_changes,
title: I18n.t(
'activities.create_module',
user: user.full_name,
module: 'How to shred'
),
message: ActionController::Base.helpers.sanitize(
'Project: <a href="#" target="_blank">School of Rock</a>'
),
generator_user: user,
created_at: Time.now
)
)
end
def system_message_notification
AppMailer.notification(
fake_user,
Notification.new(
type_of: :system_message,
title: 'sciNote 9.1 released!',
message: '<a href="#" target="_blank">View release notes</a>',
created_at: Time.now
)
)
end
private
def fake_user
User.new(
full_name: "Johny Cash",
initials: "JC",
email: "johny.cash@gmail.com",
full_name: 'Johny Cash',
initials: 'JC',
email: 'johny.cash@gmail.com',
created_at: Time.now,
updated_at: Time.now,
confirmed_at: Time.now
)
end
end
def fake_user_2
User.new(
full_name: 'Bob Dylan',
initials: 'BD',
email: 'bob.dylan@gmail.com',
created_at: Time.now,
updated_at: Time.now,
confirmed_at: Time.now
)
end
def fake_org
Organization.new(
name: 'Greatest musicians of all time'
)
end
end