sexy multipart emails pulled from the mailers and re-rendered by mailboxer

This commit is contained in:
Devin Howard 2016-12-08 16:22:43 -05:00
parent 93af2ec84e
commit f010bbe6c9
5 changed files with 32 additions and 17 deletions

View file

@ -22,12 +22,10 @@ class AccessController < ApplicationController
request = AccessRequest.create(user: current_user, map: @map)
# what about push notification to map owner?
mail = MapMailer.access_request_email(request, @map)
@map.user.notify(mail.subject, mail.body.parts[1].body.to_s)
@map.user.notify(mail.subject, 'access request', request, true, MAILBOXER_CODE_ACCESS_REQUEST)
respond_to do |format|
format.json do
head :ok
end
format.json { head :ok }
end
end
@ -40,14 +38,12 @@ class AccessController < ApplicationController
# who we then send an email to
user = User.find(user_id)
mail = MapMailer.invite_to_edit_email(@map, current_user, User.find(user_id))
user.notify(mail.subject, mail.body.parts[1].body.to_s)
user.notify(mail.subject, 'invite to edit', @map, true, MAILBOXER_CODE_INVITED_TO_EDIT)
end
@map.remove_old_collaborators(user_ids)
respond_to do |format|
format.json do
head :ok
end
format.json { head :ok }
end
end

View file

@ -6,4 +6,16 @@ class ApplicationMailer < ActionMailer::Base
def deliver
raise NotImplementedError('Please use Mailboxer to send your emails.')
end
class << self
def mail_for_notification(notification)
if notification.notification_code == MAILBOXER_CODE_ACCESS_REQUEST
request = notification.notified_object
MapMailer.access_request_email(request, request.map)
elsif notification.notification_code == MAILBOXER_CODE_INVITED_TO_EDIT
map = notification.notified_object
MapMailer.invite_to_edit_email(map, map.user, 'TODO invited user')
end
end
end
end

View file

@ -1,9 +1,4 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<%= raw @notification.body %>
</body>
</html>
<% mail = ApplicationMailer.mail_for_notification(@notification) %>
<% @notification.update(body: mail.html_part&.body&.decoded) %>
<%= raw mail.html_part&.body&.decoded %>

View file

@ -1 +1,2 @@
<%= @notification.body.html_safe? ? @notification.body : strip_tags(@notification.body) %>
<% mail = ApplicationMailer.mail_for_notification(@notification) %>
<%= mail.text_part&.body&.decoded %>

View file

@ -1,4 +1,15 @@
# frozen_string_literal: true
# notification codes to differentiate different types of notifications
# e.g. a notification might have {
# notified_object_type: 'Map',
# notified_object_id: 1,
# notification_code: MAILBOXER_CODE_ACCESS_REQUEST
# },
# which would imply that this is an access request to Map.find(1)
MAILBOXER_CODE_ACCESS_REQUEST = 'ACCESS_REQUEST'
MAILBOXER_CODE_INVITED_TO_EDIT = 'INVITED_TO_EDIT'
Mailboxer.setup do |config|
# Configures if your application uses or not email sending for Notifications and Messages
config.uses_emails = true