I’ve reached a bit of an impasse and I’m looking for a spot of help.
As part of our switchover to Discourse, we want to change the design of the default email templates and obviously, as only the copy can be changed via the control panel, we figured we’d need to substitute the email templates by building a plugin that allows our email templates to persist between upgrades.
I’ve sussed out how to override the digests, thanks to this thread here:
…however, this doesn’t help when it comes to the notification.html.erb template, which seems to be impervious to ::ActionMailer::Base.prepend_view_path.
I assume this is what is responsible for setting the location of the template file to be used, but see no obvious way to manipulate this without overriding the method in a plugin and admittedly, my experience with Ruby is somewhat limited, so any help you could provide would be very much appreciated.
Well, since yesterdaty, I’ve managed to make something happen. I don’t think it’s particularly clean, but here it is.
In my plugin.rb file:
# This attempts to override the Notification Emails by sliding in
# our custom template directory first.
require_dependency 'user_notifications'
module ::UserNotificationsOverride
def send_notification_email(opts)
Rails.configuration.paths["app/views"].unshift(File.expand_path("../templates", __FILE__))
super(opts)
end
end
class ::UserNotifications
prepend ::UserNotificationsOverride
end
…and then adjacent to the plugin file is a template/ directory that contains customised .erb files using the same structure as below /app/views. So my plugin now looks like:
What I wanted to do was shift my custom templates folder ahead of the default, in an attempt to replicate what the ::ActionMailer::Base.prepend_view_path line was doing, but without breaking anything else or by carbon-copying the entire send_notification_email method for the sake of changing one line.
If you can think of any ways to improve this, then please do share.
У вас это сработало, и продолжает ли это работать?
Я попытался создать плагин, используя ваш код в plugin.rb и структуру шаблонов, но при попытке пересобрать приложение после добавления плагина в containers/app.yml возникает ошибка:
LoadError: Нет такого файла для загрузки – user_notifications.rb
Я понял, в чём была проблема. Код для plugin.rb нужно поместить внутрь after_initialize:
after_initialize do
require_dependency 'user_notifications'
module ::UserNotificationsOverride
def send_notification_email(opts)
Rails.configuration.paths["app/views"].unshift(File.expand_path("../templates", __FILE__))
super(opts)
end
end
class ::UserNotifications
prepend ::UserNotificationsOverride
end
end
У меня это теперь работает без проблем. Спасибо, что поделились этим методом!
Содержимое шаблонов notification.html.erb и _*post.html.erb по-прежнему нельзя настраивать, поэтому нам нужно будет найти способ решить эту проблему во второй части работы по настройке электронной почты.