Customisable email templates in Rails made easy using panoramic and liquid-rails

Photo by Persnickety Prints on Unsplash

The Demand

Panoramic

1. Creating model to store templates

$ rails g model CreateNotificationTemplates body:text path:string locale:string handler:string partial:boolean format:stringclass CreateNotificationTemplates < ActiveRecord::Migration[5.2]
def change
create_table :notification_templates do |t|
t.text :body
t.string :path
t.string :locale
t.string :handler
t.boolean :partial, default: false
t.string :format
t.timestamps
end
end
end
class NotificationTemplate < ApplicationRecord  store_templatesend
class FeedbackResponseMailer < ApplicationMailer
prepend_view_path NotificationTemplate.resolver
def send_response_mail(params)
@user = User.find(params[:user_id])
mail(to: @user.email, subject: 'Thank you')
end
end
#<NotificationTemplate:0x00007fe070fb0038> {
:id => 1,
:body => "Hi <%= @user.name %>, Thanks for your feedback",
:path => "feedback_response_mailer/send_response_mail",
:locale => nil,
:handler => "erb",
:partial => false,
:format => "html"
}

Adding WYSIWYG editor

<%= form_with(model: notification_template, local: true) do |form| %>
<div class="form">
<%= form.text_area :body, class: "tinymce", rows: 40, cols: 120 %>
<%= tinymce %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

Liquid-Rails

class FeedbackResponseMailer < ApplicationMailer
prepend_view_path NotificationTemplate.resolver
def send_response_mail
@user = User.find(params[:user_id])
mail(to: @user.email, subject: 'Thank you')
end
def liquid_assigns
{ 'user_name' => @user.name }
end
end
notification_template.update(
handler: 'liquid',
body: "<p>Hi {{user_name}}, Thanks for your feedback</p>"
)

Conclusion

Ruby on Rails Developer