DEV Community

Shilpi Agrawal
Shilpi Agrawal

Posted on • Updated on

Integrating ActivityNotification & bootstrap_email in Rails 5+

Recently in our platform we started using activity_notifications for in-platform notifications and email notifications. But we wanted to use our already styled template files.

If you are struggling with the same problem, then you have come to right place.

We figured out a way to integrate bootstrap_email and activity_notifications

I am going to walk you through with basic setup of activity_notification emails and bootstrap_email and then using bootstrap_email styling for all activity_notifications.

1. Setting up ActivityNotification

To setup in-platform notification please follow gem's documentation

2. Setting up Email notification

To setup email notification please follow gem's documentation.

While following the documentation please make sure to include this

acts_as_target email: :email, email_allowed: :confirmed_at

in your target user model.

Once these two steps are ready you are good to go to start sending emails.

3. Setting up bootstrap email

To setup bootstrap for your emails, follow gem's documentation

Now, we have successfully enabled both the libraries in our platform. Now its the time to connect both of them.

4. Using custom mailer for activity_notification

While configuring activity_notification you must have created a config file for it, at config/activity_notification.rb which defines all the settings of activity_notification.

Update this below line in config/activity_notification.rb to use our custom mailer file.

config.mailer = "ActivityNotificationMailer"

And create a new file at app/mailers/activity_notification_mailer.rb

We are going to use this file to override one of the gem's function to use bootstrap_email function instead of using devise function.

class ActivityNotificationMailer < ActivityNotification::Mailer
  layout 'whatever_layout_you_are_using_for_emails'

  def send_mail(headers, fallback = nil)
    begin
      make_bootstrap_mail headers
    rescue ActionView::MissingTemplate => e
      if fallback.present?
        make_bootstrap_mail headers.merge(template_name: fallback)
      else
        raise e
      end
    end
  end
Enter fullscreen mode Exit fullscreen mode

Save this file and TADA you will be experiencing automated bootstrap styled emails getting sent from your platform.

Thank you for reading it out. Hope someone will find it helpful. And if you are that someone don’t forget to show us some love by giving some reactions and comments. :)

Top comments (0)