Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active August 18, 2021 17:20
Show Gist options
  • Save stevepolitodesign/efe6a5b21707b969023dba0b8a767637 to your computer and use it in GitHub Desktop.
Save stevepolitodesign/efe6a5b21707b969023dba0b8a767637 to your computer and use it in GitHub Desktop.
Prevent a Mailer Delivery in Rails
class NotificationsMailer < ApplicationMailer
  # https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-callbacks  
  after_action :prevent_delivery

  def unread
    @user = params[:user]
    mail(to: @user.email, subject: 'Here's what you missed...')
  end

  private
    
    def prevent_delivery
      mail.perform_deliveries = @user.should_perform_delivery?
    end
end
class User < ApplicationRecord
  ...
  def should_perform_delivery?
    # @user.should_perform_delivery?
    # => false
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment