Skip to content

Working with New Post Notifications

Brandon Robins edited this page Mar 31, 2015 · 6 revisions

When a post is published, Storytime will, by default, immediately send out an email to each active subscriber of your site notifying them of the new post. Since there are many different ways that developers may want to handle new notifications or email, Storytime provides an on_publish_with_notifications hook to let developers handle notifications/email however they'd like.

Publish with Notifications Hook

on_publish_with_notifications accepts a lambda or Proc object that is called when a post is published with the "Notify subscribers of new post" option selected. The hook can be used to prevent in-line emails, to schedule emails in the future, or to just handle new post notifications or email delivery differently.

Notable Notification Methods

There are two notable methods that can be used when working with post notifications. Both methods will send emails using the views at app/views/storytime/subscription_mailer/new_post_email.html and app/views/storytime/subscription_mailer/new_post_email.text

Storytime::PostNotifier.send_notifications_for(post_id) - Accepts a post ID. Will send emails to all active subscribers of the site where the post originated from. Uses mail.deliver (Rails < 4.2) and mail.deliver_now Rails >= 4.2) to send email immediately.

Storytime::SubscriptionMailer.new_post_email(post, sub) - Accepts a post and subscription object. Will send a single email to the email of the subscription.

Examples

Post Notification Emails using ActiveJob

This method uses ActiveJob, available in Rails 4.2+, to queue a custom job,StorytimePostNotificationJob, using a supported queueing backend (i.e. Sidekiq, Risque).

# config/initializers/storytime.rb
config.on_publish_with_notifications = Proc.new do |post|
  wait_until = post.published_at + 1.minute
  StorytimePostNotificationJob.set(wait_until: wait_until).perform_later(post.id)
end
# app/jobs/storytime_post_notification_job.rb 
class StorytimePostNotificationJob < ActiveJob::Base
  queue_as :mailers

  def perform(post_id)
    Storytime::PostNotifier.send_notifications_for(post_id)
  end
end

Post Notification Emails using whenever

This method uses whenever to setup a cron job that runs every 5 minutes and checks whether there are any new post notifications that need to be sent out.

# config/initializers/storytime.rb
config.on_publish_with_notifications = Proc.new do |post|
  # Do nothing else... Having this proc block present will prevent Storytime's default behavior (inline emails).
end
# config/schedule.rb
every 5.minutes do
  runner "StorytimePosts::Subscriptions.check_posts_and_send_notifications"
end
# lib/storytime_posts/subscriptions.rb
module StorytimePosts
  module Subscriptions
    def self.check_posts_and_send_notifications
      post_ids = Storytime::Post.published.notification_delivery_pending.pluck(:id)

      return if post_ids.empty?

      post_ids.each do |post_id|
        Storytime::PostNotifier.send_notifications_for(post_id)
      end
    end
  end
end