Replacing email/notification.html via custom Plugin - tips?

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:

/my-email-plugin
├── plugin.rb
└── templates
    └── email
        ├── _post.html.erb
        ├── invite.html.erb
        ├── notification.html.erb
        └── template.html.erb

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. :slight_smile:

5 Likes